Php Laravel未定义变量-使用isset()
我和其他人一起研究另一个问题:在实现他们的解决方案时,无论我尝试了多少次错误处理,它都会失败 在我的模型中,我有:Php Laravel未定义变量-使用isset(),php,laravel-5,Php,Laravel 5,我和其他人一起研究另一个问题:在实现他们的解决方案时,无论我尝试了多少次错误处理,它都会失败 在我的模型中,我有: public function scopeFamily($query, $parentId) { if(isset($parentId)){ return $query->where('user_id', auth()->id())->where(function ($query) { $query->wher
public function scopeFamily($query, $parentId) {
if(isset($parentId)){
return $query->where('user_id', auth()->id())->where(function ($query) {
$query->where('parent_id', $parentId)
->orWhere('reference_id', $parentId);
});
}
}
然后在blade模板中,我使用以下方法调用它:
if(isset($tasks->user->parent_id)){
$parentId = $tasks->user->parent_id;
if(isset($parentId)){
$family = App\Models\User::family($parentId)->get();
}
}
我得到错误
未定义变量:parentId
。那不可能,是吗?我知道数据库中并非所有记录都有父id,但第一个isset()
应该已经消除了这些记录。在model useuse
关键字中。当我们必须访问闭包之外的变量时,需要使用use
关键字来访问这些变量
public function scopeFamily($query, $parentId) {
if(isset($parentId)){
return $query->where('user_id', auth()->id())->where(function ($query) use ($parentId){
$query->where('parent_id', $parentId)
->orWhere('reference_id', $parentId);
});
}
}
您是否使用了
use
关键字来使用parentId
我不明白我将如何或为什么使用use
关键字。这个错误似乎是在模型中抛出的,即使它是作为一个参数出现的,因此是在执行时设置的。