请教laravel 8软删除问题,当在数据库中添加deleted_at列后,withTrashed()会变成未定义
当在Laravel 8框架下,使用SoftDeleted的时候,默认在Query中 添加 where deleted_at
is null。
在使用migration在对应的table里添加了deleted_at
列:
class AddDeletedAtToLogEntriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('log_entries', function (Blueprint $table) {
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('log_entries', function (Blueprint $table) {
//$table->dropColumn('deleted_at');
$table->dropsoftDeletes();
});
}
}
原本测试网页显示的报错信息:
Column not found: 1054 Unknown column ‘log_entries.deleted_at’ in ‘where clause’
会变成:
Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::withTrashed()
但在Model中已经明确call了SoftDelelted trait:
use Illuminate\Database\Eloquent\SoftDeletes;
class LogEntry extends Model
{
use SoftDeletes;
public function user() {
return $this->belongsTo('App\Models\User')->withTrashed();
}
.
.
.
}
希望有大神能够提供指点,谢谢。
推荐文章: