AtyFlow
AtyFlow

Reputation: 35

Using scope to filter data

I am trying to do a search, I have verified that the data is received but not to make it show the filter.

Sending the data

<div class="panel-body">

    {!! Form::open (['route' => 'report.index', 'method'=>'GET', 'class' => 'navbar-form navbar-left pull-right']) !!}
        <div class="form-group">
            <input type="text" name= "name" class="form-control" placeholder="Buscar por Servicio">
        </div>
        <button  type="submit" class="btn btn-default">Buscar</button>

At the controller:

class ControllerReport extends Controller
{
    public function index(Request $request)
    {    
        $servicio = mivista::name($request->get('name'));

        return view('report.buscar_pagos', compact('servicio'));
    }
}

and in the model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

use Illuminate\Database\Eloquent\Builder;

class mivista extends Model
{
    protected $table = 'mivista';

    public function scopeName($query,$name)
    {
        if ( ! is_null($name)) {
            return $query->where('name', 'like', '%'.$name.'%');
        }
    }
}

I clarify that leaving $service = mivista::all(); In the controller shows me all the data, but I can not make the search work with the name parameter. Waiting for your help, thank you in advance

Upvotes: 1

Views: 2305

Answers (1)

Zayn Ali
Zayn Ali

Reputation: 4925

You're using query scope to modify the query so the returned value is Builder object. You've to call get on it to fetch the data.

Try this

$servicio = mivista::name($request->get('name'))->get();

Upvotes: 1

Related Questions