欢迎各位兄弟 发布技术文章
这里的技术是共享的
所以我有这个加入查询
$artikels = DB::table('artikel')
->leftJoin('kategori_artikel','artikel.kategori','=','kategori_artikel.id')
->leftJoin('admin','artikel.penulis','=','admin.id')
->select('artikel.id','artikel.judul','artikel.kategori','artikel.penulis',
'artikel.created_at','artikel.gambar','artikel.pilihan','artikel.status',
'kategori_artikel.id as kid','kategori_artikel.name as kname',
'admin.id as adid','admin.name as adname')
->get();
我试图以雄辩的方式做到这一点
artikel模型
public function kategoriartikel(){
return $this->belongsTo('kategoriartikel');
}
public function admin(){
return $this->belongsTo('admin');
}
kategoriartikel模型
public function artikel(){
return $this->hasMany('artikel');
}
管理模式
public function artikel(){
return $this->hasMany('artikel');
}
并在我的控制器
$artikels = Artikel::with(['kategoriartikel','admin'])->get();
所以从教程,我看,如果我想显示artikel的管理员名称,那应该是
@foreach($artikels as $artikel)
<li>{{ $artikel->admin->name }}</li>
@endforeach
但我只是得到
ErrorException (E_UNKNOWN) Trying to get property of non-object
由VRAPAN 发表于1年前
嗨,
我有一对一和一对多的关系,我用它来提取的数据是好的.... ....其中一个关系是超过10000行,SQL服务器抱怨说它只能处理2100个参数。这个错误让我意识到,With eloquent选项创建一个where子句,转换为:where(id)in(foreign table ids),其超过2100。
那么只是产生这样的一个地方,所以我需要使用查询生成器而不是雄辩呢?如果这是一个很好的事情,因为我非常流利的SQL,但只是想知道是否有办法用雄辩的方式。
@vrapan是的,雄辩的这样做,没有其他的。
您需要查询构建器,不需要原始SQL,才能执行此操作。但是,问题是,你为什么要一次拉出所有10000条记录?
@JarekTkaczyk查询的最终结果是大约150个行,但要获取所有需要的数据,我需要加入几个表在一起。在表之间链接的行是相当多的。一个内在的联盟将在一个不会那么伟大的地方工作。
感谢您的答复,但至少我不会在寻找一种不存在的方法。
@vrapan然后我想你不想要一个with
,而是has/whereHas
。但是在这种情况下仍然可能不是最好的。
你确实意识到,没有这样的事情,一个雄辩的查询在所有雄辩是一个捷径,在运行时一切都转换成真正的mysql查询。并阅读这篇http://laravel.io/forum/05-12-2015-has-many-through-relationship-thepth
雄辩对象可以使用所有查询构建器方法,如join
:
$result = User
::join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.id', 'contacts.phone', 'orders.price')
->getQuery() // Optional: downgrade to non-eloquent builder so we don't build invalid User objects.
->get();
我会做这个每当->with
创建数百个相关对象我不在乎 - 巨大的性能差异。
没想到
默认情况下,Eloquent会假设您正在使用
admin_id
而不是penulis
在artikel
表上。查看关于关系的文档:
class Phone extends Eloquent { public function user() { return $this->belongsTo('User', 'local_key', 'parent_key'); } }
请注意,您有
local_key
和parent_key
,所以你local_key
会penulis
。但在大多数情况下,我建议您更改名称,以使默认值正常工作。
t0n1zz
是的,我已经将其更改为admin_id
但是它并没有起作用,我试着去
{{ dd(DB::getQueryLog()) }}
我得到这个数组
array(3) { [0]=> array(3) { ["query"]=> string(23) "select * from `Artikel`" ["bindings"]=> array(0) { } ["time"]=> float(1.71) } [1]=> array(3) { ["query"]=> string(69) "select * from `kategori_artikel` where `kategori_artikel`.`id` in (?)" ["bindings"]=> array(1) { [0]=> int(0) } ["time"]=> float(0.83) } [2]=> array(3) { ["query"]=> string(62) "select * from `Admin` where `Admin`.`id` in (?, ?, ?, ?, ?, ?)" ["bindings"]=> array(6) { [0]=> int(2) [1]=> int(5) [2]=> int(6) [3]=> int(7) [4]=> int(8) [5]=> int(4) } ["time"]=> float(0.93) } }
我也在做
dd($artikels = Artikel::with(['kategoriartikel','admin'])->get());
并得到
" ["kategori_artikel_id"]=> int(19) ["admin_id"]=> int(2) ["status"]=> int(1) ["gambar"]=> string(21) "wallpaper-2692643.jpg" ["pilihan"]=> int(1) ["created_at"]=> string(19) "2014-10-07 12:04:14" ["updated_at"]=> string(19) "2014-10-12 09:26:24" } ["relations":protected]=> array(2) { ["kategoriartikel"]=> NULL ["admin"]=> NULL } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) } [1]=> object(Artikel)#305 (20) { ["table":protected]=> string(7) "Artikel" ["fillable":protected]=> array(7) { [0]=> string(5) "judul" [1]=> string(7) "content" [2]=> string(8) "kategori" [3]=> string(6) "status" [4]=> string(6) "gambar" [5]=> string(7) "pilihan" [6]=> string(7) "penulis" } ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(10) { ["id"]=> int(2) ["judul"]=> string(40) "Odio sed perspiciatis nihil rem aperiam." ["content"]=> string(1010) " White Rabbit, trotting slowly back again, and said, very gravely, 'I think, you ought to have got in as well,' the Hatter added as an explanation. 'Oh, you're sure to happen,' she said this, she noticed that they could not join the dance. '"What matters it how far we go?" his scaly friend replied. "There is another shore, you know, as we were. My notion was that it made no mark; but he could go. Alice took up the little door into that lovely garden. First, however, she waited patiently. 'Once,' said the King, who had meanwhile been examining the roses. 'Off with her head!' Those whom she sentenced were taken into custody by the way of expressing yourself.' The baby grunted again, and all dripping wet, cross, and uncomfortable. The first thing she heard the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT-POCKET, and looked at her, and the second thing is to find her in a languid, sleepy voice. 'Who are YOU?' said the Caterpillar. Here was another long passage, and the blades of.
如果你仔细阅读会有
{ ["kategoriartikel"]=> NULL ["admin"]=> NULL }
它应该是NULL吗?
t0n1zz
好的,多次检查后,我发现有一些打字错误(小写和大写哈哈哈)为我的功能
所以总体感谢你帮助我:D