Laravel 5.5 CRUD (Create Read Update Delete) Example from scratch
In this post we will give you information about Laravel 5.5 CRUD (Create Read Update Delete) Example from scratch. Hear we will give you detail about Laravel 5.5 CRUD (Create Read Update Delete) Example from scratchAnd how to use it also give you demo for it if it is necessary.
In this Laravel tutorial, You will learn the basic CRUD (Create Read Update Delete) functionality in Laravel 5.5.
Laravel is open-source web application framework with easy documentation.
If you are thinking to build an web application from scratch in Laravel then this example will help you out.
This example is basically designed for the beginners.
In this example, you will learn how to insert form data into a database, how can you modify the existing data, in short, you will learn the complete CRUD operation with the database.
Laravel 5.5 Installation
First, I need fresh Laravel 5.5 application to start from scratch.
Run the following command to install the upgraded version of Laravel :
composer create-project --prefer-dist laravel/laravel blog
With fresh Laravel 5.5 application, You will need to require Laravel Collective
package for form builder : Laravel Collective.
Database Connection
To perform CRUD operation, You need to configure the MySQL database.
Once you have installed Laravel application then you should have .env
file in the root directory which contains the various settings. So we will put database credentials in the .env
file.
.env
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=blog DB_USERNAME=root DB_PASSWORD=xxxx
Create Member Table, Model and Controller
We will work on member table to perform all CRUD operation.
Run following command to have migration file for member table.
php artisan make:migration create_members_table
After this artisan command, You must have new migration file like “2017_09_06_071236_create_members_table.php” in following path database/migrations.
- <?php
- use IlluminateSupportFacadesSchema;
- use IlluminateDatabaseSchemaBlueprint;
- use IlluminateDatabaseMigrationsMigration;
- class CreateMembersTable extends Migration
- {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public functionup()
- {
- Schema::create('members',function(Blueprint $table){
- $table->increments('id');
- $table->string('name');
- $table->string('email');
- $table->timestamps();
- });
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public functiondown()
- {
- Schema::dropIfExists('members');
- }
- }
<?php use IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateMembersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('members', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('members'); } }
Save the migration file with above code and run following command :
php artisan migrate
Now you have members table with id, name, email and timestamps fields.
Ok let’s create the model and controller in single command :
php artisan make:model Member -c
After above command, you will get the Member model and MemberController.
app/Member.php
- <?php
- namespace App;
- use IlluminateDatabaseEloquentModel;
- class Member extends Model
- {
- protected $fillable=[
- 'name','email'
- ];
- }
<?php namespace App; use IlluminateDatabaseEloquentModel; class Member extends Model { protected $fillable = [ 'name', 'email' ]; }
Resource Route
Laravel provides single resource route instead of creating different different routes for each operation.
routes/web.php
Route::resource('members','MemberController');
MemberController
You will notice that there will be some default method available with Member Controller after running artisan command.
app/Http/Controllers/MemberController.php
Put following code in Member Controller.
- <?php
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- use AppMember;
- class MemberController extends Controller
- {
- public functionindex()
- {
- $members= Member::latest()->paginate(10);
- returnview('members.index',compact('members'))
- ->with('i',(request()->input('page',1)-1)*5);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return IlluminateHttpResponse
- */
- public functioncreate()
- {
- returnview('members.create');
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param IlluminateHttpRequest $request
- * @return IlluminateHttpResponse
- */
- public functionstore(Request $request)
- {
- request()->validate([
- 'name'=>'required',
- 'email'=>'required',
- ]);
- Member::create($request->all());
- returnredirect()->route('members.index')
- ->with('success','Member created successfully');
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return IlluminateHttpResponse
- */
- public functionshow(Member $member)
- {
- returnview('members.show',compact('member'));
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return IlluminateHttpResponse
- */
- public functionedit(Member $member)
- {
- returnview('members.edit',compact('member'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param IlluminateHttpRequest $request
- * @param int $id
- * @return IlluminateHttpResponse
- */
- public functionupdate(Request $request,Member $member)
- {
- request()->validate([
- 'name'=>'required',
- 'email'=>'required',
- ]);
- $member->update($request->all());
- returnredirect()->route('members.index')
- ->with('success','Member updated successfully');
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return IlluminateHttpResponse
- */
- public functiondestroy($id)
- {
- Member::destroy($id);
- returnredirect()->route('members.index')
- ->with('success','Member deleted successfully');
- }
- }
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppMember; class MemberController extends Controller { public function index() { $members = Member::latest()->paginate(10); return view('members.index',compact('members')) ->with('i', (request()->input('page', 1) - 1) * 5); } /** * Show the form for creating a new resource. * * @return IlluminateHttpResponse */ public function create() { return view('members.create'); } /** * Store a newly created resource in storage. * * @param IlluminateHttpRequest $request * @return IlluminateHttpResponse */ public function store(Request $request) { request()->validate([ 'name' => 'required', 'email' => 'required', ]); Member::create($request->all()); return redirect()->route('members.index') ->with('success','Member created successfully'); } /** * Display the specified resource. * * @param int $id * @return IlluminateHttpResponse */ public function show(Member $member) { return view('members.show',compact('member')); } /** * Show the form for editing the specified resource. * * @param int $id * @return IlluminateHttpResponse */ public function edit(Member $member) { return view('members.edit',compact('member')); } /** * Update the specified resource in storage. * * @param IlluminateHttpRequest $request * @param int $id * @return IlluminateHttpResponse */ public function update(Request $request,Member $member) { request()->validate([ 'name' => 'required', 'email' => 'required', ]); $member->update($request->all()); return redirect()->route('members.index') ->with('success','Member updated successfully'); } /** * Remove the specified resource from storage. * * @param int $id * @return IlluminateHttpResponse */ public function destroy($id) { Member::destroy($id); return redirect()->route('members.index') ->with('success','Member deleted successfully'); } }
Blade View Files
In this step, I will create some view files to add members, list members, edit members with master layouts.
- default.blade.php
- index.blade.php
- form.blade.php
- create.blade.php
- edit.blade.php
- show.blade.php
resources/views/layouts/default.blade.php
- <!DOCTYPEhtml>
- <htmllang="en">
- <head>
- <metacharset="utf-8">
- <metahttp-equiv="X-UA-Compatible"content="IE=edge">
- <metaname="viewport"content="width=device-width, initial-scale=1">
- <title>Laravel 5.5 CRUD example</title>
- <linkhref="{{asset('css/app.css')}}"rel="stylesheet">
- </head>
- <body>
- <divclass="container">
- @yield('content')
- </div>
- </body>
- </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel 5.5 CRUD example</title> <link href="{{asset('css/app.css')}}" rel="stylesheet"> </head> <body> <div > @yield('content') </div> </body> </html>
resources/views/members/index.blade.php
- @extends('layouts.default')
- @section('content')
- <divclass="row">
- <divclass="col-lg-12 margin-tb">
- <divclass="pull-left">
- <h2>Members CRUD</h2>
- </div>
- <divclass="pull-right">
- <aclass="btn btn-success"href="{{ route('members.create') }}"> Create New Member</a>
- </div>
- </div>
- </div>
- @if ($message = Session::get('success'))
- <divclass="alert alert-success">
- <p>{{ $message }}</p>
- </div>
- @endif
- <tableclass="table table-bordered">
- <tr>
- <th>No</th>
- <th>Name</th>
- <th>Email</th>
- <thwidth="280px">Operation</th>
- </tr>
- @foreach ($members as $member)
- <tr>
- <td>{{ ++$i }}</td>
- <td>{{ $member->name}}</td>
- <td>{{ $member->email}}</td>
- <td>
- <aclass="btn btn-info"href="{{ route('members.show',$member->id) }}">Show</a>
- <aclass="btn btn-primary"href="{{ route('members.edit',$member->id) }}">Edit</a>
- {!! Form::open(['method' => 'DELETE','route' => ['members.destroy', $member->id],'style'=>'display:inline']) !!}
- {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
- {!! Form::close() !!}
- </td>
- </tr>
- @endforeach
- </table>
- {!! $members->render() !!}
- @endsection
@extends('layouts.default') @section('content') <div > <div > <div > <h2>Members CRUD</h2> </div> <div > <a href="{{ route('members.create') }}"> Create New Member</a> </div> </div> </div> @if ($message = Session::get('success')) <div > <p>{{ $message }}</p> </div> @endif <table > <tr> <th>No</th> <th>Name</th> <th>Email</th> <th width="280px">Operation</th> </tr> @foreach ($members as $member) <tr> <td>{{ ++$i }}</td> <td>{{ $member->name}}</td> <td>{{ $member->email}}</td> <td> <a href="{{ route('members.show',$member->id) }}">Show</a> <a href="{{ route('members.edit',$member->id) }}">Edit</a> {!! Form::open(['method' => 'DELETE','route' => ['members.destroy', $member->id],'style'=>'display:inline']) !!} {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} {!! Form::close() !!} </td> </tr> @endforeach </table> {!! $members->render() !!} @endsection
resources/views/members/show.blade.php
- @extends('layouts.default')
- @section('content')
- <divclass="row">
- <divclass="col-lg-12 margin-tb">
- <divclass="pull-left">
- <h2> Show Member</h2>
- </div>
- <divclass="pull-right">
- <aclass="btn btn-primary"href="{{ route('members.index') }}"> Back</a>
- </div>
- </div>
- </div>
- <divclass="row">
- <divclass="col-xs-12 col-sm-12 col-md-12">
- <divclass="form-group">
- <strong>Name:</strong>
- {{ $member->name}}
- </div>
- </div>
- <divclass="col-xs-12 col-sm-12 col-md-12">
- <divclass="form-group">
- <strong>Email:</strong>
- {{ $member->email}}
- </div>
- </div>
- </div>
- @endsection
@extends('layouts.default') @section('content') <div > <div > <div > <h2> Show Member</h2> </div> <div > <a href="{{ route('members.index') }}"> Back</a> </div> </div> </div> <div > <div > <div > <strong>Name:</strong> {{ $member->name}} </div> </div> <div > <div > <strong>Email:</strong> {{ $member->email}} </div> </div> </div> @endsection
resources/views/members/form.blade.php
- <divclass="row">
- <divclass="col-xs-12 col-sm-12 col-md-12">
- <divclass="form-group">
- <strong>Name:</strong>
- {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
- </div>
- </div>
- <divclass="col-xs-12 col-sm-12 col-md-12">
- <divclass="form-group">
- <strong>Email:</strong>
- {!! Form::email('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
- </div>
- </div>
- <divclass="col-xs-12 col-sm-12 col-md-12 text-center">
- <buttontype="submit"class="btn btn-primary">Submit</button>
- </div>
- </div>
<div > <div > <div > <strong>Name:</strong> {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!} </div> </div> <div > <div > <strong>Email:</strong> {!! Form::email('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!} </div> </div> <div > <button type="submit" >Submit</button> </div> </div>
resources/views/members/create.blade.php
- @extends('layouts.default')
- @section('content')
- <divclass="row">
- <divclass="col-lg-12 margin-tb">
- <divclass="pull-left">
- <h2>Add New Member</h2>
- </div>
- <divclass="pull-right">
- <aclass="btn btn-primary"href="{{ route('members.index') }}"> Back</a>
- </div>
- </div>
- </div>
- @if (count($errors) > 0)
- <divclass="alert alert-danger">
- <strong>Whoops!</strong> There were some problems with your input.<br><br>
- <ul>
- @foreach ($errors->all() as $error)
- <li>{{ $error }}</li>
- @endforeach
- </ul>
- </div>
- @endif
- {!! Form::open(array('route' => 'members.store','method'=>'POST')) !!}
- @include('members.form')
- {!! Form::close() !!}
- @endsection
@extends('layouts.default') @section('content') <div > <div > <div > <h2>Add New Member</h2> </div> <div > <a href="{{ route('members.index') }}"> Back</a> </div> </div> </div> @if (count($errors) > 0) <div > <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif {!! Form::open(array('route' => 'members.store','method'=>'POST')) !!} @include('members.form') {!! Form::close() !!} @endsection
resources/views/members/edit.blade.php
- @extends('layouts.default')
- @section('content')
- <divclass="row">
- <divclass="col-lg-12 margin-tb">
- <divclass="pull-left">
- <h2>Edit Member</h2>
- </div>
- <divclass="pull-right">
- <aclass="btn btn-primary"href="{{ route('members.index') }}"> Back</a>
- </div>
- </div>
- </div>
- @if (count($errors) > 0)
- <divclass="alert alert-danger">
- <strong>Whoops!</strong> There were some problems with your input.<br><br>
- <ul>
- @foreach ($errors->all() as $error)
- <li>{{ $error }}</li>
- @endforeach
- </ul>
- </div>
- @endif
- {!! Form::model($member, ['method' => 'PATCH','route' => ['members.update', $member->id]]) !!}
- @include('members.form')
- {!! Form::close() !!}
- @endsection
@extends('layouts.default') @section('content') <div > <div > <div > <h2>Edit Member</h2> </div> <div > <a href="{{ route('members.index') }}"> Back</a> </div> </div> </div> @if (count($errors) > 0) <div > <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif {!! Form::model($member, ['method' => 'PATCH','route' => ['members.update', $member->id]]) !!} @include('members.form') {!! Form::close() !!} @endsection
Now you can perform the crud application after following above steps.
- Laravel 5.2 CRUD (Create Read Update Delete) Example
- Laravel 5 Ajax CRUD example
Hope this code and post will helped you for implement Laravel 5.5 CRUD (Create Read Update Delete) Example from scratch. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs