M
M
muhasa2019-07-30 17:34:57
Laravel
muhasa, 2019-07-30 17:34:57

Who faced such strangeness of laravel? How to explain?

The situation is this - if you make a request in this way

\Model\Ad::whereStatus(1)->whereIn("category_id", $this->data['categories']->map->only(['id']))->get();

then everything is worked out quickly enough, but if like this
\Model\Ad::whereStatus(1)->whereIn("category_id", $this->data['categories'])->get();

then the request is stretched for 5-6 seconds. In fact, the difference is that in the first case, I transfer ready-made IDs, in the second case, a collection with these IDs. But after all, the collection is their Laravel data type, why such garbage?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Vasilkov, 2019-08-01
@muhasa

Maybe I'm not an expert, but
$this->data['categories']->map->only(['id'])
1. map is a method... how can they access the method as a property in the lara? And if you have a `map` property, then be careful, there is such a method in the lara, and inside there under the hood there is almost a comparison of property_exists() method_exists() and everything is magic, you can tweak your model and there will be a question "how is it in general works" and not "why it doesn't work"
2. only this is for sampling if you made a collection from the array
collect([ 'name' => 'vasia', 'age' => 30 ])->only('name') ; // [ 'name' => 'vasia' ];
Roles::get()->only(['id' => 1]); // object Roles#1 (id => 1, title = ...)
3.
to get a list of IDs you need $categories->pluck('id')->all(); // [1,2,3]
Regarding stretching for 5-6 seconds - you see, Lara has a lot of magic inside. She is probably either looking for or trying to make toString() which calls some kind of serialize() under the hood. In general, it works, but the query there will turn out to be a fierce game, WHERE id IN ({json}, {json}) and some other garbage will be done
Debug queries var_dump($query->toSql(), $query-> getBindings()); and you look.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question