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 InstallationFirst, 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.
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.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=blog DB_USERNAME=root DB_PASSWORD=xxxxCreate 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 Illuminate\Support\Facades\Schema;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- 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 Illuminate\Database\Eloquent\Model;
- class Member extends Model
- {
- protected $fillable = [
- 'name', 'email'
- ];
- }
Laravel provides single resource route instead of creating different different routes for each operation.
routes/web.phpRoute::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.phpPut following code in Member Controller.
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Member;
- 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 \Illuminate\Http\Response
- */
- public function create()
- {
- return view('members.create');
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- 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 \Illuminate\Http\Response
- */
- public function show(Member $member)
- {
- return view('members.show',compact('member'));
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit(Member $member)
- {
- return view('members.edit',compact('member'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- 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 \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- Member::destroy($id);
- return redirect()->route('members.index')
- ->with('success','Member deleted successfully');
- }
- }
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
- <!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 class="container">
- @yield('content')
- </div>
- </body>
- </html>
- @extends('layouts.default')
- @section('content')
- <div class="row">
- <div class="col-lg-12 margin-tb">
- <div class="pull-left">
- <h2>Members CRUD</h2>
- </div>
- <div class="pull-right">
- <a class="btn btn-success" href="{{ route('members.create') }}"> Create New Member</a>
- </div>
- </div>
- </div>
- @if ($message = Session::get('success'))
- <div class="alert alert-success">
- <p>{{ $message }}</p>
- </div>
- @endif
- <table class="table table-bordered">
- <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 class="btn btn-info" href="{{ route('members.show',$member->id) }}">Show</a>
- <a class="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 class="row">
- <div class="col-lg-12 margin-tb">
- <div class="pull-left">
- <h2> Show Member</h2>
- </div>
- <div class="pull-right">
- <a class="btn btn-primary" href="{{ route('members.index') }}"> Back</a>
- </div>
- </div>
- </div>
- <div class="row">
- <div class="col-xs-12 col-sm-12 col-md-12">
- <div class="form-group">
- <strong>Name:</strong>
- {{ $member->name}}
- </div>
- </div>
- <div class="col-xs-12 col-sm-12 col-md-12">
- <div class="form-group">
- <strong>Email:</strong>
- {{ $member->email}}
- </div>
- </div>
- </div>
- @endsection
- <div class="row">
- <div class="col-xs-12 col-sm-12 col-md-12">
- <div class="form-group">
- <strong>Name:</strong>
- {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
- </div>
- </div>
- <div class="col-xs-12 col-sm-12 col-md-12">
- <div class="form-group">
- <strong>Email:</strong>
- {!! Form::email('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
- </div>
- </div>
- <div class="col-xs-12 col-sm-12 col-md-12 text-center">
- <button type="submit" class="btn btn-primary">Submit</button>
- </div>
- </div>
- @extends('layouts.default')
- @section('content')
- <div class="row">
- <div class="col-lg-12 margin-tb">
- <div class="pull-left">
- <h2>Add New Member</h2>
- </div>
- <div class="pull-right">
- <a class="btn btn-primary" href="{{ route('members.index') }}"> Back</a>
- </div>
- </div>
- </div>
- @if (count($errors) > 0)
- <div class="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 class="row">
- <div class="col-lg-12 margin-tb">
- <div class="pull-left">
- <h2>Edit Member</h2>
- </div>
- <div class="pull-right">
- <a class="btn btn-primary" href="{{ route('members.index') }}"> Back</a>
- </div>
- </div>
- </div>
- @if (count($errors) > 0)
- <div class="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
Now you can perform the crud application after following above steps.