【问题标题】:Selected on editing the data with pivot table values在使用数据透视表值编辑数据时选择
【发布时间】:2022-01-19 05:44:23
【问题描述】:

我有 3 张桌子,

$student= Student::with('zones', 'appliances','phones')->findOrFail($id);
$appliances = Appliance::all('id', 'name');

我想在编辑学生时选择设备字段。

public function phones()
{
    return $this->hasMany(Phone::class);
}

public function appliances()
{
    return $this->belongsToMany(Appliance::class, 'dealer_appliances');
}

当我dd(user.appliances) 时,我得到了收藏。如何在编辑时在选择字段中选择这些字段?

【问题讨论】:

    标签: php laravel select eloquent


    【解决方案1】:

    我不知道这是否是一个好方法,但我是这样解决的:

    {{ in_array($appliance->id, $data->appliances->pluck('id')->toArray()) ? 'selected' : ''}} 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      相关资源
      最近更新 更多