Laravel 5.7 CRUD (Create Read Update Delete) Tutorial Example
In this post we will give you information about Laravel 5.7 CRUD (Create Read Update Delete) Tutorial Example. Hear we will give you detail about Laravel 5.7 CRUD (Create Read Update Delete) Tutorial ExampleAnd how to use it also give you demo for it if it is necessary.
In this Laravel 5.7 tutorial, I will let you know how to create basic CRUD functionality.
Laravel 5.7 has some other enhancement with awesome features.
In this example, You will get step by step guide that will help you to build a CRUD application easily.
This example will have following functionality :
- How to install Laravel 5.7 application
- How to configure MySQL database
- How to work on migration file to create tables
- How to use resource route
- How to create Controller and Model
- How to create Blade file
Step 1 : Install Laravel 5.7
In this first step, I will install the fresh Laravel 5.7 application by running following command :
composer create-project --prefer-dist laravel/laravel blog
Step 2 : Configure MySQl Database
In this step, I will create a database first in MySQL and connect database with Laravel application.
We will use environment variables for database credential. So let’s open .env
file :
.env
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=dbName DB_USERNAME=dbUsername DB_PASSWORD=dbPassword
Step 3: Create migration file to generate table
In this step, I will create a migration file for “members” table so that we can work with CRUD functionality in Laravel 5.7
command:
php artisan make:migration create_members_table --create=members
It will create a migration file in “database/migrations” directory.
Now I will update the below schema inside the file timestamp_create_members_table.php
<?php use IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; classCreateMembersTableextends Migration { /** * Run the migrations. * * @return void */ publicfunctionup() { Schema::create('members', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ publicfunctiondown() { Schema::dropIfExists('members'); } }
Now I will run the migration command to create table :
php artisan migrate
Step 4: Create model using artisan command
In this step, I will create Member model by running artisan command:
php artisan make:model Member
Now I will open the app/Member.php
file that is generated by above command and specify the fillable attribute for mass assignment.
<?php namespace App; use IlluminateDatabaseEloquentModel; classMemberextends Model { protected$fillable= [ 'name', 'email' ]; }
Step 5 : Add route
In this step, I will add resource route to display member list, view member details, display add member form, store member information, display edit member form, update member information and delete member information.
routes/web.php
Route::resource('members','MemberController');
Step 6: Create Controller
In this step, I will create a new resource controller by running artisan command :
php artisan make:controller MemberController --resource
Okay, now open MemberController.php file from the directory app/Http/Controllers/.
This MemberController has seven following methods to handle CRUD operation.
index()
– will list member data from the database.create()
– will display the add member form.store()
– will insert the member information into the members table.show()
– will display the member information.edit()
– will display the edit member form.update()
– will update the member information.destroy()
– will delete the member data from the database.
app/Http/Controllers/MemberController.php
<?php namespace AppHttpControllers; use AppMember; use IlluminateHttpRequest; classMemberControllerextends Controller { /** * Display a listing of the resource. * * @return IlluminateHttpResponse */ publicfunctionindex() { $members= Member::latest()->paginate(5); return view('members.index',compact('members')) ->with('i', (request()->input('page', 1) -1) *5); } /** * Show the form for creating a new resource. * * @return IlluminateHttpResponse */ publicfunctioncreate() { return view('members.create'); } /** * Store a newly created resource in storage. * * @param IlluminateHttpRequest $request * @return IlluminateHttpResponse */ publicfunctionstore(Request $request) { $request->validate([ 'name'=>'required', 'detail'=>'required', ]); Member::create($request->all()); return redirect()->route('members.index') ->with('success','Member created successfully.'); } /** * Display the specified resource. * * @param AppMember $member * @return IlluminateHttpResponse */ publicfunctionshow(Member $member) { return view('members.show',compact('member')); } /** * Show the form for editing the specified resource. * * @param AppMember $member * @return IlluminateHttpResponse */ publicfunctionedit(Member $member) { return view('members.edit',compact('member')); } /** * Update the specified resource in storage. * * @param IlluminateHttpRequest $request * @param AppMember $member * @return IlluminateHttpResponse */ publicfunctionupdate(Request $request, Member $member) { $request->validate([ 'name'=>'required', 'detail'=>'required', ]); $member->update($request->all()); return redirect()->route('members.index') ->with('success','Member updated successfully'); } /** * Remove the specified resource from storage. * * @param AppMember $member * @return IlluminateHttpResponse */ publicfunctiondestroy(Member $member) { $member->delete(); return redirect()->route('members.index') ->with('success','Member deleted successfully'); } }
Step 7: Create Blade Files
In this step, I will create master template and some blade file related to member management.
- layout.blade.php
- index.blade.php
- create.blade.php
- edit.blade.php
- show.blade.php
Okay, now I will create master template inside the views folder.
resources/views/layout.blade.php
<!DOCTYPE html> <html> <head> <title>Build CRUD Application in Laravel 5.7 - onlinecode </title> <linkhref="{{ asset('css/app.css') }}"rel="stylesheet"type="text/css"/> </head> <body> <divclass="container"> @yield('content') </div> </body> </html>
Now create members folder inside resources >> views
and create all files inside that members folder.
resources/views/members/index.blade.php
@extends('layout') @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
resources/views/members/form.blade.php
I will create a common form that will use in create and edit module.
<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>
resources/views/members/create.blade.php
@extends('layout') @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
resources/views/members/edit.blade.php
@extends('layout') @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
resources/views/members/show.blade.php
@extends('layout') @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
- Laravel 5.5 CRUD (Create Read Update Delete) Example from scratch
- 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.7 CRUD (Create Read Update Delete) Tutorial Example. 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