Laravel model is returning null for belongsTo and hasOne. PhpMyAdmin says "Link not found!", but the key exists and is directly accessible in both

huangapple go评论74阅读模式
英文:

Laravel model is returning null for belongsTo and hasOne. PhpMyAdmin says "Link not found!", but the key exists and is directly accessible in both

问题

我有两个模型,分别命名为:Subject(主题)和Group(组),Subject有一个名为group_id的列,设置了外键,指向groups表。

当我调用$subject->group时,它返回null;它应该返回Subject所属的Group模型。我可以通过调用Group::where('id', '=', $subject->group_id)手动获取Group,这个方法可以正常工作。$subject变量也按预期工作。

在Subject模型中我尝试过以下方法:

// 当然不是一次性尝试,这只是更容易展示我尝试过的方法。
public function group(): BelongsTo {
     return $this->hasOne(Group::class, 'id', 'group_id');
     return $this->belongsTo(Group::class, 'id', 'group_id');
     return $this->belongsTo(Group::class);
}

web.php:

Route::get('/subjects/{subject}', [App\Http\Controllers\SubjectsController::class, 'subject'])->name('subject');

在我的控制器中我如何进行测试:

public function subject(Request $request, Subject $subject) 
{
    dd($subject->group);
}

dd($subject) 返回一个填充了预期数据的Subject模型对象。返回$subject->group()返回一个Group模型对象,但其中没有数据。

我到目前为止尝试过:

// 使用php artisan tinker:
$s = Subject::first(); $g = Group::where('id', $s->group_id)->firstOrFail();

返回

Illuminate\Database\Eloquent\ModelNotFoundException 模型[App\Models\Group]没有查询结果。

$testSub = Subject::where('id', '=', 4074)->first();
$testGroup = Group::where('id', '=', $testSub['group_id'])->first();
dd($testSub, $testGroup, $testSub->group);

返回填充了预期数据的subject和group模型对象。$testSub->group返回null。

我调用了

dd($student->is($testSub));

它返回true。

一个能够工作的方法是:

$subject->group()->get()

但这不是理想的。

在模型中使用'with',无论是作为受保护的成员还是调用方法,都不起作用。

英文:

I have two models named: Subject and Group, Subject has a group_id column with a foreign key setup to point to the groups' table.

When I call $subject->group it returns null; it should return the Group model that the Subject belongs to. I can manually get the Group by calling Group::where('id', '=', $subject->group_id) and that works fine. The $subject variable is also working as expected.

What I've tried in the Subject model:

// Not all at once obviously, it's just easier to show what I've tried like this.
public function group(): BelongsTo {
     return $this->hasOne(Group::class, 'id', 'group_id');
     return $this->belongsTo(Group::class, 'id', 'group_id');
     return $this->belongsTo(Group::class);
}

web.php:

Route::get('/subjects/{subject}', [App\Http\Controllers\SubjectsController::class, 'subject'])->name('subject');

How I'm testing in my controller:

public function subject(Request $request, Subject $subject) 
{
    dd($subject->group);
}

dd($subject) returns a subject model object filled with expected data. Returning $subject->group() returns a group model object but no data in it.

What I've tried so far:

// php artisan tinker:
$s = Subject::first(); $g = Group::where('id', $s->group_id)->firstOrFail();

Returns

  Illuminate\Database\Eloquent\ModelNotFoundException  No query results for model [App\Models\Group].
        $testSub = Subject::where('id', '=', 4074)->first();
        $testGroup = Group::where('id', '=', $testSub['group_id'])->first();
dd($testSub, $testGroup, $testSub->group);

Returns the subject and group model objects filled with the expected data. $testSub->group returns null.

I called

dd($student->is($testSub));

And it returns true

Something that does work is:

$subject->group()->get()

But that isn't ideal.

Using 'with' either in the model as a protected member or calling the method, does not work.

答案1

得分: 0

检查输入参数 "$subject" 是否具有正确的数据。

在主题模型中,我使用以下关系可以获取组数据。

public function group(): BelongsTo {
    return $this->belongsTo(Group::class);
}
英文:

Check if the input parameter "$subject" has right data.

I use the following relationship in the subject model can get the group data.

public function group(): BelongsTo {
     return $this->belongsTo(Group::class);
}

答案2

得分: 0

我在 subjects 表中有另一列名为 'group',它正在干扰。现在更改了方法的名称,现在可以正常工作。

英文:

Turns out I had another column named 'group' in the subjects table that was interfering. Changed the name of the method and it works now.

huangapple
  • 本文由 发表于 2023年6月13日 05:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定