问题
I would like to know if is it possible to work in a model using query builder to make a join between two tables, I don't want to use eloquent
this is my model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DB;
class Tbl_Perimetro extends Model
{
puplic function perimetros(){
$carteras = DB::table('tbl_perimetros')
->join('tbl_equipo_postventaatcs', 'tbl_equipo_postventaatcs.id', '=', 'tbl_perimetros.postventaatc_id')
->join('tbl_lista_carteras', 'tbl_equipo_postventaatcs.carteras_id', '=', 'tbl_lista_carteras.id')
->get();
return $carteras;
}
}
回答1:
If you just want to use query builder you could to do so. But I realy recomend to use eloquent methods. Also, is important to look about mutators, appends and scopes options to models that could help you more than query builder alone.
Other appointments:
DB::table('tbl_perimetros')
could be replaced by$this
in this model- Do not forget
return
for the method. - Try
protect $appends = ['perimetros']
and rename method togetPermietrosAttribute
to return perimetros as an item in model object.
来源:https://stackoverflow.com/questions/60101929/is-it-possible-to-work-in-a-model-with-query-builder-using-laravel