CRUD OPERATIONS WITH LARAVEL 5.7



Laravel is an open-source php framework. Laravel 5.7 stable release was on September 4 2018. As we all know laravel needs composer.

1   Install Laravel 5.7

The below command will install laravel
            Composer create-project --prefer-dist laravel/laravel crud

2.      Setup Database Configuration

            We can setup Database configuration in .env file.

3.      Create Table

     php artisan make:migration create_products_table --create=profile
     
       Now go to path “database/migrations” and we can change the migration file of profile table

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('profiles');
    }
}

Now run the below command
            php artisan migrate

4.      Creating Controller

Now we can create controller and model. For creating the controller and model we need to run the below command
php artisan make:controller ProfileController --resource --model=Model/Profile

Now we can see controller is created on “app/Http/Controllers/”

5.      Creating Routes

We can add the routes inside the web.php file of  routes folder.
            Route::resource('profile','ProfileController');
6.      
       Create default methods in product controller
a.       Index()
b.      Create()
c.       Store()
d.      Show()
e.       Edit()
f.       Update()
g.      Destroy()

Above methods are the default methods in ProfileController
7.      Now we can write the code for CRUD operation
<?php
 
namespace App\Http\Controllers;
use App\Model\Profile;
use Illuminate\Http\Request;
 
class ProfileController extends Controller
{
    public function index()
    {
        $profiles = Profile::latest()->paginate(5);
 
        return view('profiles.index',compact('profiles'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

    public function create()
    {
        return view('profiles.create');
    }
 
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required',
        ]);
 
        Profile::create($request->all());
  
        return redirect()->route('profiles.index')
                        ->with('success','Profile created successfully.');
    }

    public function show(Profile $Profile)
    {
        return view('profiles.show',compact('Profile'));
    }

    public function edit(Profile $Profile)
    {
        return view('profiles.edit',compact('Profile'));
    }
 
    public function update(Request $request, Profile $Profile)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required',
        ]);
 
        $Profile->update($request->all());
 
        return redirect()->route('profiles.index')
                        ->with('success','Profile updated successfully');
    }

    public function destroy(Profile $Profile)
    {
        $Profile->delete();
 
        return redirect()->route('profiles.index')
                        ->with('success','Profile deleted successfully');
    }
}

8.      Now we can go to app/Model/

<?php
 
namespace App;
use Illuminate\Database\Eloquent\Model;
  
class Profile extends Model
{
    protected $fillable = [
        'name', 'email'
    ];
}
9
.      Now create blade files
We have 5 blade files
a.       Layout.blade.php
b.      Index.blade.php
c.       Create.blade.php
d.      Edit.blade.php
e.       Show.blade.php

Layout.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
 
<div class="container">
    @yield('content')
</div>
  
</body>
</html>

Index.php
@extends('profile.layout')

@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Welcome</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('Profiles.create') }}"> Create New Profile</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>Slno</th>
            <th>Name</th>
            <th>Email</th>
            <th width="280px">Action</th>
        </tr>
        @foreach ($Profiles as $Profile)
        <tr>
            <td>{{ ++$i }}</td>
            <td>{{ $Profile->name }}</td>
            <td>{{ $Profile->email }}</td>
            <td>
                <form action="{{ route('Profiles.destroy',$Profile->id) }}" method="POST">
                    <a class="btn btn-info" href="{{ route('Profiles.show',$Profile->id) }}">Show</a>
                    <a class="btn btn-primary" href="{{ route('Profiles.edit',$Profile->id) }}">Edit</a>
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </table>
 
    {!! $Profiles->links() !!}
@endsection

Create.blade.php

@extends('profiles.layout')
 
@section('content')
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Add New Profile</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-primary" href="{{ route('profiles.index') }}"> Back</a>
        </div>
    </div>
</div>
  
@if ($errors->any())
    <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 action="{{ route('profiles.store') }}" method="POST">
    @csrf
     <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Name:</strong>
                <input type="text" name="name" class="form-control" placeholder="Name">
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Detail:</strong>
                <input type="email" name="email" class="form-control" placeholder="email">
            </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>
  
</form>
@endsection

Edit.blade.php

@extends('profiles.layout')
  
@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Edit Profile</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('profiles.index') }}"> Back</a>
            </div>
        </div>
    </div>
  
    @if ($errors->any())
        <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 action="{{ route('profiles.update',$profile->id) }}" method="POST">
        @csrf
        @method('PUT')
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Name:</strong>
                    <input type="text" name="name" value="{{ $profile->name }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Detail:</strong>
                    <input type="email" name="email" value="{{ $profile->email }}" class="form-control" placeholder="email">
                </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>
    </form>
@endsection

Show.blade.php

@extends('profiles.layout')

@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2> Show Profile</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('profiles.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>
                {{ $profile->name }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Details:</strong>
                {{ $profile->email }}
            </div>
        </div>
    </div>
@endsection

10.  Now run the below command

php artisan serve

The above command will generate the local server
            http://localhost:8000/profiles

Finally we complete the crud operation with laravel 5.7. In laravel 5.7 we a lot of updations in features such as laravel nova, email verification, guest user gates/policies are the some of the new features in laravel 5.7. Laravel is powerful PHP mvc framework. Laravel is updating very fast compared to other framework. We check go through the updates of laravel 5.7 later.

Comments