Include only some fields of the models related to Laravel Eloquent

In Laravel Eloquent queries I usually do a “with”, which allows me to fetch the related models at once, with eager loading.

The issue that I want to optimize now is to bring only some of the fields from the related models, instead of all the columns. It is a table that has a lot of information, but I only need to bring me the title.

I have seen in the Laravel documentation that I have the possibility to specify the fields, with this syntax.

$featuredProducts = ProductTag::whereTagId($tag->id)->whereFeatured(true)->with(‘product:title’)->get();

The problem is that it is not bringing me the title, not even the complete record, it only brings me null. The relationship is well realized, it is proven. If I do this:

$featuredProducts = ProductTag::whereTagId($tag->id)->whereFeatured(true)->with(‘product’)->get();

It returns the relationship correctly in the query, but Eloquent gives me all the product data and I only want the title. what could be wrong? Is it possible to optimize the query in another way?

See also  align-items flexbox in Internet Explorer
Loading Facebook Comments ...
Loading Disqus Comments ...