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 Example2.Create Read Update Delete CRUD Operations Laravel 5.8 Query Builder with Example
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.
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
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
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
resources/views/blogs/index.blade.phplaravel 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.
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.
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.
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
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
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
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');
}
}
}