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.– Jéf Bueno
Sorry, I forgot... $destinations = $destinations->paginate('perPage'))->appends([ 'servors' => request('server'), 'iddestination' => request('iddestination'), ]);
– Juliano
Please enter the code in the question. And also, put the relevant code there, throwing pieces of code won’t do you any good.
– Jéf Bueno
Note if I pass the idsector works as expected, the problem is in Join when I pass a server.
– Juliano