Search for a row using a substring

0

You see, I have a table Game with the variable nombre , which obviously is a string of characters.

Well, I have created a form to search for a row using a substring:

@extends('layouts.app')
@section('content')
    <div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Introduzca el nombre del juego que busca</div>

                <div class="card-body">
                    <form method="POST" action="busca_nombre" novalidate>
                        @csrf

                        <div class="form-group row">
                            <label for="nombre" class="col-md-4 col-form-label text-md-right">Nombre del juego que busca</label>

                            <div class="col-md-6">
                                <input id="nombre" type="text" class="form-control{{ $errors->has('nombre') ? ' is-invalid' : '' }}" name="nombre" value="{{ old('nombre') }}" required autofocus>

                                @if($errors->has('nombre'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('nombre') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <input type="hidden" name="user_id" value="{{ auth()->user()->id }}"/>

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Buscar Juego
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

And this happened to the following function:

public function encuentroNombre(NombreRequest $request){

    public function encuentroNombre(NombreRequest $request){
        $juegos=Juego::where(stripos('nombre',$request->nombre),'>',0)->orderBy('numero')->get();
        if(count($juegos))
            return view('saludos',compact('juegos'));
        else
            return back()->with('message',['danger','No se ha encontrado ningun juego cuyo nombre contenga la palabra escrita.']);
    }

Well, I get this error message:

  

SQLSTATE [42S22]: Column not found: 1054 Unknown column '' in 'where   clause '(SQL: select * from juegos where' '> 0 order by numero   asc)

How do I indicate that I want you to check if a substring is in the variable nombre of the table Game?

    
asked by Miguel Alparez 18.07.2018 в 13:50
source

1 answer

1

You can do it using LIKE

Concatenating "%":

$juegos = Juego::where('nombre', 'like', '%'.$request->nombre.'%')->orderBy('numero')->get();

Or using (Complex curly syntax):

$juegos = Juego::where('nombre', 'like', '%{$request->nombre}%')->orderBy('numero')->get();
    
answered by 18.07.2018 / 14:46
source