Laravel larvel:with()方法,引用父模型属性?
可以在with()方法中引用“父”模型属性吗?Laravel larvel:with()方法,引用父模型属性?,laravel,laravel-query-builder,Laravel,Laravel Query Builder,可以在with()方法中引用“父”模型属性吗? 以下是我想做的: $slug = $this->property('slug'); $post = new Post; $post = $post->where('slug', $slug) ->with(['categories' => function ($query) { $query->
以下是我想做的:
$slug = $this->property('slug');
$post = new Post;
$post = $post->where('slug', $slug)
->with(['categories' => function ($query) {
$query->where('category_attribute', '!=', 'post_attribute');
}]);
但它不起作用,似乎无法识别post属性
class Post extends Model
{
...
public $belongsToMany = [
'categories' => [
'Codalia\PluginName\Models\Category',
'table' => 'codalia_pluginname_categories_posts',
'order' => 'name'
]
];
...
}
class Category extends Model
{
...
public $belongsToMany = [
'posts' => ['Codalia\PluginName\Models\Post',
'table' => 'codalia_pluginname_categories_posts',
'order' => 'published_at desc',
],
...
}
您是否在模型帖子中注册了关系类别?另外,您能否更详细地分享一下您的帖子和类别模型的结构?是的,我在我的帖子模型中注册了关系类别。我认为针对这种特定情况进行连接查询可能更好。是的,我想这是最好的选择,但我必须“foreach”结果是将类别合并到一个数组中,就像()一样。with()方法没有提供更多的灵活性,这真是太遗憾了。您是否在模型帖子中注册了关系类别?以及,你能多分享一点你的帖子和类别模型的结构吗?是的,我在我的帖子模型中注册了关系类别。我认为对这个特定的案例进行连接查询可能更好。是的,我想这是最好的选择,但我必须“foreach”结果,以便将类别组合到一个数组中,就像()一样。with()方法没有提供更多的灵活性,这真是太遗憾了。