W
W
wearts_ru2018-11-06 20:32:47
ORM
wearts_ru, 2018-11-06 20:32:47

Laravel Eloquent summarizes different requests. How to get rid?

there is a code:

$model = new \App\Item;

if($value = $request->get('value'))
    $model = $model->where('title', 'like', '%'.$value.'%');

$params = $model->whereNotIn('id', $ids)->orderBy('title', 'asc')->get();
$paramschecked = $model->whereIn('id', $ids)->orderBy('title', 'asc')->get();

$query= $model->whereIn('id', $ids)->orderBy('title', 'asc')->toSql();

dd($query);

At the output I get:
string(192) "select * from `items` where `title` like ? and `id` not in (?, ?, ?, ?, ?, ?) and `id` in (?, ?, ?, ?, ?, ?) and `id` in (?, ?, ?, ?, ?, ?) order by `title` asc, `title` asc, `title` asc"

Why are requests cumulative?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iamoverit, 2018-11-07
@iamoverit

calling$model->whereNotIn('id', $ids) , you set the where property inside the $model object, which is taken into account when subsequently calling $model->get(). To
make two different requests, you need to use two different $model objects like this:

$model = new \App\Item;

if($value = $request->get('value'))
    $model = $model->where('title', 'like', '%'.$value.'%');

$paramsecheckedModel = clone $model;
$params = $model->whereNotIn('id', $ids)->orderBy('title', 'asc')->get();
$paramschecked = $paramsecheckedModel->whereIn('id', $ids)->orderBy('title', 'asc')->get();

$query = $model->toSql();
$query2 = $paramsecheckedModel->toSql();
dd([$query, $query2]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question