E
E
Eduard Bazhenov2019-10-21 22:25:18
Laravel
Eduard Bazhenov, 2019-10-21 22:25:18

How to make a laravel filter?

I'm trying to implement a filter by three parameters.
view:

{{ Form::open(['route' => ['roll.index', 'method' => 'GET']]) }}
        {{ Form::token() }}
        <div class="input-group col-md-9">
            {{ Form::select('catalog_id', \App\Models\Catalog::pluck('label','id'), null, ['placeholder' => 'Выберите каталог...', 'class' => 'form-control'])}}
            {{ Form::select('category_id', \App\Models\RollCategory::pluck('label','id'), null, ['placeholder' => 'Выберите категорию...', 'class' => 'form-control'])}}
            {{ Form::select('picture_id', \App\Models\RollPicture::pluck('label','id'), null, ['placeholder' => 'Выберите направление рисунка...', 'class' => 'form-control'])}}
            <div class="input-group-append mb-3">
                {{ Form::submit('Применить', ['class' => 'btn btn-primary']) }}
            </div>
        </div>
        {{ Form::close() }}

Scope in the model:
public function scopeFilter($query)
    {
        if(request('catalog_id'))
        {
            $query->where('catalog_id', request('catalog_id'));
        }
        if(request('category_id'))
        {
            $query->where('category_id', request('category_id'));
        }
        if(request('picture_id'))
        {
            $query->where('picture_id', request('picture_id'));
        }
        return $query;
    }

Next I try to output the results to index but the request is sent as insert
public function index(Request $request)
    {
        $rolls = RollStorage::with(['catalog', 'category', 'picture'])->filter()->paginate('10');
        return view('admin.storage.roll.index', compact('rolls'));
    }
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'category_id' cannot be null (SQL: insert into `roll_storages` (`catalog_id`, `category_id`, `picture_id`, `updated_at`, `created_at`) values (1, ?, ?, 2019-10-21 19:15:07, 2019-10-21 19:15:07))

Probably, it is impossible to use one method, how then to do it differently and more optimally?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eduard Bazhenov, 2019-10-22
@kip_34

Figured it out myself

{{ Form::open(['route' => 'roll.index', 'method' => 'GET']) }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question