Is it possible to work in a model with query builder using Laravel?

心不动则不痛 提交于 2020-04-17 20:36:11

问题


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 to getPermietrosAttribute 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!