使用SoftDeletes时出错

时间:2017-08-05 07:08:48

标签: laravel laravel-5 laravel-5.4

我在某个模型中使用SoftDeletes时出现此错误。

(1/1) FatalThrowableError

Type error: Too few arguments to function Illuminate\Database\Eloquent\Model::setAttribute(), 1 passed in /Users/ThunderBirdsX3/Desktop/Workspace/myproject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php on line 519 and exactly 2 expected

at in HasAttributes.php (line 511)
at Model->setAttribute(object(Carbon))in HasAttributes.php (line 519)
at Model->setAttribute('', object(Carbon))in Model.php (line 1289)
at Model->__set('', object(Carbon))in SoftDeletes.php (line 70)
at Sell->runSoftDelete()in SoftDeletes.php (line 51)
at Sell->performDeleteOnModel()in Model.php (line 751)
at Model->delete()in SellController.php (line 176)
at SellController->destroy(object(Request), '99')
at call_user_func_array(array(object(SellController), 'destroy'), array(object(Request), 'sell' => '99'))in Controller.php (line 55)
at Controller->callAction('destroy', array(object(Request), 'sell' => '99'))in ControllerDispatcher.php (line 44)
at ControllerDispatcher->dispatch(object(Route), object(SellController), 'destroy')in Route.php (line 203)
at Route->runController()in Route.php (line 160)
at Route->run()in Router.php (line 574)
at Router->Illuminate\Routing\{closure}(object(Request))in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in SubstituteBindings.php (line 41)
at SubstituteBindings->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in Authenticate.php (line 43)
at Authenticate->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in VerifyCsrfToken.php (line 65)
at VerifyCsrfToken->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in ShareErrorsFromSession.php (line 49)
at ShareErrorsFromSession->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in StartSession.php (line 64)
at StartSession->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in AddQueuedCookiesToResponse.php (line 37)
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in EncryptCookies.php (line 59)
at EncryptCookies->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in Pipeline.php (line 102)
at Pipeline->then(object(Closure))in Router.php (line 576)
at Router->runRouteWithinStack(object(Route), object(Request))in Router.php (line 535)
at Router->dispatchToRoute(object(Request))in Router.php (line 513)
at Router->dispatch(object(Request))in Kernel.php (line 176)
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in ForceHttps.php (line 27)
at ForceHttps->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in ValidatePostSize.php (line 27)
at ValidatePostSize->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in CheckForMaintenanceMode.php (line 46)
at CheckForMaintenanceMode->handle(object(Request), object(Closure))in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))in Pipeline.php (line 102)
at Pipeline->then(object(Closure))in Kernel.php (line 151)
at Kernel->sendRequestThroughRouter(object(Request))in Kernel.php (line 116)
at Kernel->handle(object(Request))in index.php (line 53)

使用Sell::destroy($id);Sell::find($id)->delete();

Sell模型

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Sell extends Model
{
  use SoftDeletes;

  /* The attributes that are mass assignable.
  *
  * @var array
  */
  protected $fillable = [
    'employee_id', 'product_id',
  ];

  const UPDATED_AT = null;

  public function Employee()
  {
    return $this->belongsTo(Employee::class)->withTrashed();
  }

  public function Product()
  {
    return $this->belongsTo(Product::class)->withTrashed();
  }

  public function Install()
  {
    return $this->hasOne(Install::class);
  }
}

如果我删除use SoftDeletes;,则不会收到任何错误。 我的sells表格已经有deleted_at列。在模型中添加protected $dates = ['deleted_at'];它没有帮助。

控制器

<?php namespace App\Http\Controllers;

use Validator, Entrust;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Employee;
use App\Product;
use App\Position;
use App\Sell;

class SellController extends Controller
{

  ...

  public function destroy(Request $request, $id)
  {
    if (!Entrust::can('sell-delete')) { abort(403); }

    DB::beginTransaction();
    Sell::find($id)->Install()->delete(); //This line was not problem.
    Sell::destroy($id); //This line has a problem
    DB::commit();

    $request->session()->regenerateToken();

    return redirect()->back();
  }
}

数据库结构 DB Structure

1 个答案:

答案 0 :(得分:0)

尝试在模型中添加此内容

protected $dates = ['deleted_at'];

<强> Laravel Docs

  

要为模型启用软删除,请在模型上使用Illuminate \ Database \ Eloquent \ SoftDeletes特征,并将deleted_at列添加到$ dates属性