Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Are you ure you want to group by user_id? What are you trying to do?

Last updated 2 years ago.
0

@kokokurak This is sample like sudo code.

But I think group by is not a problem.

if I use

$article = new Article();
        $query = $article->with('user')
            ->where('user_id', '=', 1)
        $user = $query->get(['name','post','user.name']);
select name, post from `articles` where `user_id` = ?
select name from `users` where `user`.`id` in (?)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user.name' in 'field list' (SQL: select `name`, `post`, `user`.`name` from `articles` where `user_id` = 1)

same error.

Last updated 2 years ago.
0

Can not try myself, byt try this:

$article = Article::with(array('user' => function($query)
{
    $query->select('id','name as username');
}
))->where('user_id', '=', 1)
   ->get(['name', 'post']);
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

iClosedz iclosedz Joined 10 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.