laravel model delete code example
Example 1: laravel delete where
DB::table('users')->where('id', $id)->delete();
Example 2: laravel fillable
protected $fillable = [
'title',
'slug',
'body',
'image',
'published',
'comments_open'
];
Example 3: laravel create or update
$flight = App\Models\Flight::updateOrCreate(
['departure' => 'Oakland', 'destination' => 'San Diego'],
['price' => 99, 'discounted' => 1]
);
Example 4: laravel delete relationship data
class User extends Eloquent
{
public function photos()
{
return $this->has_many('Photo');
}
public static function boot() {
parent::boot();
static::deleting(function($user) {
$user->photos()->delete();
});
}
}
Example 5: laravel eloquent remove from db
$res=User::where('id',$id)->delete();
Example 6: larave Soft Deletes
Schema::table('flights', function (Blueprint $table) {
$table->softDeletes();
});