Laravel 4-无法以一对多关系检索数据 [英] Laravel 4 - Can't retrieve data in a one-to-many relationship
问题描述
我有一个报价模型:
class Quote extends Eloquent {
public function quote_lines() {
return $this->hasMany('QuoteLine');
}
}
还有QuoteLine模型:
And a QuoteLine model:
class QuoteLine extends Eloquent {
public function quote() {
return $this->belongsTo('Quote');
}
}
我对此模型的表格是quote_lines.
My table for this model is quote_lines.
模式:
Schema::create('quote_lines', function($table)
{
$table->increments('id');
$table->integer('quote_id');
$table->string('title');
$table->string('desciption');
$table->integer('quantity');
$table->decimal('unit_price', 5, 2);
$table->timestamps();
});
Schema::create('quotes', function($table)
{
$table->increments('id');
$table->integer('contact_id');
$table->integer('company_id');
$table->timestamps();
});
问题是我似乎无法从Quote控制器访问引号行:
The thing is I can't seem to access the quote lines from my Quote controller:
$quote_lines = Quote::find($id)->quote_lines;
dd($quote_lines);
仅返回NULL
推荐答案
为使用magic属性,关系的方法名称必须采用正确的驼峰形式.例如,要使用Quote::find($id)->quote_lines
,该方法必须命名为quoteLines()
The method name for a relationship MUST be in proper camel-case in order to use the magic property. E.g., In order to use, Quote::find($id)->quote_lines
, the method must be named quoteLines()
让我解释一下:
调用Quote::find($id)->quote_lines;
会激活Eloquent类中的魔术__get方法. __get()在要检索的属性上调用getAttribute($key)
.因此,在这种情况下,它调用getAttribute('quote_lines')
Calling Quote::find($id)->quote_lines;
activates the magic __get method in the Eloquent class. __get() calls getAttribute($key)
on the property being retrieved. So in this case, it calls getAttribute('quote_lines')
如果键(quote_lines)在模型的属性数组中不可用,它将在类中搜索一个方法.它通过通过camel_case()
转换密钥来实现. Laravel的camel_case()方法会将quote_lines转换成quoteLines.然后,它会在您的类中查找一个名为quoteLines()的函数,如果找到该函数,则将其视为关系方法.
If the key (quote_lines) is not available in the model's attributes array, it will instead search for a method in the class. It does this by transforming the key via camel_case()
. Laravel's camel_case() method will transform quote_lines into quoteLines. It then looks for a function in your class named quoteLines(), and treats it as a relationship method if its found.
因此,关系方法必须写为quoteLines. (或任何适当的驼峰大小写的字符串)
Therefore, the relationship method MUST be written as quoteLines. (or any string that is proper camel case)
public function quoteLines() {
return $this->hasMany('QuoteLine');
}
然后您可以使用Quote::find($id)->quote_lines
或Quote::find($id)->quoteLines
来访问此关系(驼峰式包装时,两者都将作为'quote_lines'和'quoteLines'的求值为'quoteLines').
You may then access this relationship by using either Quote::find($id)->quote_lines
or Quote::find($id)->quoteLines
(both will work as both 'quote_lines' and 'quoteLines' evaluate to 'quoteLines' when camel-cased.)
脚注:laravel中的驼峰式保护套"指的是这种形式的字符串:heyThereWorld. 第一个字母必须是小写字母,所有后续单词的第一个字母必须大写
Footnote: "camel case" in laravel refers to a string of this form: heyThereWorld. The first letter must be lowercase, and the first letter of all subsequent words must be capitalized
这篇关于Laravel 4-无法以一对多关系检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!