I'm doing CRUD operation using Backpack. Now I need soft delete for that. How can I enable it.
Following can be done for soft delete in Laravel.
add a column in the table called "deleted_at" with type datetime.
In the Model add namespace "use Illuminate\Database\Eloquent\SoftDeletes;" and Trait "use SoftDeletes;"
Example
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
class Age extends Model
{
use CrudTrait;
use SoftDeletes;
Related
anyone can help me?
how to resolve ->
BadMethodCallException
Call to undefined method App\ModalName::onlyTrashed()
my controller ->
public function destroy(Abc $abc)
{
$abc= Abc::destroy($abc->id)->get();
return redirect('/dir/abcdir')-> with('delete', 'aaa');
}
According to the docs, you need to use traits 'SoftDeletes'.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Flight extends Model
{
use SoftDeletes;
}
PS:
Please post complete code of your controller and model.
For SoftDelete to work in Laravel Eloquent Model
You must use SoftDeletes Trait
Table schema should have $table->softDeletes(); which will add deleted_at column in the table.
Reference: Eloquent Soft-Delete
I need to use policy for crud controller in laravel backpack crud package.
I use :
$this->authorizeResource(Post::class);
and i get this error :
Method
App\Http\Controllers\Admin\PostCrudController::authorizeResource does
not exist.
How should i use Policy (specially resource policy) in laravel backpack crud?
You should be able to do that using Laravel's AuthorizesRequests trait. Generated Backpack CRUD Controllers don't have it by default, since not everybody uses this Laravel feature.
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\MonsterRequest as StoreRequest;
use App\Http\Requests\MonsterRequest as UpdateRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class MonsterCrudController extends CrudController
{
use AuthorizesRequests;
public function setup()
{
// ...
I am using laravel default database notifications, I want to add softdelete to notifications table.
I have created a migration with softdelete which has added deleted_at column to the notifications table. The problem is I have to add 'use SoftDeletes' to notifications model(according to laravel docs) but cannot find the notifications model.
$table->softDeletes();
I tried adding 'use SoftDeletes' to HasDatabaseNotifications trait but it still deletes the row. Is there another way to add softdelete to notifications table. TIA
In your model at top before start class use
use Illuminate\Database\Eloquent\SoftDeletes;
After class
class Notification extends Model
{
use SoftDeletes;
/**
* The attributes that should be mutated to dates.
*
* #var array
*/
protected $dates = ['deleted_at'];
}
This is how I solved it, I hope it will be useful for you and other friends
App\Classes\MyDatabaseNotification.php
namespace App\Classes;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\DatabaseNotification;
class MyDatabaseNotification extends DatabaseNotification
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
App\Classes\MyNotifiable.php
namespace App\Classes;
use Illuminate\Notifications\Notifiable;
trait MyNotifiable
{
use Notifiable;
/**
* Get the entity's notifications.
*/
public function notifications()
{
return $this->morphMany(MyDatabaseNotification::class, 'notifiable')
->orderBy('created_at', 'desc');
}
}
App\User.php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Classes\MyNotifiable;
...
class User extends Authenticatable
{
use MyNotifiable;
...
In Category.php
namespace App;
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
and in Controller.php
namespace App\Http\Controllers;
use App\Models\Category;
use App\Setting;
use App\Post;
use function GuzzleHttp\Promise\all;
use Illuminate\Http\Request;
First Change from use App\Category; to use Category, to use \Category, to use App\Models\Category.
Second composer dump-autoload.
You are using two namespace in Category.php
namespace App; //REMOVE THIS If model file is in app\Models folder
namespace App\Models; // Remove this if model file is in app(root) folder
use Illuminate\Database\Eloquent\Model;
class Category extends Model {......}
Include it in your class dependencies:
use App\Category;
I have added $table->softDeletes() to my migration file and have added the protected $softDelete = true; to my model, and I do have the deleted_at in my table. But whenever I call delete(), the entire row is deleted. Here is how I am calling my delete:
Schedule::whereId($id)->whereClientId($client_id)->delete();
What am I doing wrong?
In case somebody bumps into this question. You can still use SoftDeletes in Laravel 5. Just use the softdeletes trait.
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; //<--- use the softdelete traits
class YourModel extends Model
{
use SoftDeletes; //<--- use the softdelete traits
protected $dates = ['deleted_at']; //<--- new field to be added in your table
protected $fillable = [
];
}
You can view laravel docs or This great tutorial on how to softdelete
If you're overriding \Illuminate\Database\Eloquent\Model::boot for whatever reason, ensure you call parent::boot in the method so traits (like SoftDeletes) can be booted.