mercredi 28 juin 2017

Search queries not returning on inputs

I have a search form to make finding products easier. unfortunately it isn't querying from my input and is just returning all the fields...

public function index(Request $request)
{
    //
    $types = Category::all();
    $products = Product::where('approved', '=', 1)->leftJoin('reviews', 'reviews.product_id', '=', 'product.id')->select('product.*', DB::raw('AVG(ratings) as ratings_average' ))->groupBy('id')->orderBy('ratings_average', 'DESC')->get();



    $name=$request->input('name');
    $type=$request->input('categories_id');


    if(!empty($name)){
        $products->Where('name', 'LIKE', '%'.$name.'%');
    }
    if(!empty($type)){
        $products->Where('categories_id', 'LIKE', '%'.$type.'%');
    }


    return view('pages.search')->withProducts($products)->withTypes($types)

View:

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

                    {!! Form::label('name', 'Search By Name:') !!}
                    {!! Form::text('name', null, array('class' => 'form-control')) !!}
                </div>



                    {!! Form::label('categories_id', 'Search By Category:') !!}
                    <select class="form-control" name="categories_id" >
                        <option value=""></option>

                        @foreach($types as $type)

                            <option value=""></option>
                        @endforeach
                    </select>

                </div>

                    {!! Form::submit('Find Product', array('class' => ' btn-lg btn-block')) !!}
                    {!! Form::close() !!}

Controller:

Route::get('search', 'SearchController@index')->name('search.index');

Any ideas why nothing seems to be happening ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire