I’m quite new to Laravel so I don’t quite understand the eloquent and it’s query syntaxes.
I want to get the name
field in table products
from ProposalController
, but the table relationships are like this:
I read that I can use whereHas
if there’s a relationship between the wanted table. but can it be used for this case?
tb_proposal
-----------
id // primary key
customer_name
grand_total
tb_detail_proposal
------------------
id // primary key
proposal_id // foreign key to tb_proposal.id
product_id // foreign key to tb_product.id
qty
price
tb_product
----------
id // primary key
product_name
stock
price
this is what I have now in ProposalController.php:
class ProposalController extends Controller
{
public function index()
{
$data = Proposal::latest();
if (request('search')) {
$data->where('customer_name', 'like', '%'.request('search').'%')
->orWhereHas('tb_product', function (Builder $query) {
$query->where('product_name', 'like', '%'.request('search').'%')
});
}
$view_data = [
'page_title' => 'Browse Proposals',
'active' => 'proposal',
'data' => $data->get()
];
return view('proposal.index', $view_data);
}
}
2
Answers
No , because
whereHas
method in Laravel is specifically designed for querying based on relationships.if you want to query the products table directly without a relationship, you would typically use a join (read this) or a separate query for the products table.
Why don’t you instead move that logic into your model and keep your controller clean?
Here’s what I would do:
Define the relationship between
Product
andProposal
:The details inside the relationship will give you the ability to get
qty
andprice
, see here.Define the inverse relationship in Proposal:
With these changes you can get either collection with the related models as such:
Now, how about the search term? You can create a scope (either a global or local scope, according to your needs). Here’s the Proposal model, with an added search scope:
To use this in your controller you simply call: