I have a hasMany relation function like this:
public function articles() { return $this->hasMany('App\Article'); }
And use it like this:
$data = \App\User::with('articles')->get();
I don't have any problems with it, since it's returning the expected data. Something like this:
{ "id": 1, "name": "Jhon", "lastname": "Doe", "articles": [ { "id": 1, "title": "Article 1", "status": "published", "published_at": "2015-04-30" }, { "id": 2, "title": "Article 2", "status": "draft", "published_at": null } ] }
What I am trying to achieve but I still can't it's to fetch just a subset of the relation's fields to obtain this:
{ "id": 1, "name": "Jhon", "lastname": "Doe", "articles": [ { "id": 1, "title": "Article 1" }, { "id": 2, "title": "Article 2" } ] }
My intention is to find a way to specify the subset of fields in the Model's function instead of iterating the returning collection and unset the unwanted fields.
Is this possible?
A hasMany relation denotes a one-to-many connection of a model to another model through referential integrity. The referential integrity is enforced by a foreign key constraint on the target model which usually references a primary key on the source model.
BelongsTo is a inverse of HasOne. We can define the inverse of a hasOne relationship using the belongsTo method. Take simple example with User and Phone models. I'm giving hasOne relation from User to Phone. class User extends Model { /** * Get the phone record associated with the user.
has() is to filter the selecting model based on a relationship. So it acts very similarly to a normal WHERE condition. If you just use has('relation') that means you only want to get the models that have at least one related model in this relation.
Yes, it's possible. You've got a couple options.
*NB: For options 1 and 2 below, the foreign key (user_id
) must be selected so that Laravel knows how to link the models together when building the relationships.
Modify the relationship query when using it. The with()
method can accept an array of key/value pairs where the key is the name of the relationship and the value is a Closure that modifies the relationship query.
$data = \App\User::with(['articles' => function($query) { // user_id is required here* $query->select(['id', 'title', 'user_id']); }])->get();
Create a new relationship that contains the fields you want.
public function articleTitles() { // user_id is required here* return $this->hasMany('App\Article')->select(['id', 'title', 'user_id']); } $data = \App\User::with('articleTitles')->get();
If you're only concerned about the array/json output, you can modify the App\Article model to only display the id and title when converted to an array.
class Article extends Model { protected $visible = ['id', 'title']; }
What you choose depends on what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With