dongqu9972 2018-08-03 13:29
浏览 29

检索子关系时,Laravel 5.5关系不起作用

I have a Laravel 5.5 app that has an edit or update function that has two dropdowns. One dropdown is the parent and the second dropdown is it child relationship. So basically the first dropdown has many second dropdown thats the case.

Now about the code in my controller

$services = Service::all()->sortBy('code', SORT_NATURAL | SORT_FLAG_CASE)->pluck('description', 'id');
$categories = Service::with('categories')->get()->sortBy('categories.name', SORT_NATURAL | SORT_FLAG_CASE)->pluck('categories.name', 'categories.id');

I used a pluck method that will retrieve only the name and the id of the categories which is the child relation of service like what I have said. A service has many categories and I only want to display the categories in the dropdown based on the selected service in the dropdown which is automatically chosen base on existing record with the help of Laravel pluck.

However the second dropdown or the categories is empty or the second query is not working.

Here is how I defined my relationship in the service model.

public function categories()
{
   return $this->hasMany('App\Models\Categories', 'service_id', 'id');
}

Now the code in my blade file

<div class="form-group col-sm-6">
    {!! Form::label('service_id', 'Service:') !!}
    {!! Form::select('service_id', $services, null, ['class' => 'form-control input-md','required', 'id' => 'service_id'])!!}
</div>

<div class="form-group col-sm-6">
    {!! Form::label('categories_id', 'Category:') !!}
    {!! Form::select('categories_id', $categories, null, ['class' => 'form-control input-md','required', 'id' => 'category_id'])!!}
</div>

Appreciate if someone could help. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doubinei1457 2018-08-03 13:37
    关注

    I would take a look at the values you are passing to pluck. Since you are generating a new collection with the second query, I believe you should be able to call pluck('name', 'id');. The categories.column_name is used to avoid ambiguity in the query, but all the collection is doing is retrieving models, so if you use the property names without the table prefix - you should get what you're after.

    I previously assumed that you were handling the sorting via query; since you are handling that at the collection level as well, I'd remove the categories prefix from that field too. You could also adjust the query to use the ->orderBy() method and pass the field names as an array to get (like so: ->get(['name', 'id'])). It all depends on your needs - if my first suggestion doesn't work for you, you could move the sorting and filtering to the query. Hope that helps some.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度