Doubt Join with paginate Adjustable

Asked

Viewed 190 times

0

How to mount the following consultation with eloquent?

SELECT * FROM nos.destinos d join nos.servidors s on (d.idservidor = s.idservidor)
where s.servidor  like '%nome%' 

My models

class Servidor extends Model
{
    protected $primaryKey = 'idservidor';

    public function destino(){
        return $this->hasmany('App\Destino', 'idservidor', 'idservidor');
    }
}

class Destino extends Model
{
    protected $primaryKey = 'iddestino';

    public function servidor(){
        return $this->belongsTo('App\Servidor', 'idservidor', 'idservidor');
    } 
}

Controller

public Function index() { $destinations = new Destination;

         if(request()->has('perpage')) {
             session(['perPage' => request('perpage')]);
         }

         // filtros

         if (request()->has('servidor')){
            if (request('servidor') != ""){

                 $destinos = $destinos->join('servidors', 'destinos.idservidor', '=', 'servidors.idservidor')
                ->select('destinos.*', 'servidors.servidor')
                 ->where('servidors.servidor', 'like', '%' . request('servidor') . '%')->get();

            }
        }


        if (request()->has('iddestino')){
             if (request('iddestino') != ""){

                $destinos = $destinos->where('iddestino', '=', request('iddestino'));
             }
         }

      if (request()->has('idsetor')){
             if (request('idsetor') != ""){

                $destinos = $destinos->where('idsetor', '=', request('idsetor'));

             }
         }      
       // ordenando
       //  $destinos = $destinos->orderBy('destino', 'asc');

         $destinos = $destinos->paginate(session('perPage'))->appends([
             'servidors' => request('servidor'),
             'iddestino' => request('iddestino'),
             'idsetor' => request('idsetor'),
         ]);

         $perPages = PerPage::orderBy('valor')->pluck('nome', 'valor');

         $servidors = Servidor::orderBy('servidor')->pluck('servidor', 'idservidor');
         $setors = Setor::orderBy('setor')->pluck('setor', 'idsetor');


         return view('destino.index', compact('destinos','servidors', 'perPages', 'setors')); 
}

Error:

Badmethodcallexception Method paginate does not exist.

  • The error speaks of paginate() and your code shows no call to this method. Please edit the question and add this part of the code.

  • Sorry, I forgot... $destinations = $destinations->paginate('perPage'))->appends([ 'servors' => request('server'), 'iddestination' => request('iddestination'), ]);

  • Please enter the code in the question. And also, put the relevant code there, throwing pieces of code won’t do you any good.

  • Note if I pass the idsector works as expected, the problem is in Join when I pass a server.

1 answer

1


Turns out inside this if

if (request()->has('servidor')){ }

You materialize the data with a ->get(). This makes the query is executed in the database and the data is brought to memory.

Just remove this part, getting like this:

if (request()->has('servidor')){
    if (request('servidor') != ""){
         $destinos = $destinos->join('servidors', 'destinos.idservidor', '=', 'servidors.idservidor')
            ->select('destinos.*', 'servidors.servidor')
            ->where('servidors.servidor', 'like', '%' . request('servidor') . '%');
    }
}
  • Thanks for the help. I also removed the 'server.server' section from the select clause.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.