search validation in laravel

3

I have a search engine in laravel for teams by its ip and by its bar code for that create two scopes

     public function scopeBuscarip($query, $ip)
  {
    if (trim($ip) != '') {
      $query = $query->where('ip', $ip);
    }
  }

  public function scopeBuscarcod($query, $ip)
  {
    if (trim($ip) != '') {
      $query = $query->where('cod_barra', $ip);
    }
  }

This works well, but when I do not have registered products, it creates a redirection loop that is my problem. Someone can help me with this validation.

this is my code in the controller

public function index(Request $request)
    {
      $equipos = Equipo::buscarip($request->ip)->orderBy('id', 'ASC')->paginate(10);

       if (count($equipos) == null) {
        $equipos = Equipo::buscarcod($request->ip)->orderBy('id', 'ASC')->paginate(10);
        if (count($equipos) == null) {
          flash('No existe equipo registrado con la IP o el Codigo de Barra: ' . $request->ip , 'danger');
          return redirect()->route('equipos.index');
        }else{
            return view('equipos.list')->with('equipos', $equipos);
        }
      }else{
        return view('equipos.list')->with('equipos', $equipos);
      }
    }
    
asked by alexander123 28.11.2016 в 04:49
source

1 answer

1

Your main problem is that when there are no results, you redirect to the same method, for which an infinite redirection is generated.

The solution is probably (depending on how you have designed your application) show the same view of the list of equipment together with the "flash" that reports that there are no registered teams.

In addition to your main problem, I see other possible improvements to your code, such as eliminating responsibilities that do not correspond to Scopes and reducing redundant code in the controller, this without going too far and not talking about other layers to remove responsibilities to the controller:

  • It should not be the responsibility of the scopes to have to "clean" the input data, that is the responsibility of the request or a service, in the worst case, in the controller:

    public function scopeBuscarip($query, $ip)
    {
        $query = $query->where('ip', $ip);
    }
    
    public function scopeBuscarcod($query, $ip)
    {
        $query = $query->where('cod_barra', $ip);
    }
    
  • Apart from the main problem of infinite redirection, you can improve the checking of empty arrays and make a small rewrite of the code so that it can be understood and maintained more easily:

    public function index(Request $request)
    {
        $equipos = [];
    
        $equipos = Equipo::buscarip($request->ip)->orderBy('id', 'ASC')->paginate(10);
    
        $equipos = empty($equipos) ? Equipo::buscarcod($request->ip)->orderBy('id', 'ASC')->paginate(10) : $equipos;
    
        if (empty($equipos)) {
            flash('No existe equipo registrado con la IP o el Codigo de Barra: ' . $request->ip , 'danger');
        }
    
        return view('equipos.list')->with('equipos', $equipos);
    }
    
  • The code reads a bit strange, especially because you double check the ip property of the request with two different fields of the database, which apparently would not be very logical, but it depends on the requirements and the design of your application.

        
    answered by 28.11.2016 / 06:07
    source