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
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
antalya escort akseki escort aksu escort alanya escort bayan antalya escort antalya grup escort antalya masöz antalya rus escort antalya sınırsız escort antalya travesti escort bdsm escort beldibi escort bayan belek escort bayan çallı escort bayan çamyuva escort bayan demre escort bayan elmalı escort bayan fethiye escort bayan finike escort bayan göynük escort bayan güllük escort bayan ışıklar escort bayan kaş escort bayan kemer escort bayan konyaaltı altınkum escort bayan konyaaltı arapsuyu escort bayan konyaaltı escort bayan konyaaltı liman escort korkuteli escort bayan kültür escort kundu escort bayan lara escort bayan manavgat escort bayan muratpaşa escort bayan örnekköy escort bayan rus escort bayan serik escort bayan side escort bayan suriyeli escort tekirova escort bayan üniversiteli escort alanya escort mahmutlar escort alanya escort mahmutlar escort alanya escort mahmutlar escort istanbul escort avrupa yakası escort şişli escort topkapı escort istanbul vip escort maslak escort merter escort aksaray escort arnavutköy escort ataköy escort bağcılar escort bahçelievler escort bakırköy escort beşitaş escort beylikdüzü escort beyoğlu escort halkalı escort istanbul sınırsız escort kağıthane escort mecidiyeköy escort nişantaşı escort escort istanbul ataşehir escort beykoz escort çekmeköy escort kadıköy escort maltepe escort pendik escort sancaktepe escort şile escort sultanbeyli escort tuzla escort ümraniye escort üsküdar escort
beylikdüzü escort istanbul escort şişli escort beylikdüzü escort istanbul escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort alanya escort manavgat escort bursa escort eskişehir escort sakarya escort samsun escort malatya escort trabzon escort adıyaman escort afyon escort ağrı escort aksaray escort amasya escort ardahan escort artvin escort aydın escort balikesir escort bartın escort batman escort bayburt escort bilecik escort bingöl escort bitlis escort bolu escort burdur escort çanakkale escort çankırı escort çorum escort düzce escort edirne escort elazığ escort erzincan escort erzurum escort giresun escort gümüşhane escort hatay escort ığdır escort ısparta escort maraş escort karabük escort karaman escort kars escort kastamonu escort kilis escort kırıkkale escort kırklareli escort kırşehir escort kütahya escort manisa escort mardin escort muğla escort muş escort nevşehir escort niğde escort ordu escort osmaniye escort rize escort urfa escort siirt escort sinop escort şırnak escort sivas escort ordu escort tokat escort tunceli escort usak escort van escort yalova escort yozgat escort zonguldak escort kıbrıs escort
adana escort ankara escort bursa escort diyarbakir escort eskisehir escort kayseri escort kocaeli escort konya escort malatya escort maras escort mersin escort ordu escort sakarya escort samsun escort hatay escort mardin escort tekirdag escort antalya escort aydin escort nigde escort nevsehir escort elazig escort manisa escort osmaniye escort denizli escort mugla escort
bedava bahis
tahmin
ankara escort - ankara escort
ataşehir escort ataşehir anadolu yakası escort ankara escort antalya escort antalya escort

flynta 5 mg

flynta 5 mg fiyat

orcafil 5 mg

lifta

degra

sildegra

combo 100 mg

degra 100 mg

vigrande

sinegra

jeligra

lifta 20

vigrande 100 mg

lifta 20 mg

lifta 5 mg

sildegra 100 mg

vigaroo

flynta 20 mg

viagra 100 mg fiyat

orijinal viagra

viagra 100 mg

eczane viagra

viagra sipariş

viagra satın al

viagra nedir

viagra yorum

viagra fiyat

viagra fiyatı 2021

cialis

cialis fiyat

cialis 20 mg

cialis 100 mg

cialis nedir

cialis 5 mg fiyatı

cialis hap

cialis 5 mg

cialis 20 mg eczane fiyatı

cialis 100 mg fiyat

cialis satış

izmir escort
ataşehir escort
pendik aktar