Laravel 5.2 CRUD (Create Read Update Delete) Example from Scratch

Laravel 5.2 CRUD (Create Read Update Delete) Example from Scratch

Laravel 5.2 CRUD (Create Read Update Delete) Example from Scratch

I am going to explain how to create a single module with Laravel CRUD (Create, Read, Update, Delete) application.Kindly follow each step to create a simple CRUD application.I assure you, if you follow these steps properly then you won't get any error.In this CRUD application, you can add new product, edit those products, view product details and delete product.

Before going to start, kindly let you know following things :Step 1: Install Laravel 5.2

If Laravel is not installed in your system then first install with following command and get fresh Laravel project.

composer create-project --prefer-dist laravel/laravel blog "5.2.*"

When you run above command then it create application with name blog in your system.From Laravel 5, you have to install laravelcollective/html for Form class.To know the installation process of laravelcollective/html, kindly go through this url HTML/FORM not found in Laravel 5?.

Step 2: Create products table and model

Follow the simple step to create products table in your database.First create migration for products table using Laravel 5 php artisan command,so first run this command -

php artisan make:migration create_products_table
After this command, you will see a migration file in following path database/migrations and you have to simply put following code in migration file to create products table.

  1. use Illuminate\Database\Schema\Blueprint;
  2. use Illuminate\Database\Migrations\Migration;
  3. class CreateProductsTable extends Migration
  4. {
  5. public function up()
  6. {
  7. Schema::create('products', function (Blueprint $table) {
  8. $table->increments('id');
  9. $table->string('name');
  10. $table->text('details');
  11. $table->timestamps();
  12. });
  13. }
  14. public function down()
  15. {
  16. Schema::drop("products");
  17. }
  18. }

Save this migration file and run following command

php artisan migrate

After create `products` table, yor should create model for product table.Create file in following path app/Product.php and put bellow couple of code in Product.php file:

app/Product.php

  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class Product extends Model
  4. {
  5. public $fillable = ['name','details'];
  6. }
Step 3: Add Route and Controller

To handling request, you need to create route for products CRUD, so just add resource route in your routes file.I added resource route because it will add index, show, create, show and delete routes automatically.So put bellow line of code in your route file.

app/Http/routes.php

  1. Route::resource('productCRUD','ProductCRUDController');

Now we will create ProductCRUDController in following path app/Http/Controllers, all routes will manage by this ProductCRUDController.php file.

app/Http/Controllers/ProductCRUDController.php

  1. namespace App\Http\Controllers;
  2. use Illuminate\Http\Request;
  3. use App\Http\Controllers\Controller;
  4. use App\Product;
  5. class ProductCRUDController extends Controller
  6. {
  7. /**
  8. * Display a listing of the resource.
  9. *
  10. * @return \Illuminate\Http\Response
  11. */
  12. public function index(Request $request)
  13. {
  14. $products= Product::orderBy('id','DESC')->paginate(5);
  15. return view('ProductCRUD.index',compact('products'))
  16. ->with('i', ($request->input('page', 1) - 1) * 5);
  17. }
  18. /**
  19. * Show the form for creating a new resource.
  20. *
  21. * @return \Illuminate\Http\Response
  22. */
  23. public function create()
  24. {
  25. return view('ProductCRUD.create');
  26. }
  27. /**
  28. * Store a newly created resource in storage.
  29. *
  30. * @param \Illuminate\Http\Request $request
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function store(Request $request)
  34. {
  35. $this->validate($request, [
  36. 'name' => 'required',
  37. 'details' => 'required',
  38. ]);
  39. Product::create($request->all());
  40. return redirect()->route('productCRUD.index')
  41. ->with('success','Product created successfully');
  42. }
  43. /**
  44. * Display the specified resource.
  45. *
  46. * @param int $id
  47. * @return \Illuminate\Http\Response
  48. */
  49. public function show($id)
  50. {
  51. $product= Product::find($id);
  52. return view('ProductCRUD.show',compact('product'));
  53. }
  54. /**
  55. * Show the form for editing the specified resource.
  56. *
  57. * @param int $id
  58. * @return \Illuminate\Http\Response
  59. */
  60. public function edit($id)
  61. {
  62. $product= Product::find($id);
  63. return view('ProductCRUD.edit',compact('product'));
  64. }
  65. /**
  66. * Update the specified resource in storage.
  67. *
  68. * @param \Illuminate\Http\Request $request
  69. * @param int $id
  70. * @return \Illuminate\Http\Response
  71. */
  72. public function update(Request $request, $id)
  73. {
  74. $this->validate($request, [
  75. 'name' => 'required',
  76. 'details' => 'required',
  77. ]);
  78. Product::find($id)->update($request->all());
  79. return redirect()->route('productCRUD.index')
  80. ->with('success','Product updated successfully');
  81. }
  82. /**
  83. * Remove the specified resource from storage.
  84. *
  85. * @param int $id
  86. * @return \Illuminate\Http\Response
  87. */
  88. public function destroy($id)
  89. {
  90. Product::find($id)->delete();
  91. return redirect()->route('productCRUD.index')
  92. ->with('success','Product deleted successfully');
  93. }
  94. }
