laravel 5.8 crud tutorial, laravel 5.8 crud example, laravel 5.8 crud generator, CRUD Operations Laravel 5.8 With Query Builder, Laravel 5.8 CRUD Operation with Example,Create Read Update Delete CRUD Operations Laravel 5.8 Query Builder with Example
Hello freinds Today, I will tell you laravel 5.8 crud operation that you can create, read, update and delete how you build with Query Builder in laravel 5.8.
I will tell you a few steps below which the step is to make us crud operation in laravel 5.8.
You can insert update and delete the crud operation data from 2 types in laravel 5.8.
1.Laravel 5.8 CRUD (Create Read Update Delete) Generator For Beginners With Example
2.Create Read Update Delete CRUD Operations Laravel 5.8 Query Builder with Example
New Version : – Laravel 6.0 CRUD Tutorial Application
Type 1 :- Laravel 5.8 CRUD (Create Read Update Delete) Generator For Beginners With Example
Hello Freinds Today, I will tell you laravel 5.8 crud operation that you can create, read, update and delete how you build in laravel 5.8.
I will tell you a few steps below which the step is to make us crud operation in laravel 5.8.
Overview
Step 1: Laravel 5.8 Install
Step 2: Connect Database Configuration
Step 3: Create Table in Laravel 5.8
Step 4: Create Resource Route in Laravel 5.8
Step 5: Create Model
Step 6: Create Controller
Step 7: Create Blade Files
Step 1: Laravel 5.8 Install
The first step is to install laravel 5.8. To install laravel 5.8 we first have to install the composer. If you have already installed the composer in your system or laptop. There is no need to install a composer.
composer create-project --prefer-dist laravel/laravel blog
Step 2: Connect Database Configuration
In this step, I will tell you how to do database configuration in Laravel 5.8. For database configuration, you will need a database name, username, password.
We change the .env file for the database configuration
.env
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Database Name DB_USERNAME=User Name (root) DB_PASSWORD=Password
Step 3: Create Table in Laravel 5.8
This is the third step. In this step you will be told how to create a table in laravel 5.8, it is very easy to create table in laravel 5.8. The table will create.
php artisan make:migration create_blogs_table --create=blogs
database/migrations
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateBlogsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('blogs', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('content'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('blogs'); } }
after that Now you have to run this migration by following command:
php artisan migrate
Step 4: Create Resource Route in Laravel 5.8
routes/web.php
Route::resource('blogs','blogController');
Step 5: Create Model
Then after that we will create a database table model. To create a model we can create a model by using the laravel command. The systax to create the model is given below.
php artisan make:model Blog
app/Blog.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Blog extends Model { protected $fillable = [ 'title', 'content' ]; }
Step 6: Create Controller
Then we will create the controller. Through this controller we will Create, Red, Update, Delete the data.So let’s Create data into laravel 5.8.Learn through this controller.
Syntax Create for Controller
php artisan make:controller blogController --resource
After bellow command you will find new file in this path app/Http/Controllers/blogController.php.
1.index()
2.create()
3.store()
4.show()
5.edit()
6.update()
7.destroy()
So, let’s copy code and paste on blogController.php file.
app/Http/Controllers/blogController.php
<?php namespace App\Http\Controllers; use App\Blog; use Illuminate\Http\Request; class blogController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $blogs = Blog::orderby('id', 'desc')->get(); return view('blogs.index',compact('blogs')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('blogs.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $request->validate([ 'title' => 'required', 'content' => 'required', ]); Blog::create($request->all()); return redirect()->route('blogs.index') ->with('success','blogs created successfully.'); } /** * Display the specified resource. * * @param \App\Blog $blog * @return \Illuminate\Http\Response */ public function show(Blog $blog) { return view('blogs.show',compact('blog')); } /** * Show the form for editing the specified resource. * * @param \App\Blog $blog * @return \Illuminate\Http\Response */ public function edit(Blog $blog) { return view('blogs.edit',compact('blog')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Blog $blog * @return \Illuminate\Http\Response */ public function update(Request $request, Blog $blog) { $request->validate([ 'title' => 'required', 'content' => 'required', ]); $blog->update($request->all()); return redirect()->route('blogs.index') ->with('success','blogs updated successfully'); } /** * Remove the specified resource from storage. * * @param \App\Blog $blog * @return \Illuminate\Http\Response */ public function destroy(Blog $blog) { $blog->delete(); return redirect()->route('blogs.index') ->with('success','Blog deleted successfully'); } }
Step 7: Create Blade Files
1. layout.blade.php
2. index.blade.php
3. create.blade.php
4. edit.blade.php
5. show.blade.php
So let’s just create following file and paste bellow code.
resources/views/blogs/layout.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 5.8 CRUD Generator Application</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/blogs/index.blade.php
laravel 5.8 We were talking about crud tutorial. This is the second step. In this index page you will show a list of all the blogs, along with the edit, delete and read button will show you on this page. This page also has a button for Create New Blogs at the top. You can click here from the create blog page. And there you can create your own blog.
@extends('blogs.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Laravel 5.8 CRUD Example</h2> </div> <div class="pull-right"> <a class="btn btn-success" href="{{ route('blogs.create') }}"> Create New Blog</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>Title</th> <th>Content</th> <th width="280px">Action</th> </tr> @foreach ($blogs as $blog) <tr> <td>{{ $blog->title }}</td> <td>{{ $blog->content }}</td> <td> <form action="{{ route('blogs.destroy',$blog->id) }}" method="POST"> <a class="btn btn-info" href="{{ route('blogs.show',$blog->id) }}">Show</a> <a class="btn btn-primary" href="{{ route('blogs.edit',$blog->id) }}">Edit</a> @csrf @method('DELETE') <button type="submit" class="btn btn-danger">Delete</button> </form> </td> </tr> @endforeach </table> @endsection
resources/views/blogs/create.blade.php
This is the third step of laravel 5.8 crud tutorial. In this file you will get a create blog form. Here you can create your own blog.
@extends('blogs.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Add New Blogs</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('blogs.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('blogs.store') }}" method="POST"> @csrf <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Title:</strong> <input type="text" name="title" class="form-control" placeholder="title"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Content:</strong> <textarea class="form-control" style="height:150px" name="content" placeholder="Content"></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/blogs/edit.blade.php
This is the fourth step of laravel 5.8 crud Tutorial Generator. With this file you can update your created blog.
@extends('blogs.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Edit Blogs</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('blogs.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('blogs.update',$blog->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>Title:</strong> <input type="text" name="title" value="{{ $blog->title }}" class="form-control" placeholder="Name"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Content:</strong> <textarea class="form-control" style="height:150px" name="content" placeholder="Detail">{{ $blog->content }}</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/blogs/show.blade.php
This is the fifth step of laravel 5.8 crud Tutorial Generator. With this file you can show and read your created or updated blog.
@extends('blogs.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2> Show Blogs</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('blogs.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>Title:</strong> {{ $blog->title }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Content:</strong> {{ $blog->content }} </div> </div> </div> @endsection
After completing the command, run the following command in your cmd
php artisan serve
Now you can open bellow URL on your browser:
http://localhost:8000/blogs
Type 2 :- Create Read Update Delete CRUD Operations Laravel 5.8 Query Builder with Example
1.Install Laravel 5.8.
2.Create Database Table.
3.Create Table Model.
4.Create Blog Form.
5.Route File.
6.Create Controller With (Insert,Update,Delete).
7.Update Blog Form With Show Data.
Step 1:- Install Laravel 5.8
The first step is to install laravel 5.8. To install laravel 5.8 we first have to install the composer. If you have already installed the composer in your system or laptop. There is no need to install a composer.
composer create-project --prefer-dist laravel/laravel blog
Step 2:-Create Database Table
In this step, you have to create a database table, which will have names like column, title, description, category etc.
CREATE TABLE `blog` ( `id` int(11) NOT NULL, `title` varchar(250) DEFAULT NULL, `description` text, `category` varchar(250) DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Indexes for dumped tables -- -- -- Indexes for table `blog` -- ALTER TABLE `blog` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `blog` -- ALTER TABLE `blog` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;COMMIT;
Step 3:-Create Table Model
Then after that we will create a database table model. To create a model we can create a model by using the laravel command. The systax to create the model is given below.
php artisan make:model Blog
app/Blog.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Blog extends Model { protected $table = 'blog'; }
Step 4:-Create Blog Form
Then after that we will create a blog form. In which we will add the input text field title, description, category. In this form, csrf will also use the token. Csrf’s use is used for security purpose.
resources/views/blog.blade.php
<!DOCTYPE html> <html> <head> <title>Create, Read, Update and Delete Operation Laravel 5.8</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"> </head> <body> <form action="{{url('savedata')}}" method="post"> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> <div class="col-md-12"> <div class="form-group"> <label for="">Title</label> <input name="title" type="text" value="{{old('title')}}" class="form-control"> </div> <div class="form-group"> <label for="">Description</label> <textarea class="form-control" value="{{old('description')}}" rows="10" cols="105" name="description"></textarea> </div> <div class="form-group"> <label for="">Category</label> <select name="category" class="form-control"> <option>--Select Category--</option> <option value="song">Song</option> <option value="video">Video</option> <option value="movie">Movie</option> </select> </div> </div> <div class="col-md-6"> <div class="box-footer"> <button type="submit" class="btn btn-primary">Save</button> </div> </div> </form> <h1>List Data</h1> <table id="example1" class="table table-bordered table-striped"> <thead> <tr> <th>S.No.</th> <th>Title</th> <th>Description</th> <th data-orderable="false">Action</th> </tr> </thead> <tbody> <?php $i=1; foreach ($bloglist as $key => $value) { $title = $bloglist[$key]->title; $page = $bloglist[$key]->description; ?> <tr> <td>{{$i}}</td> <td>{{$title}}</td> <td><?PHP echo stripcslashes(substr($bloglist[$key]->discription, 0, 149)); ?>..</td> <td> <a href="{{url('edit/'.$bloglist[$key]->id)}}">Edit</a> | <a href="{{url('delete/'.$bloglist[$key]->id)}}" onclick="return confirm('Are you sure? Delete ')">Delete</a> </td> </tr> <?php $i++; }?> </tbody> </table> </body> </html>
Step 5:- Route File
Then we will give the url of post method in the route file, which we will post the data through the controller and save it in the database. And in the same route file, we will also write a query of update or delete.
routes/web.php
Route::get('blog', 'blogController@blog'); Route::post('savedata', 'blogController@savedata'); Route::get('edit/{id}', 'blogController@edit'); Route::post('Updateblog', 'blogController@Updateblog'); Route::get('delete/{id}', 'blogController@delete');
Step 6:-Create Controller With (Insert,Update,Delete)
Then we will create the controller. Through this controller we will insert, update, delete the data.In laravel 5.8 we will save the data by query builder.So let’s insert data into laravel 5.8.Learn through this controller.
Syntax Create for Controller
php artisan make:controller blogController --resource
blogController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Blog; // DB use for query builder use DB; // DB use for query builder use Redirect; use View; class DataController extends Controller { public function blog(Request $request) { $bloglist = Blog::orderby('id', 'desc')->get(); return view('blog')->with('bloglist', $bloglist); } public function savedata(Request $request) { $title = $request->title; $description = $request->description; $category = $request->category; Blog::insert([ 'title' =>$title, 'description' =>$description, 'category' =>$category ]); return redirect()->back()->with('message', 'Thank you for Post Blog.'); } public function edit($id) { $edit_url = Blog::select('*')->where('id', '=', $id)->get(); return view('edit')->with('edit_url', $edit_url); } public function Updateblog(Request $request) { $id = $request->id; $title = $request->title; $description = $request->description; $category = $request->category; Blog::where('id', $id)->update([ 'title' =>$title, 'description' =>$description, 'category' =>$category ]); return redirect()->back()->with('message', 'Thank you for Post Blog Update.'); } public function delete($id){ $userans = Blog::where('id', $id)->delete(); if($userans){ return redirect()->back()->with('message', 'One recoard deleted'); } } }
Step 7:- Update Blog Form With Show Data
resources/views/edit.blade.php
<!DOCTYPE html> <html> <head> <title>Update Blog</title> </head> <body> <form action="{{url('Updateblog')}}" method="post"> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> <input type="hidden" name="id" value="{{ $edit_url[0]->id }}"> <div class="col-md-6"> <div class="form-group"> <label for="">Title</label> <input name="title" type="text" value="{{ $edit_url[0]->title }}" class="form-control"> <p class="help-block pull-right">*Required</p> </div> <div class="form-group"> <label for="">Description</label> <textarea name="description" class="form-control">{{$edit_url[0]->description}}</textarea> <p class="help-block pull-right">*Required</p> </div> <div class="form-group"> <label for="">Category</label> <select name="category" class="form-control"> <option value="">--Select Category--</option> <option value="song"<?php echo $edit_url[0]->category == 'song' ? ' selected="selected"' : ''; ?>>Song</option> <option value="video"<?php echo $edit_url[0]->category == 'video' ? ' selected="selected"' : ''; ?>>Video</option> <option value="movie"<?php echo $edit_url[0]->category == 'movie' ? ' selected="selected"' : ''; ?>>Movie</option> </select> </div> </div> </div> <div class="col-md-6"> <div class="box-footer"> <button type="submit" class="btn btn-primary">Update</button> </div> </div> </form> </body> </html>