Laravel 5.5 CRUD (Create Read Update Delete) Example from scratch

Laravel 5.5 CRUD (Create Read Update Delete) Example from scratch

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.

  1. <?php
  2. use Illuminate\Support\Facades\Schema;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Database\Migrations\Migration;
  5. class CreateMembersTable extends Migration
  6. {
  7. /**
  8. * Run the migrations.
  9. *
  10. * @return void
  11. */
  12. public function up()
  13. {
  14. Schema::create('members', function (Blueprint $table) {
  15. $table->increments('id');
  16. $table->string('name');
  17. $table->string('email');
  18. $table->timestamps();
  19. });
  20. }
  21. /**
  22. * Reverse the migrations.
  23. *
  24. * @return void
  25. */
  26. public function down()
  27. {
  28. Schema::dropIfExists('members');
  29. }
  30. }

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
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Member extends Model
  5. {
  6. protected $fillable = [
  7. 'name', 'email'
  8. ];
  9. }
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.

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Member;
  5. class MemberController extends Controller
  6. {
  7. public function index()
  8. {
  9. $members = Member::latest()->paginate(10);
  10. return view('members.index',compact('members'))
  11. ->with('i', (request()->input('page', 1) - 1) * 5);
  12. }
  13. /**
  14. * Show the form for creating a new resource.
  15. *
  16. * @return \Illuminate\Http\Response
  17. */
  18. public function create()
  19. {
  20. return view('members.create');
  21. }
  22. /**
  23. * Store a newly created resource in storage.
  24. *
  25. * @param \Illuminate\Http\Request $request
  26. * @return \Illuminate\Http\Response
  27. */
  28. public function store(Request $request)
  29. {
  30. request()->validate([
  31. 'name' => 'required',
  32. 'email' => 'required',
  33. ]);
  34. Member::create($request->all());
  35. return redirect()->route('members.index')
  36. ->with('success','Member created successfully');
  37. }
  38. /**
  39. * Display the specified resource.
  40. *
  41. * @param int $id
  42. * @return \Illuminate\Http\Response
  43. */
  44. public function show(Member $member)
  45. {
  46. return view('members.show',compact('member'));
  47. }
  48. /**
  49. * Show the form for editing the specified resource.
  50. *
  51. * @param int $id
  52. * @return \Illuminate\Http\Response
  53. */
  54. public function edit(Member $member)
  55. {
  56. return view('members.edit',compact('member'));
  57. }
  58. /**
  59. * Update the specified resource in storage.
  60. *
  61. * @param \Illuminate\Http\Request $request
  62. * @param int $id
  63. * @return \Illuminate\Http\Response
  64. */
  65. public function update(Request $request,Member $member)
  66. {
  67. request()->validate([
  68. 'name' => 'required',
  69. 'email' => 'required',
  70. ]);
  71. $member->update($request->all());
  72. return redirect()->route('members.index')
  73. ->with('success','Member updated successfully');
  74. }
  75. /**
  76. * Remove the specified resource from storage.
  77. *
  78. * @param int $id
  79. * @return \Illuminate\Http\Response
  80. */
  81. public function destroy($id)
  82. {
  83. Member::destroy($id);
  84. return redirect()->route('members.index')
  85. ->with('success','Member deleted successfully');
  86. }
  87. }
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
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>Laravel 5.5 CRUD example</title>
  8. <link href="{{asset('css/app.css')}}" rel="stylesheet">
  9. </head>
  10. <body>
  11. <div class="container">
  12. @yield('content')
  13. </div>
  14. </body>
  15. </html>
