Laravel - 真的很努力地理解雄辩 [英] Laravel - Really struggling to understand eloquent

查看:109
本文介绍了Laravel - 真的很努力地理解雄辩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相当新的Laravel从Codeigniter过来,大部分我真的很喜欢,但我真的不能让我的头在雄辩。



如果我想这样做一个简单的查询:

  SELECT * FROM site INNER JOIN tweeter ON tweeter.id = site .tweeter_id 

我尝试做这样的事情(带有属于):

  $ site = Site :: with('tweeter') - > find($ site_id); 

但是现在我有两个查询和一个不需要的IN(),像这样:

  SELECT * FROM`site` WHERE`id` ='12'LIMIT 1 

SELECT *从`tweeter` WHERE`id` IN('3')

所以我尝试强制加入像这样:

  $ site = Site :: join('tweeter','tweeter.id','=' site.tweeter_id') - >找到($ SITE_ID); 

现在我收到如下错误:

  SQLSTATE [23000]:完整性约束违规:1052 where子句中的列id不明确

SQL:SELECT * FROM`site` INNER JOIN` tweeter` ON`tweeter`.`id` =`site.tweeter_id` WHERE`id` =?限制1

绑定:数组(
0 => 12,

很明显错误在哪里,哪里需要使用像site.id =?这样的东西。但是我无法看到这样的事情发生了吗?



所以我只是回到流畅和使用:

  DB :: table('site') - > join('tweeter','tweeter.id','=','site.tweeter_id') - > ;其中('site.id','=',$ site_id) - > first()

我想这不是一个大问题。我真的很想说服雄辩。我忍不住感觉到我大错特错,误会了它的工作原理。我错过了什么吗?或者真的要以非常具体的方式使用?



我想我真正的问题是:有没有使我想使用Eloquent的查询?

解决方案

我实际上发现这个行为是有利的。考虑一下(我会修改你的例子)。所以我们有很多网站,每个都有很多高音扬声器。每个网站在DB中有很多信息:许多列,其中一些文本列包含大量文本/数据。



您可以使用以下方式进行查询: p>

  SELECT * FROM site INNER JOIN tweeter ON tweeter.id = site.tweeter_id 

有两个缺点:


  1. 您会收到大量冗余数据。您获得的同一网站的高音单位的每一行将具有您只需要一次的同一站点数据,因此PHP和DB之间的通信需要更长时间。

  2. 您如何做foreach( tweeter_of_this_site)?我猜你会显示所有的网站在某种类型的列表,然后在每个网站,你显示所有的高音扬声器。您将需要编程一些自定义逻辑来执行此操作。

使用ORM方法可以解决这两个问题:它只能获取站点数据一次,它允许你这样做:

  foreach($ sites as $ site){
foreach($ site-> tweeters as $ tweeter){}
}

我还是说的是:不要打它!我曾经说过:为什么要使用ORM,我可以编写自己的SQL,谢谢。现在我在Laravel使用它,这很棒!


I'm fairly new to Laravel having come over from Codeigniter and for the most part I really like it, but I really can't get my head around Eloquent.

If I want to do a simple query like this:

SELECT * FROM site INNER JOIN tweeter ON tweeter.id = site.tweeter_id

I try doing something like this (with a "belongs to"):

$site = Site::with('tweeter')->find($site_id);

But now I have two queries and an IN() which isn't really needed, like so:

SELECT * FROM `site` WHERE `id` = '12' LIMIT 1

SELECT * FROM `tweeter` WHERE `id` IN ('3')

So I try and force a join like so:

$site = Site::join('tweeter', 'tweeter.id', '=', 'site.tweeter_id')->find($site_id);

And now I get an error like so:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous

SQL: SELECT * FROM `site` INNER JOIN `tweeter` ON `tweeter`.`id` = `site.tweeter_id` WHERE `id` = ? LIMIT 1

Bindings: array (
  0 => 12,
)

It's obvious where the error is, the where needs to use something like "site.id = ?". But I can't see anyway to make this happen?

So i'm just stuck going back to fluent and using:

DB::table('site')->join('tweeter', 'tweeter.id', '=', 'site.tweeter_id')->where('site.id','=',$site_id)->first()

I guess it's not a massive problem. I would just really like to understand eloquent. I can't help but feel that i'm getting it massively wrong and misunderstanding how it works. Am I missing something? Or does it really have to be used in a very specific way?

I guess my real question is: Is there anyway to make the query I want to make using Eloquent?

解决方案

I actually find this behaviour advantageous. Consider this (I'll modify your example). So we have many sites and each has many tweeters. Each site has a lot of info in the DB: many columns, some of them text columns with lots of text / data.

You do the query your way:

SELECT * FROM site INNER JOIN tweeter ON tweeter.id = site.tweeter_id

There are two downsides:

  1. You get lots of redundant data. Each row you get for a tweeter of the same site will have the same site data that you only need once so the communication between PHP and your DB takes longer.
  2. How do you do foreach (tweeter_of_this_site)? I'm guessing you display all the sites in some kind of list and then inside each site you display all of it's tweeters. You'll have to program some custom logic to do that.

Using the ORM approach solves both these issues: it only gets the site data once and it allows you to do this:

foreach ($sites as $site) {
    foreach($site->tweeters as $tweeter) {}
}

What I'm also saying is: don't fight it! I used to be the one that said: why would I ever use an ORM, I can code my own SQL, thank you. Now I'm using it in Laravel and it's great!

这篇关于Laravel - 真的很努力地理解雄辩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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