A
A
Amenov_Abdul2019-06-19 05:24:52
Laravel
Amenov_Abdul, 2019-06-19 05:24:52

How to make a Laravel fetch?

Hello!
I can't solve this problem:
There are 4 tables: User, Categories, category_user, Tender
User, Categories I linked many to many.
I need to display tenders depending on which category the user has, before, when one category could be selected and not several, I did it like this:

$user = User::find($id);
return Tender::where('published_at', '<', Carbon::now())
        ->where('date_end', '>', Carbon::now())
        ->where('category', $user->category)
        ->where('status', true)
        ->with(['rates'])
        ->orderBy('id', 'DESC')
        ->get();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2019-06-19
@Amenov_Abdul

$categoryIds = [1, 2, 3];
$builder->whereIn('category_id', $categoryIds);

But it's better to link categories and tenders and get through
$user->categories()->with('tenders')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question