Hi Developer, In this tutorial we will learn delete record by id example in laravel web applications, This tutorial will give you very simple meny example of delete row by id in laravel.
We will see in this post how to delete records from database in four different ways using laravcel app. You can see in the following meny different examples, how to remove a row from a table using the laravel query.
Example 1: Delete By where query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
public function deleteUser()
{
$id = 1;
User::where('id', $id)->delete();
}
}
Example 2: Delete By find query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
public function deleteUser()
{
$id = 1;
$user = User::find($id);
$user->delete();
}
}
Example 3: Delete By destroy query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
public function deleteUser()
{
$id = 1;
User::destroy($id);
}
}
This query for delete multiple item
Example 4: Delete By destroy query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
public function deleteUser()
{
User::destroy([1, 2, 3]);
}
}
Example 5: Delete By whereIn query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
public function deleteUser()
{
User::whereIn('id', [1, 2, 3])->delete();
}
}
I hope this tutorial help for you.
Related Post