Laravel continuously releasing it’s new versions with stable features and advanced life time support which is well maintained and equally balanced to get the job done. Definitely Laravel gonna be the big advantage if you guys planning to hit the big projects. While wondering upon these advanced features of Laravel, today am planning to discuss about the CRUD operation with Laravel 5.7 in detail. Before getting into it, you can also have a quick look on my recent blogs on Laravel, hope that could you to understand the laravel better.
Generate Charts And Graphs Using Laravel
A Guide To Laravel Eloquent Relationships – Part 1
Laravel Eloquent Relationships – Part 2
Install Laravel 5.7
Run the below command to install laravel,
Composer create-project --prefer-dist laravel/laravel crud
Setup Database Configuration
You can do all the database setup configuration on .env file.
Create Table
Comment to create table,
php artisan make:migration create_products_table --create=profile
Go to path “database/migrations” and here you 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'); } }
Use the below command to migrate.
php artisan migrate
Creating Controller
Run the below command to create a controller and model,
php artisan make:controller ProfileController --resource --model=Model/Profile
You can see the created controller on “app/Http/Controllers/”
Creating Routes
Run the following command to add the routes inside the web.php file of routes folder.
Route::resource('profile','ProfileController');
Create Default Methods In Product Controller
Index()
Create()
Store()
Show()
Edit()
Update()
Destroy()
Above listed methods are the default methods in ProfileController
Write The Code For CRUD Operation
Here you can see the entire process of 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'); } }
Creating blade files In Laravel 5.7
We have 5 blade files,
- Layout.blade.php
- Index.blade.php
- Create.blade.php
- Edit.blade.php
- 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.blade.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
Run the below command to run the local server
php artisan serve
Now you can see my local server successfully running here http://localhost:8000/profiles
We almost covered all the crud operation with laravel 5.7. In laravel 5.7 also has some cool features like laravel nova, email verification, guest user gates/policies are the some of the new features in laravel 5.7. Parallely, Laravel is getting updated faster than the other frameworks so will continue to look more about the updates of laravel 5.7 and the upcoming features of Laravel later. If you have any queries related to this, then the comment box is open for you! you can post your queries will surely help you out!