Today, We want to share with you Laravel 5.8 CRUD Example Tutorial For Beginners From Scratch.In this post we will show you insert update delete with laravel 5.8, hear for Laravel 5.8 DataTables Ajax Crud Tutorial we will give you demo and example for implement.In this post, we will learn about Laravel CRUD Operations Tutorial step by step in version 5.8 with an example.
Laravel 5.8 CRUD Example Tutorial For Beginners From Scratch
There are the Following The simple About Laravel 5.8 CRUD Example Tutorial For Beginners From Scratch Full Information With Example and source code.
As I will cover this Post with live Working example to develop Laravel 5.8 CRUD (Create Read Update Delete) Tutorial For Beginners, so the laravel 5.8 crud operation step by step for this example is following below.
Step 1 : Fresh Install Laravel 5.8
composer create-project --prefer-dist laravel/laravel atmiya25
Step 2: Change Laravel .env Database Configuration
.env files
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Eneter your secure database name(atmiya25) DB_USERNAME=Eneter secure database username([email protected]#$%^) DB_PASSWORD=Eneter secure database password([email protected]#$%^)
Step 3: Create Table
Make a Database migration for “members” table using Laravel 5.8 php artisan command
php artisan make:migration create_members_table --create=members
File Path : “database/migrations”
<?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->text('detail'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('members'); } }
Step 4: Define a Laravel 5.8 Resource Route
routes/web.php
Route::resource('members','MemberController');
Step 5: Create Laravel 5.8 Controller and Model
Laravel 5.8 create new controller as MemberController
php artisan make:controller MemberController --resource --model=Member
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'); } }
app/Member.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Member extends Model { protected $fillable = [ 'name', 'detail' ]; }
Step 6: Create Laravel 5.8 Blade Files
resources/views/members/layout.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 5.8 CRUD Application - pakainfo.com</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>
resources/views/members/index.blade.php
@extends('members.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Laravel 5.8 CRUD Example from scratch - ItSolutionStuff.com</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>Details</th> <th width="280px">Action</th> </tr> @foreach ($members as $member) <tr> <td>{{ ++$i }}</td> <td>{{ $member->name }}</td> <td>{{ $member->detail }}</td> <td> <form action="{{ route('members.destroy',$member->id) }}" method="POST"> <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> @csrf @method('DELETE') <button type="submit" class="btn btn-danger">Delete</button> </form> </td> </tr> @endforeach </table> {!! $members->links() !!} @endsection
resources/views/members/create.blade.php
@extends('members.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 ($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('members.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> <textarea class="form-control" style="height:250px" name="detail" placeholder="Detail"></textarea> </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
resources/views/members/edit.blade.php
@extends('members.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 ($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('members.update',$member->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="{{ $member->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> <textarea class="form-control" style="height:250px" name="detail" placeholder="Detail">{{ $member->detail }}</textarea> </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
resources/views/members/show.blade.php
@extends('members.layout') @section('content') <div class="pakainfo row"> <div class="pakainfo col-lg-12 margin-tb"> <div class="pakainfo pull-left"> <h2> Show Member</h2> </div> <div class="pakainfo pull-right"> <a class="pakainfo btn btn-primary" href="{{ route('members.index') }}"> Back</a> </div> </div> </div> <div class="pakainfo row"> <div class="pakainfo col-xs-12 col-sm-12 col-md-12"> <div class="pakainfo form-group"> <strong>Name:</strong> {{ $member->name }} </div> </div> <div class="pakainfo col-xs-12 col-sm-12 col-md-12"> <div class="pakainfo form-group"> <strong>Details:</strong> {{ $member->detail }} </div> </div> </div> @endsection
Angular 6 CRUD Operations Application Tutorials
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about Laravel 5.8 CRUD Example Tutorial For Beginners From Scratch.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.