php Laravel 属于不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24469122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel belongsTo not working
提问by Yashari
I have 2 models in my app, 'User' & 'MedicineType' (each User belongs to one MedicineType).
我的应用程序中有 2 个模型,“用户”和“MedicineType”(每个用户属于一个 MedicineType)。
I made the one-to-many relation between two model using belongsTo() and hasMany(). hasMany() relation works perfectly but belongTo() doesn't work. Does anyone know where did I make a mistake?
我使用belongsTo() 和hasMany() 在两个模型之间建立了一对多关系。hasMany() 关系完美运行,但belongTo() 不起作用。有谁知道我在哪里犯了错误?
User::find(1)->medicine_type [this returns nothing]
MedicineType::find(1)->users [this returns users]
User::find(1)->medicine_type [这不返回任何内容]
MedicineType::find(1)->users [这将返回用户]
Here's the code to Models:
这是模型的代码:
class MedicineType extends Eloquent {
public function users()
{
return $this->hasMany('User');
}
}
class User extends Eloquent {
public function medicine_type()
{
return $this->belongsTo('MedicineType');
}
}
And here is my database structure:
这是我的数据库结构:
users:
id
name
medicine_type_id
medicine_types:
id
name
回答by Melvin Koopmans
The reason your relation is not working is not because of the relations specified in the model, but because of the method naming in the User model and not specifying the foreign key.
您的关系不起作用的原因不是因为模型中指定的关系,而是因为 User 模型中的方法命名而不是指定外键。
Instead of:
代替:
public function medicine_type()
{
return $this->belongsTo('MedicineType');
}
Use:
用:
public function medicineType()
{
return $this->belongsTo('MedicineType', 'id');
}
I hope this works for you ;)
我希望这对你有用;)
Everything together:
一切都在一起:
<?php // app/models/MedicineType.php
class MedicineType extends Eloquent {
// Determines which database table to use
protected $table = 'medicine_types';
public function users()
{
return $this->hasMany('User');
}
}
and:
和:
<?php // app/models/User.php
class User extends Eloquent {
// Determines which database table to use
protected $table = 'users';
public function medicineType()
{
return $this->belongsTo('MedicineType', 'id');
}
}
Testing if it works:
测试它是否有效:
$user = User::find(1);
return $user->medicineType->name;
This successfully returns the related medicine_type's name.
这成功地返回了相关的medicine_type 的名称。
I hope this helps you further ;)
我希望这能帮助你进一步;)
回答by Joseph
Maybe there's an issue with Eloquent finding the foreign key. Try this:
也许 Eloquent 查找外键存在问题。尝试这个:
class User extends Eloquent {
public function medicine_type()
{
return $this->belongsTo('MedicineType', 'medicine_type_id');
}
}
EDIT:
编辑:
Also, Eloquent tries to find the table "medicinetypes" and not "medecine_types", so you need to specify that as well using the $table
variable.
此外,Eloquent 尝试查找表“medicinetypes”而不是“medecine_types”,因此您还需要使用$table
变量指定它。
class MedicineType extends Eloquent {
protected $table = 'medicine_types';
public function users()
{
return $this->hasMany('User');
}
}
回答by HPage
I made the stupid mistake of not adding the "return" in the relationship method!
我犯了一个愚蠢的错误,没有在关系方法中添加“返回”!
Make sure you return the relationship... Obviously this will notwork:
请确保您返回的关系......显然,这将不工作:
public function medicineType()
{
$this->belongsTo('MedicineType', 'id');
}
回答by Yashari
I change "medicine_type" to "medicineType" and everythings got OK...
我将“medicine_type”更改为“medicineType”,一切正常...
回答by sh6210
In my case the related models data was deleted & laravel don't get soft deleted data in general query. To get soft deleted data you've to use "withTrashed() or onlyTrashed()".
在我的情况下,相关模型数据被删除,laravel 在一般查询中不会得到软删除数据。要获得软删除的数据,您必须使用“withTrashed() 或 onlyTrashed()”。
You can check the documentation here.
您可以在此处查看文档。
回答by Jonnathan moreno
The number one voted answer might be the best answer in most cases. However, if you still are having trouble loading the related relationship and no luck..
在大多数情况下,投票第一的答案可能是最佳答案。但是,如果您仍然无法加载相关关系并且没有运气..
There is one other thing that may work. Take a look at each of the model's tables and their indexes or foreign keys. In my case I had changed the table name but I never updated the involved index and foreign keys.
还有一件事可能有效。查看模型的每个表及其索引或外键。就我而言,我更改了表名,但从未更新涉及的索引和外键。
Solution.
解决方案。
A:(Feeling Lazy) Just delete the associated index or foreign key..
A:(感觉很懒)只要删除关联的索引或外键即可。
B:(I am not Lazy) Drop table via laravel migration and re-run artisan migrate with the appropriate foreign keys.
B:(我不是懒惰)通过laravel迁移删除表并使用适当的外键重新运行artisan migrate。
回答by genarocupil
Model:
模型:
class User extends Eloquent {
public function medicine_type()
{
return $this->belongsTo('MedicineType');
}
}
UserController.php
用户控制器.php
$users= App\User::all();
foreach ($users as $user) {
echo $user->medicine_type->name;
}