G
G
GaserV2016-07-17 13:50:30
Laravel
GaserV, 2016-07-17 13:50:30

How to pull the work of the desired category?

Such situation. There is a table - works, all works are stored in it. There is a column - categories , which indicates the categories to which the work belongs, in the form: category, category,
category I need to pull out all works related to a given category. How can I do it? Heard about realtionships, but did not understand. Could you show a working example? Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrzej Wielski, 2016-07-17
@GaserV

You have selected the wrong category structure.
Read about connections. For categories, belongsToMany is ideal, but it can be wiser - through a polymorphic relationship.
Then, to select by category, something like

Work::whereHas('categories', function($q) use($category){
   $q->where('slug', $category);
})->get();

V
Vasily Petrov, 2016-07-17
@token_bus

It's hard to judge such an informative question.
Try this
Work::where('categories', $category)->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question