Php 雄辩的简单关系并不';好像不行
我试图得到一个与计时器主题相关的特定计时器,比如:Php 雄辩的简单关系并不';好像不行,php,laravel,eloquent,Php,Laravel,Eloquent,我试图得到一个与计时器主题相关的特定计时器,比如: Timer::with('timer_theme')->find(1); 但它的回报是: >>Timer::with('Timer_theme')->查找(1); =>应用程序\计时器{#3245 id:1, 用户id:1, 鼻涕虫:“测试”, 结束时间:“2020-03-25 21:59:14”, 是否锁定:1, 创建时间:“2020-03-2418:26:33”, 更新地址:“2020-03-25 19:59:14”, 主题:1,
Timer::with('timer_theme')->find(1);
但它的回报是:
>>Timer::with('Timer_theme')->查找(1);
=>应用程序\计时器{#3245
id:1,
用户id:1,
鼻涕虫:“测试”,
结束时间:“2020-03-25 21:59:14”,
是否锁定:1,
创建时间:“2020-03-2418:26:33”,
更新地址:“2020-03-25 19:59:14”,
主题:1,
计时器主题:空,
}
我的模型设置如下:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Timer extends Model
{
protected $table = 'timers';
protected $fillable = [
'user_id',
'slug',
'end_time',
'is_locked',
'timer_theme_id'
];
public function timer_theme() {
return $this->belongsTo('App\TimerTheme');
}
public function user() {
return $this->belongsTo('App\Models\User');
}
}
及
我已经搞乱了模型中的关系,但仍然没有找到有效的解决方案。您的关系设置正确,并按预期工作: 但是,您提供的数据库布局、代码和调试输出在
timer\u themes
表的外键名称中显示了一些冲突
确保您的timers
表中有一个名为timer\u theme\u id
的列,然后您的型号应如下所示:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Timer extends Model
{
protected $table = 'timers';
protected $fillable = [
'user_id',
'slug',
'end_time',
'is_locked',
'timer_theme_id'
];
public function timer_theme() {
return $this->belongsTo('App\TimerTheme');
}
public function user() {
return $this->belongsTo('App\Models\User');
}
}
尝试使用TimerTheme::class
而不是中的字符串timer\u主题
with
>>timer::with(TimerTheme::class)->find(1)->toSql()illighted/Database/Eloquent/RelationNotFoundException,并在模型[App/timer]上显示消息“调用未定义的关系[TimerTheme]”)“timer\u theme
是您的桌子名吗?”@BarryDwith()
采用关系的名称,而不是类的名称。外键的名称是什么?您似乎在theme\u id
和timer\u theme\u id
之间切换。我想这可能是你的问题。谢谢你,我没有意识到我将列主题id命名为timer主题id。
timers
-------
id
user_id
slug
end_time
is_locked
theme_id
timer_themes
-------------
id
name
view_path
css_path