Laravel 5.7 CRUD (Create Read Update Delete) Tutorial Example

Laravel 5.7 CRUD (Create Read Update Delete) Tutorial Example

In this Laravel 5.7 tutorial, I will let you know how to create basic CRUD functionality.

Laravel 5.7 has some other enhancement with awesome features.

In this example, You will get step by step guide that will help you to build a CRUD application easily.

This example will have following functionality :

  • How to install Laravel 5.7 application
  • How to configure MySQL database
  • How to work on migration file to create tables
  • How to use resource route
  • How to create Controller and Model
  • How to create Blade file
Step 1 : Install Laravel 5.7

In this first step, I will install the fresh Laravel 5.7 application by running following command :

composer create-project --prefer-dist laravel/laravel blog
Step 2 : Configure MySQl Database

In this step, I will create a database first in MySQL and connect database with Laravel application.

We will use environment variables for database credential. So let's open .env file :

.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dbName
DB_USERNAME=dbUsername
DB_PASSWORD=dbPassword
Step 3: Create migration file to generate table

In this step, I will create a migration file for "members" table so that we can work with CRUD functionality in Laravel 5.7

command:
php artisan make:migration create_members_table --create=members

It will create a migration file in "database/migrations" directory.

Now I will update the below schema inside the file timestamp_create_members_table.php

<?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');
    }
}

Now I will run the migration command to create table :

php artisan migrate
Step 4: Create model using artisan command

In this step, I will create Member model by running artisan command:

php artisan make:model Member

Now I will open the app/Member.php file that is generated by above command and specify the fillable attribute for mass assignment.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Member extends Model
{
    protected $fillable = [
        'name', 'email'
    ];
}
Step 5 : Add route

In this step, I will add resource route to display member list, view member details, display add member form, store member information, display edit member form, update member information and delete member information.

routes/web.php
Route::resource('members','MemberController');
Step 6: Create Controller

In this step, I will create a new resource controller by running artisan command :

php artisan make:controller MemberController --resource

Okay, now open MemberController.php file from the directory app/Http/Controllers/.

This MemberController has seven following methods to handle CRUD operation.

  1. index() - will list member data from the database.
  2. create() - will display the add member form.
  3. store() - will insert the member information into the members table.
  4. show() - will display the member information.
  5. edit() - will display the edit member form.
  6. update() - will update the member information.
  7. destroy() - will delete the member data from the database.
app/Http/Controllers/MemberController.php
<?php
  
namespace App\Http\Controllers;
  
use App\Member;
use Illuminate\Http\Request;
  
class MemberController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $members = Member::latest()->paginate(5);
  
        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',
            'detail' => 'required',
        ]);
  
        Member::create($request->all());
   
        return redirect()->route('members.index')
                        ->with('success','Member created successfully.');
    }
   
    /**
     * Display the specified resource.
     *
     * @param  \App\Member  $member
     * @return \Illuminate\Http\Response
     */
    public function show(Member $member)
    {
        return view('members.show',compact('member'));
    }
   
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Member  $member
     * @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  \App\Member  $member
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Member $member)
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);
  
        $member->update($request->all());
  
        return redirect()->route('members.index')
                        ->with('success','Member updated successfully');
    }
  
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Member  $member
     * @return \Illuminate\Http\Response
     */
    public function destroy(Member $member)
    {
        $member->delete();
  
        return redirect()->route('members.index')
                        ->with('success','Member deleted successfully');
    }
}
Step 7: Create Blade Files

In this step, I will create master template and some blade file related to member management.

  1. layout.blade.php
  2. index.blade.php
  3. create.blade.php
  4. edit.blade.php
  5. show.blade.php

Okay, now I will create master template inside the views folder.

resources/views/layout.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Build CRUD Application in Laravel 5.7 - ExpertPHP.in </title>
    <link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
</head>
<body>
  
<div class="container">
    @yield('content')
</div>
   
</body>
</html>

Now create members folder inside resources >> views and create all files inside that members folder.

resources/views/members/index.blade.php
@extends('layout')
@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
resources/views/members/form.blade.php

I will create a common form that will use in create and edit module.

<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>
resources/views/members/create.blade.php
@extends('layout')
@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
resources/views/members/edit.blade.php
@extends('layout')
 
@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
resources/views/members/show.blade.php
@extends('layout')

@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

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