Alias for consultation in two tables with laravel

1

Is there an abbreviated way to identify the columns of each table by creating the query in this way?

$product = Product::with(['order'])->where('user_id','=',\Auth::user()->id)->find($id);

For example, specifying something like this:

->where('order.user_id','=',\Auth::user()->id)

user_id is a field in the order table, the example does not work because does not recognize the user_id field .

Or for this type of query I need to use if or if the buldier DB ::

    
asked by César Alejandro M 06.08.2017 в 06:24
source

1 answer

1

That's what the whereHas function is, which makes a query in the relationship:

->whereHas('order', function ($query) {
    $query->where('user_id', \Auth::user()->id);
})->find($id);

More information in the documentation: link

    
answered by 06.08.2017 / 14:50
source