resources/views/members/index.blade.php
  1. @extends('layouts.default')
  2. @section('content')
  3. <div class="row">
  4. <div class="col-lg-12 margin-tb">
  5. <div class="pull-left">
  6. <h2>Members CRUD</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-success" href="{{ route('members.create') }}"> Create New Member</a>
  10. </div>
  11. </div>
  12. </div>
  13. @if ($message = Session::get('success'))
  14. <div class="alert alert-success">
  15. <p>{{ $message }}</p>
  16. </div>
  17. @endif
  18. <table class="table table-bordered">
  19. <tr>
  20. <th>No</th>
  21. <th>Name</th>
  22. <th>Email</th>
  23. <th width="280px">Operation</th>
  24. </tr>
  25. @foreach ($members as $member)
  26. <tr>
  27. <td>{{ ++$i }}</td>
  28. <td>{{ $member->name}}</td>
  29. <td>{{ $member->email}}</td>
  30. <td>
  31. <a class="btn btn-info" href="{{ route('members.show',$member->id) }}">Show</a>
  32. <a class="btn btn-primary" href="{{ route('members.edit',$member->id) }}">Edit</a>
  33. {!! Form::open(['method' => 'DELETE','route' => ['members.destroy', $member->id],'style'=>'display:inline']) !!}
  34. {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
  35. {!! Form::close() !!}
  36. </td>
  37. </tr>
  38. @endforeach
  39. </table>
  40. {!! $members->render() !!}
  41. @endsection
resources/views/members/show.blade.php
  1. @extends('layouts.default')
  2. @section('content')
  3. <div class="row">
  4. <div class="col-lg-12 margin-tb">
  5. <div class="pull-left">
  6. <h2> Show Member</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-primary" href="{{ route('members.index') }}"> Back</a>
  10. </div>
  11. </div>
  12. </div>
  13. <div class="row">
  14. <div class="col-xs-12 col-sm-12 col-md-12">
  15. <div class="form-group">
  16. <strong>Name:</strong>
  17. {{ $member->name}}
  18. </div>
  19. </div>
  20. <div class="col-xs-12 col-sm-12 col-md-12">
  21. <div class="form-group">
  22. <strong>Email:</strong>
  23. {{ $member->email}}
  24. </div>
  25. </div>
  26. </div>
  27. @endsection
resources/views/members/form.blade.php
  1. <div class="row">
  2. <div class="col-xs-12 col-sm-12 col-md-12">
  3. <div class="form-group">
  4. <strong>Name:</strong>
  5. {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
  6. </div>
  7. </div>
  8. <div class="col-xs-12 col-sm-12 col-md-12">
  9. <div class="form-group">
  10. <strong>Email:</strong>
  11. {!! Form::email('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
  12. </div>
  13. </div>
  14. <div class="col-xs-12 col-sm-12 col-md-12 text-center">
  15. <button type="submit" class="btn btn-primary">Submit</button>
  16. </div>
  17. </div>
resources/views/members/create.blade.php
  1. @extends('layouts.default')
  2. @section('content')
  3. <div class="row">
  4. <div class="col-lg-12 margin-tb">
  5. <div class="pull-left">
  6. <h2>Add New Member</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-primary" href="{{ route('members.index') }}"> Back</a>
  10. </div>
  11. </div>
  12. </div>
  13. @if (count($errors) > 0)
  14. <div class="alert alert-danger">
  15. <strong>Whoops!</strong> There were some problems with your input.<br><br>
  16. <ul>
  17. @foreach ($errors->all() as $error)
  18. <li>{{ $error }}</li>
  19. @endforeach
  20. </ul>
  21. </div>
  22. @endif
  23. {!! Form::open(array('route' => 'members.store','method'=>'POST')) !!}
  24. @include('members.form')
  25. {!! Form::close() !!}
  26. @endsection
resources/views/members/edit.blade.php
  1. @extends('layouts.default')
  2. @section('content')
  3. <div class="row">
  4. <div class="col-lg-12 margin-tb">
  5. <div class="pull-left">
  6. <h2>Edit Member</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-primary" href="{{ route('members.index') }}"> Back</a>
  10. </div>
  11. </div>
  12. </div>
  13. @if (count($errors) > 0)
  14. <div class="alert alert-danger">
  15. <strong>Whoops!</strong> There were some problems with your input.<br><br>
  16. <ul>
  17. @foreach ($errors->all() as $error)
  18. <li>{{ $error }}</li>
  19. @endforeach
  20. </ul>
  21. </div>
  22. @endif
  23. {!! Form::model($member, ['method' => 'PATCH','route' => ['members.update', $member->id]]) !!}
  24. @include('members.form')
  25. {!! Form::close() !!}
  26. @endsection

Now you can perform the crud application after following above steps.

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort serdivan escort sapanca escort karasu escort hendek escort ankara escort
sakarya escort kızlar sakarya escort kaliteli sakarya escort vip sakarya escort elit sakarya escort bayan sakarya escort adresleri.