使用Auth :: id()仅在创建和更新时使用Eloquent

时间:2017-03-23 08:54:36

标签: laravel-5 eloquent

我需要帮助才能在创建和更新时使用Auth :: id()Eloquent。 我想要实现的是,当我使用Eloquent模型保存或更新时,当前登录的用户ID将被添加到touching_by字段。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

class Account extends Model
{
  use SoftDeletes;
  public function __construct(array $attributes = array())
  {
      $this->setRawAttributes(array_merge($this->attributes, array(
        'touched_by' => Auth::id()
      )), true);
      parent::__construct($attributes);
  }

  /**
   * The attributes that should be mutated to dates.
   *
   * @var array
   */
  protected $dates = ['deleted_at'];
}

但是当我做Model :: all()?

时,这会给我当前登录的用户

0 个答案:

没有答案