Laravel Route and Simple CRUD using eloquent

Laravel Route and Simple CRUD using eloquent

 After creating the Laravel project, first of all, create a new post model and also migrate to use the posts table.

php artisan make:model Post -m

It creates a model Post.php for you and also creates a migration file like 2020_08_11_053014_create_posts_table.php in the migration folder.

Add bellow code to the 2020_08_11_053014_create_posts_table.php 

Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('body');
$table->boolean('is_admin')->default(0);
$table->timestamps();
});

After adding the code, lets migrate

php artisan migrate

Now let's create a route for posts table

Go to web.php file

Laravel default route 

Route::get('/', function () {
return view('welcome');
});

Let's learn the eloquent model or ORM(Object Relation Model):

It handles the database to create, read, Update, and Delete records from the database easily.

Create a post using save() method

/*
* Insert Data Using Eloquent Method
*/
Route::get('/create_post',function (){
$post = new Post();
$post->title='This is the first title';
$post->body = 'This is the post body';
$post->save();
});

It creates a new record to the database.

Another way to create a new record just like this

First, go the Post.php model and add this code

protected $fillable = [
'title',
'body'
];

Then, create another route to insert data to the database

/*
* Insert Data using Eloquent method and using model fillable attribute
*/

Route::get('/insert_data',function (){
Post::create(['title'=>'This is eloquent title','body'=>'This is eloquent body']);
});


insert data

Fetch the record using all() method

/*
* Find all records using eloquent all() method
*/

Route::get('get_post',function (){
$posts = Post::all();
return $posts;
});


Fetch single record using find() method

/*
* Find single records from DB post table using find() method
*/

Route::get('/get_post_one/{id}',function ($id){
$post = Post::find($id);
// return $post;
return var_dump($post);
});


Update data using save() method

/*
* Update data one using save() method
*/

Route::get('/update_post/{id}',function ($id){

$post = Post::find($id);
$post->title='Updated Post title';
$post->body='Updated post body';
$post->save();

});


Update data using update() method

/*
* Update data two using update() method
*/

Route::get('/update/{id}',function ($id){
Post::where('id',$id)->update(['title'=>'Updated title two','body'=>'updated body two']);
});


Delete record using delete() method


/*
* Delete data using delete() method
*/

Route::get('/delete/{id}',function ($id){
$post = Post::find($id);
$post->delete();
});


Another way to delete record

/*
* Another way to delete record from database
*/
Route::get('/delete2/{id}',function ($id){
Post::where('id',$id)->delete();
});


Now discuss in soft delete

/*
* Soft delete technique &
* This technique temporary deleted the record as a result the record didnt show in the fetch query.
* The record will go to the trashed using deleted_at column
*/

Route::get('/soft-delete/{id}',function ($id){
$post = Post::find($id);
$post->delete();
});
soft-delete


Fetch only trashed records

/*
* Getting only trashed records from DB
*/

Route::get('/get-only-trashed/',function (){
return Post::onlyTrashed()->get();
});

Fetching records with trashed

/*
* Getting records with trashed items from DB
*/

Route::get('/get-with-trashed',function (){
return Post::withTrashed()->get();
});

Restore all trashed records

/*
* Restore all trashed items and make it general records
*/
Route::get('/restore',function (){
Post::withTrashed()->restore();
});

Force delete method one to delete permanently delete only trashed items

/*
* Force delete to permanently delete trashed records
*/

Route::get('/force-delete1',function (){
Post::onlyTrashed()->forceDelete();
});

Force delete method two to delete permanently delete with trashed items

/*
* Force delete to permanently delete all records with trashed items
*/

Route::get('/force-delete2',function (){
Post::withTrashed()->forceDelete();
});


To find all routes just run this command

php artisan route:list

Output


route-list
:

 


You may like these posts