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
Bahis forum Bahis forumu Deneme bonusu
gaziantep escort title="gaziantep escort bayan">gaziantep escort bayan gaziantep escort
esenyurt escort
ataşehir escort maltepe escort escort bayan anadolu yakası escort kaliteli escort anadolu yakası escort çekmeköy escort ümraniye escort kurtköy escort anadolu yakası escort escort bayan bostancı escort ataşehir escort ümraniye 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
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 etlik escort çankaya escort kızılay escort demetevler escort dikmen escort esat escort eryaman escort ankara escort bayan
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 adapazarı escort afyon escort aksaray escort alanya escort aliağa escort amasya escort antalya escort ataşehir escort aydın escort balıkesir escort bartın escort batman escort bayraklı escort bilecik escort bodrum escort bolu escort bornova escort buca escort burdur escort bursa escort çanakkale escort çankırı escort çorum escort darıca escort didim escort edirne escort edremit escort elazığ escort konya escort eryaman escort kıbrıs escort eskişehir escort fethiye escort giresun escort gümüşhane escort güngören escort hatay escort iskenderun escort ısparta escort gaziemir escort karabük escort karaman escort karşıyaka escort kaş escort kastamonu escort kayseri escort kırıkkale escort kırklareli escort kırşehir escort kocaeli escort kuşadası escort kütahya escort lara escort malatya escort manisa escort maraş escort mersin escort muğla escort nevşehir escort niğde escort ödemiş escort ordu escort osmaniye escort pendik escort sakarya escort atakum escort samsun escort sincan escort sinop escort sivas escort tekirdağ escort tokat escort trabzon escort tuzla escort urfa escort uşak escort düzce escort yalova escort yozgat escort zonguldak escort dörtyol escort antakya escort yüreğir escort bandırma escort gürsu escort marmaris escort ereğli escort çorlu escort afşin escort gemlik escort kemer escort karatay escort selçuklu escort beyşehir escort bosna escort ceyhan escort nazilli escort talas escort yıldırım escort

viagra sipariş

lifta 20 mg

cialis fiyat

viagra 100 mg fiyat

cialis jel

viagra satın al

cialis 5 mg fiyat

viagra satın al

viagra fiyatı

degra

vigrande

degra 100 mg

orcafil

cialis nedir

cialis 20 mg eczane fiyatı

cialis fiyat

cialis 20 mg

cialis 100 mg

viagra fiyat

lifta

lifta 5 mg

hardcis

cialis eczane

cialis 5 mg fiyatı

cialis 100 mg fiyat

cialis 5 mg

viagra fiyatları

viagra satış

online eczane viagra

viagra eczane

cialis 20 mg

cialis 100 mg

cialis hap

cialis 20

cialis eczane

cialis satış

cialis fiyatları

cialis fiyatı

viagra fiyat

viagra fiyatları

viagra eczane

viagra satın al

pfizer viagra satış

online eczane viagra

pfizer viagra satın al

viagra sipariş

viagra sipariş hattı

viagra fiyat 2022

online viagra sipariş

viagra fiyat 2022 eczane

viagra fiyat eczane

cialis 5 mg

kamagra satın al

kamagra

kamagra jel

kamagra jel sipariş

kamagra sipariş

kamagra fiyat

kamagra jel fiyatı

kamagra fiyatı

kamagra 100mg

kamagra 100mg fiyat

süper kamagra

kamagra nedir

Ereksiyon Hapları

orcafil 5 mg

flynta 5 mg

lifta 5 mg

flynta 20 mg

lifta 20 mg

degra fiyat

degra 100 mg

sildegra 100 mg

sildegra

sinegra

vigrande

vigrande 100 mg

combo 100 mg

vigaroo

jeligra

cialis satış

cialis 100 mg fiyat

cialis 20 mg eczane fiyatı

cialis 5 mg

cialis hap

cialis 5 mg fiyatı

cialis nedir

cialis 100 mg

cialis 20 mg

cialis fiyat

cialis

viagra fiyatı 2021

viagra fiyat

viagra yorum

viagra nedir

viagra satın al

viagra sipariş

eczane viagra

viagra 100 mg

orijinal viagra

viagra 100 mg fiyat

novagra

geciktirici krem

geciktirici sprey

viga krem

viga sprey

nely8

stag sprey

delay sprey

geciktirici mendil

geciktirici

viagra fiyat viagra fiyatları cialis 20 mg cialis 100 mg kamagra kamagra jel vega 100 vega 100 mg viagra cialis
iskenderun escort, iskenderun escort, iskenderun escort, hatay escort, iskenderun escort, iskenderun escort, iskenderun escort, iskenderun escort, iskenderun escort, iskenderun escort, iskenderun escort, iskenderun escort, antakya escort, antakya escort, bursa escort, bursa escort, hatay escort,
diyarbakır escort, diyarbakır escort, elazığ escort, elazığ escort, arsuz escort, arsuz escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort, denizli escort,
adana escort, adana escort, adana escort, adana escort, adana escort, malatya escort, malatya escort, eskişehir escort, eskişehir escort, manisa escort, izmit escort, izmit escort, izmit escort, izmit escort, izmit escort, izmit escort, izmit escort, izmit escort, izmit escort, ısparta escort, ankara escort, ankara escort, gaziantep escort, gaziantep escort, gaziantep escort, seks hikayeleri, erotik hikayeleri, erotik seks hikayeleri, sakarya escort, sakarya escort, sakarya escort,
izmir escort
ataşehir escort