5

I have two database tables: users and students. A student is a user, but has extra data (address). I have a belongsTo('User') relationship in my Student model.

If I now query the Student model like this:

$this->student
    ->with('user')
    ->first();

I get this result:

{
    "id": 1,
    "user_id": 12,
    "street": "Petershof",
    "house_number": "3787",
    "postal_code": "8161 NM",
    "city": "Tienhoven",
    "user": {
        "id": 12,
        "email": "[email protected]",
        "first_name": "Nathan",
        "last_name": "van Dijk",
        "name": "Nathan van Dijk"
    }
}

However, I want to flatten the result, so that the user fields are in the parent (Student) object. Like this:

{
    "id": 1,
    "user_id": 12,
    "street": "Petershof",
    "house_number": "3787",
    "postal_code": "8161 NM",
    "city": "Tienhoven",
    "email": "[email protected]",  // = user field
    "first_name": "Nathan",        // = user field
    "last_name": "van Dijk",       // = user field
    "name": "Nathan van Dijk"      // = user field
}

I have tried to use leftJoin('users', 'user_id', '=', 'users.id') instead of with(), but then I lose my virtual attribute 'name' (defined in the User model) and also can't query relations of User anymore.

How can I achieve this in a clean way?

2 Answers 2

0
$this->student
  ->leftJoin('users', 'users.id', '=', 'students.user_id')
  ->select(
    'students.*',
    'users.email',
    'users.first_name',
    'users.last_name',
    DB::raw("concat(users.first_name, ' ', users.last_name) as name"),
  )->first();

You could drop the table in select like users.email in this case.

The relations will work just the same, I suppose the problem was only with overriding students id with users id, if you didn't specify the select and just grabbed all the fields.

5
  • 1
    Thanks for your answer. This is a good way to go for now, but the thing I don't really like is that you skip the User model. So if I want to change the virtual name field, I have to change it on different locations. Jan 20, 2015 at 22:02
  • What do you mean by skip user model ? If the only thing you want to achieve is output presentation, in form of JSON I suppose, then you should have said that. Jan 20, 2015 at 22:26
  • 2
    I mean that with using leftJoin() I have to rewrite the 'virtual name field' as an sql concat, while I already defined it in the User model, so that is double code and not DRY. Ideally would be to do something like this: $this->student->with(user, 'left join')->first(). Is there a way to achieve that? Jan 20, 2015 at 22:37
  • Well, I wouldn't worry about DRY in such case. You either use Eloquent with all its features (ie. no joins on relations at all) or you use custom queries (like I've shown above). So, no, there's no way to achieve that. And, again, tell me what you want it for, then I can suggest the solution. Jan 20, 2015 at 22:48
  • 1
    Ok, thanks for your explanation. I want to create a JSON api. There are more complex relationships to come, so what would be your advice? Use Eloquent, custom sql, or maybe other ORM? Jan 20, 2015 at 22:53
0

Old question, but here goes.

Reading your comment it seems you want to make an api for this. If it's only one endpoint formatting the result on the fly shouldn't be an issue however for more endpoints I would suggest a transformer pattern.

This would work by having a separate class return an array based on your db data. Then you take that array and use it in your response().

So, in your controller, instead of doing something like return response(Student::all()); you could do something like return response(StudentTransformer::transform(Student::all())).

The advantage here is you have only one folder (your Transformers) to check for response data format. The disadvantage is when your DB changes you also need to update your transformers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.