我有以下三种型号:
class Category extends Eloquent
{
protected $primaryKey = 'categoryID';
public $timestamps = false;
protected $fillable = [
'categoryName',
];
public function categoriesfields()
{
return $this->hasMany(\App\Models\Categoriesfield::class, 'categoryID');
}
}
类别字段:
class Categoriesfield extends Eloquent
{
protected $primaryKey = 'fieldID';
public $timestamps = false;
protected $casts = [
'categoryID' => 'int'
];
protected $fillable = [
'fieldName_ar',
'categoryID'
];
public function category()
{
return $this->belongsTo(\App\Models\Category::class, 'categoryID');
}
public function categoriesoptions()
{
return $this->hasMany(\App\Models\Categoriesoption::class, 'fieldID');
}
}
分类选项:
class Categoriesoption extends Eloquent
{
protected $primaryKey = 'optionID';
public $timestamps = false;
protected $casts = [
'optionParent' => 'int',
'fieldID' => 'int'
];
protected $fillable = [
'fieldID'
];
public function categoriesfield()
{
return $this->belongsTo(\App\Models\Categoriesfield::class, 'fieldID');
}
}
当我运行这个查询时,我得到了这个错误:
$data= Category::with('category.categoriesfields')-
>with('category.categoriesfields.categoriesoptions')
->where('fieldID', '=', 3)->get();
SQLSTATE[42S22]:列未找到: 1054未知列'field ID'在'where子句'(SQL:选择*从类别
其中字段ID
=3)任何帮助请知道是什么问题!!?
有关此目的,请参见约束急加载:
$fieldID = 3; // added the variable here to see how to import it to the closure
$data = Category::with([
'categoriesfields' => function ($query) use ($fieldID) {
$query->with('categoriesoptions')->where('fieldID', '=', $fieldID);
}
])->get();
发生这种情况是因为在没有field dID
列的类别表上添加了where子句。
我正在使用Laravel和它提供的雄辩的ORM,但我正在努力选择我需要的数据。我有两种型号 我该怎么做?
在我的应用程序中,我更新了从一对多到多对多的关系,我试图找出一种持久化关联功能的方法。 假设我有两个相关的表,例如狗和主人。如果我有一系列的主人,并且我正试图为这些主人获取一份狗的id列表,我应该如何雄辩地做到这一点? 这里也提出了类似的问题:https://laracasts.com/discuss/channels/laravel/getting-many-to-many-related-da
问题内容: 我已经能够使用以下原始sql获取查询结果: 但是我还不能使它雄辩地工作。 根据一些类似的答案,我曾尝试在或中执行功能,但尚未过滤每个人的操作。就目前而言: 我至少在标题 大致 正确的方向,正确的…? 如果相关,则该模型具有两个关系: 问题答案: 因此,作为参考,我像这样解决了它: 奇怪的是,其中的子句需要语音标记才能使变量正确通过sql查询(同样,“ 2”似乎不起作用,而“ 2”却可以
我试图从一个表中选择所有结果,并在用户ID匹配时与另一个表合并。 我有三个表:runs、users和一个run\u用户透视表。我想从数据透视表中的“运行”和其他列(即“已完成”、“粘性”、“上次测试”和“难度”)中选择所有结果,但只从当前用户的“运行用户”中提取数据。 在原始SQL中,我通过一个带有AND语句的左连接成功地做到了这一点: 如何通过查询生成器执行此操作,有何建议?我可以在Larave
我使用laravel雄辩到CRUD数据。我的问题是,我不知道如何使用laravel雄辩的ORM进行UNION查询。你能帮我解决我的问题吗 这是我想要在laravel雄辩或查询生成器中实现的示例查询
假设我们正在使用Laravel的查询生成器: 我正在寻找一个等效的SQL: 当我必须键入大量select和WHERE(或者通常在select的列alias中包含别名,并且它在结果数组中使用)时,这将特别有用。如果没有任何表别名,我需要输入更多的内容,所有内容的可读性都会降低。在laravel文档中找不到答案,有什么想法吗?