Home > OS >  Laravel Relational Table if record exist
Laravel Relational Table if record exist

Time:02-16

I want to select only the products that have a record in the relational table in the selectbox.

For example I just want to get product name which is product_id in the monthly_records table

Tables;

Products(ID, product_name)

Suppliers(ID, supplier_name)

monthly_records(ID,pruduct_id,supplier_id,date)

CodePudding user response:

If I understood your question correctly then Laravel's Eloquent Relationships should do the trick for you.

Here is the documentation that will help you on this: https://laravel.com/docs/9.x/eloquent-relationships#one-to-many-inverse

CodePudding user response:

Eloquent has() method is what you are looking for. With that you can do something like:

Product::has('monthlyRecords')->pluck('name');
  • Related