【问题标题】:Laravel display created_by and edited_by name using respective IDsLaravel 使用各自的 ID 显示 created_by 和 edit_by 名称
【发布时间】:2019-03-10 14:36:42
【问题描述】:

我有一辆餐桌车。该表具有字段 - created_by 和edited_by。每次创建或编辑车辆时,该特定用户的 ID 都会保存到此列中。 在显示此表详细信息的显示视图中,我想显示用户名而不是 ID。我在这里错过了什么或做错了什么?

我与用户关联的车辆模型:

class Vehicle extends Model
{
    public function customer(){
        return $this->belongsTo('App\Customer','customer_id');
    }

    public function user(){
        return $this->belongsTo('App\User','edited_by');
    }
}

显示数据的控制器:

 public function show($id)
 {
    $vehicle = Vehicle::find($id);
    $files = File::where('vehicle_id',$id);
    return view('vehicles.show')->with('vehicle',$vehicle)
            ->with('files',$files);    
 }

查看

<tr>
    <th>Created By</th>
    <td>{{$vehicle->user->name}}</td>
</tr>
<tr>
    <th>Last Edited By</th>
    <td>{{$vehicle->user->name}}</td>
</tr>

我放置了hasmany关系的用户模型:

class User extends Authenticatable
{
    public function vehicles(){
        return $this->hasMany('App\Vehicle');
    }  
}

使用hasmany 我可以显示created_by 或edited_by 名称。如何同时显示 created_by 和edited_by 名称?

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您在 Vehicle 模型上的当前关系与编辑器匹配。为了为创建者实现相同的目标,您可以创建与 User 模型的另一个关系并将其传递给 created_by

    class Vehicle extends Model
    {
        public function customer(){
            return $this->belongsTo('App\Customer','customer_id');
        }
    
        public function creator(){
            return $this->belongsTo('App\User','created_by');
        }
    
        public function editor(){
            return $this->belongsTo('App\User','edited_by');
        }
    }
    

    在您看来,您可以使用$vehicle-&gt;creator-&gt;name$vehicle-&gt;editor-&gt;name 显示名称

    【讨论】:

      【解决方案2】:

      由于您只有用户的id,因此您必须从数据库中获取用户的名称。
      在您的模型中,您可以添加 Accessor 来实现此目的。

      class Vehicle extends Model
      {
           // rest of the code
      
          public function getCreatedByNameAttribute()
          {
              return User::where('id', $this->created_by)->pluck('name')->first();
          }
      }
      

      您可以通过以下方式访问created by users name

      <tr>
          <th>Created By</th>
          <td>{{$vehicle->created_by_name}}</td>
      </tr>
      

      edited_by做同样的事情

      如果你想使用关系来做到这一点。

      注意:为此,您必须在“created_at”上定义外键 列到user

      class Vehicle extends Model
      {
          // rest of the code
      
          public function creator()
          {
              return $this->belongsTo(User::class, 'created_at', 'id');
          }
      }
      

      那么您可以按如下方式访问它

      <tr>
          <th>Created By</th>
          <td>{{$vehicle->creator->name}}</td>
      </tr>
      

      【讨论】:

      • 感谢您的选择。但是,我希望利用关系(一对多/多对多),而不是使用查询直接从数据库中获取它。从数据库中获取名称可以直接在我的控制器中使用 - $created_by = User::find($customer->created_by)->name;和 $edited_by = User::find($customer->edited_by)->name;
      猜你喜欢
      • 2021-08-26
      • 2019-02-03
      • 2021-04-06
      • 1970-01-01
      • 2015-03-06
      • 2021-12-27
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      相关资源
      最近更新 更多