Laravel CRUD Project

Step 1: First install the latest composer:

Step 2: Go on xampp directory

c:\xampp\htdocs> composer create-project laravel/laravel crudlaravel

Step 3:- configure the database in the .env file.

create the database in mysql DB with the name crudlarvel

Set the database name, user name and password

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=crudlaravel
DB_USERNAME=root
DB_PASSWORD=

now we need to make one schema for the database.

Now, this is Movieticket’s table schema.

File :- create_movie_ticket_table.php

create create_movie_ticket_table.php file in databae migration directory.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMovieticketTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('movietickets', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->string('description');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('movietickets');
}
}

Run the following command.

php artisan migrate

It will create the tables in the database.

Step 4:- Laravel Authentication.

Just type the following command.

php artisan make:auth

File name : index.php

Start the Laravel server by the following command.

php artisan serve

We can register the user by the following URL: http://localhost/crudlaravel/

Step 5: Make models and controllers for our application.

Type the following cmd in the terminal.

php artisan make:controller TicketController

create model

The User model is already there, so no need to create again.

php artisan make:model Ticket

Step 6: Create the views for our application.

File name : create.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><br />
@endif
<div class="row">
<form method="post" action="{{url('/create/ticket')}}">
<div class="form-group">
<input type="hidden" value="{{csrf_token()}}" name="_token" />
<label for="title">Ticket Title:</label>
<input type="text" class="form-control" name="title"/>
</div>
<div class="form-group">
<label for="description">Ticket Description:</label>
<textarea cols="5" rows="5" class="form-control" name="description"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
</div>
</div>
@endsection

make a route to get this file

File name : web.php

Route::get('/create/ticket','TicketController@create');

Write the function in TicketController

File name : TicketController.php

public function create()
{
return view('user.create');
}

Note:

To store the data in a database, first, we need to prevent the mass assignment exception.
So, in the Ticket.php model, add the following attribute.

File name : Ticket.php

protected $fillable = ['user_id', 'title', 'description'];

Create one web route to handle the post request.
Route::post('/create/ticket','TicketController@store');

Step 7:

Include Ticket.php model into the TicketController.php file and write the store function code.

use App\Ticket;

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$ticket = new Ticket();
$data = $this->validate($request, [
'description'=>'required',
'title'=> 'required'
]);

$ticket->saveTicket($data);
return redirect('/home')->with('success', 'New support ticket has been created! Wait sometime to get resolved');
}

Also, we have created the saveTicket() function into the model.

File name : index.php

public function saveTicket($data)
{
$this->user_id = auth()->user()->id;
$this->title = $data['title'];
$this->description = $data['description'];
$this->save();
return 1;
}

Step 8: Display the tickets.

File name : home.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
@if(\Session::has('success'))
<div class="alert alert-success">
{{\Session::get('success')}}
</div>
@endif

<div class="row">
<a href="{{url('/create/ticket')}}" class="btn btn-success">Create Ticket</a>
<a href="{{url('/tickets')}}" class="btn btn-default">All Tickets</a>
</div>
</div>
@endsection

File name : index.php


example 2:

File name : index.php

<?php

namespace App\Http\Controllers;

use App\Student;
use Illuminate\Http\Request;

class StudentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$students = Student::all();
return view('student.list', compact('students','students'));
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
return view('student.create');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$request->validate([
'txtFirstName'=>'required',
'txtLastName'=> 'required',
'txtAddress' => 'required'
]);

$student = new Student([
'first_name' => $request->get('txtFirstName'),
'last_name'=> $request->get('txtLastName'),
'address'=> $request->get('txtAddress')
]);

$student->save();
return redirect('/student')->with('success', 'Student has been added');
}

/**
* Display the specified resource.
*
* @param \App\Student $student
* @return \Illuminate\Http\Response
*/
public function show(Student $student)
{
//
return view('student.view',compact('student'));
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Student $student
* @return \Illuminate\Http\Response
*/
public function edit(Student $student)
{
//
return view('student.edit',compact('student'));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Student $student
* @return \Illuminate\Http\Response
*/
public function update(Request $request,$id)
{
//

$request->validate([
'txtFirstName'=>'required',
'txtLastName'=> 'required',
'txtAddress' => 'required'
]);


$student = Student::find($id);
$student->first_name = $request->get('txtFirstName');
$student->last_name = $request->get('txtLastName');
$student->address = $request->get('txtAddress');

$student->update();

return redirect('/student')->with('success', 'Student updated successfully');
}

/**
* Remove the specified resource from storage.
*
* @param \App\Student $student
* @return \Illuminate\Http\Response
*/
public function destroy(Student $student)
{
//
$student->delete();
return redirect('/student')->with('success', 'Student deleted successfully');
}
}
?>

File name : index.php


Example :- CRUD

File name : index.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Resources\PostCollection;
use App\Post;

class PostController extends Controller
{
// all posts
public function index()
{
$posts = Post::all()->toArray();
return array_reverse($posts);
}

// add post
public function add(Request $request)
{
$post = new Post([
'title' => $request->input('title'),
'description' => $request->input('description')
]);
$post->save();

return response()->json('The post successfully added');
}

// edit post
public function edit($id)
{
$post = Post::find($id);
return response()->json($post);
}

// update post
public function update($id, Request $request)
{
$post = Post::find($id);
$post->update($request->all());

return response()->json('The post successfully updated');
}

// delete post
public function delete($id)
{
$post = Post::find($id);
$post->delete();

return response()->json('The post successfully deleted');
}
}

Define Routes in Web.php

Now define routes in the web.php and api.php routes file. So Go to Routes folder and Open web.php and Api.php file and update the following routes:

File name : index.php


Web.php

File name : web.php

<?php

Route::get('{any}', function () {
return view('app');
})->where('any', '.*');

Api.php

File name : api.php

<?php

use Illuminate\Http\Request;


Route::get('posts', 'PostController@index');
Route::group(['prefix' => 'post'], function () {
Route::post('add', 'PostController@add');
Route::get('edit/{id}', 'PostController@edit');
Route::post('update/{id}', 'PostController@update');
Route::delete('delete/{id}', 'PostController@delete');
});





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here


Ittutorial