jeudi 22 février 2018

How do I retrieve many questions from one user from a specific provider within my query?

I am a bit stuck on the logic to retrieve a user (a service provider) that has comments on his profile from many other users (in this case customers).

So what I want to retrieve is the service provider along with all questions that have been asked by the different customers and additional information about each customer to whom this question belongs to.

So my current relationship is a user (a customer) hasMany questions:

class User extends Authenticatable {
    use Notifiable;
    protected $table = 'na_user';

    public function questions()
    {
      return $this->hasMany(Question::class, 'provider_id'); // provider_id = ID of service provider
    }
}

And each question belongs to a specific user (a customer):

class Question extends Model
{
  protected $table = 'na_question';

  public function question()
  {
     return $this->belongsTo(User::class, 'user_id');
  }
}

My query looks like this:

$user = User::select('na_user.id', 'na_user.first_name', 'na_user.last_name') 
->with('questions')
->where( [
    [ 'na_user_more_info.user_id', $id ],  // ID of service provider
    [ 'na_user.user_role', 2 ]  // user_role of service provider
] )->firstOrFail();

What I retrieve from this query is all questions that have been asked to that specific service provider, but I am unable to also retrieve the customer that has posted the question and his information along with the query, how do I do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire