是否可以使用Laravel在带有查询构建器的模型中工作? [英] Is it possible to work in a model with query builder using Laravel?

查看:41
本文介绍了是否可以使用Laravel在带有查询构建器的模型中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用查询生成器在模型中工作以在两个表之间建立联接,我不想用雄辩的话

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

这是我的模特

<?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;
}
}

推荐答案

如果您只想使用查询生成器,则可以这样做.但是我真的建议您使用雄辩的方法. 另外,查看模型的mutator,appends和scopes选项也很重要,这不仅对查询生成器有帮助,还可以提供更多帮助.

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.

其他约会:

    在此模型中,
  • DB::table('tbl_perimetros')可以替换为$this
  • 不要忘记return方法.
  • 尝试protect $appends = ['perimetros']并将方法重命名为getPermietrosAttribute,以将perimetros作为模型对象中的项返回.
  • 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.

这篇关于是否可以使用Laravel在带有查询构建器的模型中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