【问题标题】:How to change the standard id using sync in laravel 5如何在 laravel 5 中使用同步更改标准 ID
【发布时间】:2015-05-01 15:09:05
【问题描述】:

是否有使用同步更改标准 ID 的选项?

我想存储数据透视表的 id 而不是普通的 id。

在此处查看示例:

    foreach($foor->bar as $bar) {
        if(in_array($bar->id, array_keys($request->input('options')))) {
            $bar->options()->sync($request->input('options')[$bar->id]);
        }
    }

我想使用 $bar->pivot->id 的 id 而不是 $bar id,我该如何实现呢?

【问题讨论】:

    标签: laravel eloquent sync laravel-5


    【解决方案1】:

    你的问题不是很清楚,但据我猜测这应该可行:

    为了能够使用相关模型的列,请将with('relation') 添加到您的查询中。这会将相关模型立即加载到结果中。

    @见:http://laravel.com/docs/5.1/eloquent-relationships#eager-loading

    $foo = App\Bar::with('options')->all();
    foreach ($foo as $bar) {
        echo $bar->options->id
    }
    

    这应该可以让您访问选项的 id 并且您的代码可以是

    foreach($foor->bar as $bar) {
        if(in_array($bar->option->id, array_keys($request->input('options')))) {
            $bar->options()->sync($request->input('options')[$bar->option->id]);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 2017-07-12
      • 2017-03-16
      • 1970-01-01
      • 2019-03-13
      • 2019-02-22
      相关资源
      最近更新 更多