return view('site.item.show')->withItem($item->except(['created_at', 'updated_at']));
Oct 28, 2016
12
Level 25
makeHidden for relationship columns?
I got the following:
$item = Item::with('products')->whereHas('products', function ($query) use ($slug) {
$query->where('slug', '=', $slug);
})->firstOrFail();
return view('site.item.show')->withItem($item->makeHidden(['created_at', 'updated_at']));
I'm trying to hide some columns. I do it on the fly because I want to use those columns in a different view. So sometimes they need to be included and sometimes not. This works. However, not for the relations. Products still shows created_at and updated_at. How do you hide/visible them on the fly?
Level 25
this part didn't work:
Item::with('products', function($query) {
$query->select(['column1', 'column2', ...]); // in the queryBuilder you have to select them manulally
})
it will raise this exception:
ErrorException in Builder.php line 1150:
explode() expects parameter 2 to be string, object given
I finally got it:
$item = Item::with(['products' => function($query) {
$query->select(['name', 'id', 'item_id', 'slug']);
}])
->whereHas('products', function($query) use ($slug) {
$query->whereSlug($slug);
})
->exclude(['created_at', 'updated_at'])
->firstOrFail();
thanks for the help. I was ready to give it up. But with @InaniELHoussain directions I got it.
2 likes
Please or to participate in this conversation.