laravel delete method code example

Example 1: force delete soft delete laravel

Soft Delete : $user->delete();
Force Delete : $user->forceDelete();
Restore Soft Deleted Item : $user->restore();

Example 2: laravel delete where

DB::table('users')->where('id', $id)->delete();

Example 3: laravel form method delete

// Blade
@method('delete')

// Html
<input type="hidden" name="_method" value="delete">

Example 4: delete route method in laravel

<form method="POST" action="{{ route('route.name', [ 'id'=> $item->id ]) }}">
              @csrf
              <input type="hidden" name="_method" value="DELETE">
              <button type="submit" class="btn btn-danger btn-icon">
                <i data-feather="delete"></i>
              </button>
            </form>

Example 5: laravel create or update

// If there's a flight from Oakland to San Diego, set the price to $99.
// If no matching model exists, create one.
$flight = App\Models\Flight::updateOrCreate(
    ['departure' => 'Oakland', 'destination' => 'San Diego'],
    ['price' => 99, 'discounted' => 1]
);

Example 6: larave Soft Deletes

Schema::table('flights', function (Blueprint $table) {
    $table->softDeletes();
});

Tags:

Php Example