Step 4: Create Blade File

Now we will create blade file for listing, create, edit, show, form, default (which is master template).According to standard structure of Laravel, we create new layouts folder/directory in following path resources/views and create default.blade.php within that folderresources/views/layouts/.

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 CRUD</title>
  8. <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
  9. </head>
  10. <body>
  11. <div class="container">
  12. @yield('content')
  13. </div>
  14. </body>
  15. </html>

Now we will create layout for product listing, for this we will create seperate directory name ProductCRUD which contain files related products functionality and create index.blade.php file within resources/views/ProductCRUD/.

resources/views/ProductCRUD/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>Products CRUD</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-success" href="{{ route('productCRUD.create') }}"> Create New Product</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>Details</th>
  23. <th width="280px">Action</th>
  24. </tr>
  25. @foreach ($products as $product)
  26. <tr>
  27. <td>{{ ++$i }}</td>
  28. <td>{{ $product->name}}</td>
  29. <td>{{ $product->details}}</td>
  30. <td>
  31. <a class="btn btn-info" href="{{ route('productCRUD.show',$product->id) }}">Show</a>
  32. <a class="btn btn-primary" href="{{ route('productCRUD.edit',$product->id) }}">Edit</a>
  33. {!! Form::open(['method' => 'DELETE','route' => ['productCRUD.destroy', $product->id],'style'=>'display:inline']) !!}
  34. {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
  35. {!! Form::close() !!}
  36. </td>
  37. </tr>
  38. @endforeach
  39. </table>
  40. {!! $products->render() !!}
  41. @endsection

Now we create new blade file to add new product within ProductCRUD folder

resources/views/ProductCRUD/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 Product</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-primary" href="{{ route('productCRUD.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' => 'productCRUD.store','method'=>'POST')) !!}
  24. @include('ProductCRUD.form')
  25. {!! Form::close() !!}
  26. @endsection

Now we create new blade file to edit product within ProductCRUD folder

resources/views/ProductCRUD/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 Product</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-primary" href="{{ route('productCRUD.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($product, ['method' => 'PATCH','route' => ['productCRUD.update', $product->id]]) !!}
  24. @include('ProductCRUD.form')
  25. {!! Form::close() !!}
  26. @endsection

Now we create a new blade file form.blade.php within ProductCRUD folder

resources/views/ProductCRUD/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>Details:</strong>
  11. {!! Form::textarea('details', null, array('placeholder' => 'Details','class' => 'form-control','style'=>'height:100px')) !!}
  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>

At Last we create a blade file to show the product details

resources/views/ProductCRUD/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 Product</h2>
  7. </div>
  8. <div class="pull-right">
  9. <a class="btn btn-primary" href="{{ route('productCRUD.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. {{ $product->name}}
  18. </div>
  19. </div>
  20. <div class="col-xs-12 col-sm-12 col-md-12">
  21. <div class="form-group">
  22. <strong>Details:</strong>
  23. {{ $product->details}}
  24. </div>
  25. </div>
  26. </div>
  27. @endsection
Now you can build your first CRUD Application..

Phone: (+91) 8800417876
Noida, 201301
Deneme bonusu veren siteler Bahis forum Bahis forumu
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
bedava bahis
atasehir escort anadolu yakasi escort ankara 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

izmir escort
ataşehir escort