N
N
NDll2020-07-30 12:39:33
Laravel
NDll, 2020-07-30 12:39:33

Data output left join Repository?

there is a model
User (name,email)
AND models UserAttribute (fullanme, birthday, city_id), UserService (premium,top) , City (name)

There is an interface

interface UserRepositoryContract
{
    public function getAllUsersWithPaginate($perPage);
}

repository UserRepository
class UserRepository implements UserRepositoryContract
{
    private $user;

    public function __construct($user)
    {
        $this->user = $user;
    }
}

how do i make a query with leftJoin to list all users with name,fullanme,city.name?

I can't figure out how to make this request in the repository
public function getAllUsers($perPage)
{
   
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2020-07-31
@NDll

Use connections . Add user_id to UserAttribute/UserService, hasOne relationship

public function attributes()
{
    return $this->hasOne(UserAttribute::class);
}

UserAttribute bind to city
public function city()
{
  return $this->belongsTo(City::class);
}

Request
User::with('attributes.city', 'services')
    ->paginate($perPage);

But UserAttribute is an extra layer. It is better to remove these fields in User and write connections there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question