P
P
Pavel Zhukau2015-08-11 02:34:45
MySQL
Pavel Zhukau, 2015-08-11 02:34:45

How to get a row from MySQL according to the necessary data located in different tables?

Good night everyone, I've been sitting sweating for an hour now, my head doesn't cook at all. I'll try to explain the problem:
there are tables:
clothes
clothes_styles
clothes_ranges
styles
ranges
And a bunch of similar tables.
In the clothes table - clothes are stored, in the clothes_styles table there is a binding of clothes to styles, that is, the clothes_id and styles_id fields, and in the styles table - the styles themselves. But the trouble is that in the clothes_styles table there can be several rows with the same clothes_id value, but different styles_id values. And there are still a bunch of such tables in the database, similar to these. What is the salt itself, you need to display rows from the clothes table, which, for example, have an id 1,2 style and a bunch of the same parameters by analogy with these.
Point in which direction to look. I am writing in Laravel, I tried using belongsToMany and where , but it does not search in the already formed array, but in the database. You can get an array of all clothes along with styles, ranges, etc. using with, but you have no idea how to search in it. I hope I have clarified the problem. Thanks in advance for any help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2015-08-11
@NikesDark

You can try like this:

// Модель Cloth
public function styles()
{
  return $this->belongsToMany(...);
}

And the sample:
$result = Cloth::whereHas('styles', function ($q)
{
  return $q->whereIn('id', [1, 2]);
})->get();

If I understand correctly, it should work.

I
Ivan Vyrov, 2015-08-11
@ProKiLL

The most optimal solution is to make an inner join request, see details here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question