英文:
Models relation not working when trying to get the children models in Laravel
问题
I understand your request. Here's the translated code portion:
我有一个属于模型 `Exam` 的模型 `Question`。`Exam` 可以有许多问题(一对多关系)。在存储数据时我没有问题,但是当我尝试使用 `Exam::find($exam->id)->questions;` 来获取考试的问题时,我得到了 `NULL`。我已经使用 `dd` 输出了 `Exam::find($exam->id)` 的结果,关系是空的:`#relations: []`。
这是我注册我的 `Question` 模型的方式:
```php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Question extends Model
{
public $timestamps = false;
protected $fillable = ['question', 'correct_answer', 'category_id', 'type'];
public function questionCategory():belongsTo {
return $this->belongsTo(QuestionCategory::class);
}
public function exam(): BelongsTo {
return $this->belongsTo(Exam::class);
}
}
这是 Exam
模型:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Exam extends Model
{
public $timestamps = false;
protected $fillable = ['name'];
public function question(): HasMany
{
return $this->hasMany(Question::class);
}
}
这是问题表的数据库模式来自迁移文件:
Schema::create('questions', static function (Blueprint $table) {
$table->id();
$table->string('question');
$table->integer('correct_answer')->nullable();
$table->integer('question_category_id')->default(1);
$table->integer('exam_id');
$table->string('type')->default('text');
});
这是我保存问题数据的方式:
$request->validate([
'examId' => ['required', 'numeric'],
'questions' => 'required'
]);
$exam = Exam::find($request->input('examId'));
$questions = $request->input('questions');
foreach ($questions as &$question_entry) {
// 如果问题不存在则创建问题
if ((int)$question_entry['id'] === 0) {
$question = new Question();
} else {
$question = Question::find($question_entry['id']);
}
$question->exam_id = $exam->id;
$question->question = $question_entry['body'];
$question->type = $question_entry['type'];
$question->question_category_id = $question_entry['category'];
$question->save();
}
成功保存到数据库,并且我可以在数据库中看到记录。如果我尝试像 Question::where('exam_id', 1)->get()
这样的操作,我会得到来自该考试的问题,但我不明白为什么当我尝试从父模型获取结果(例如 Exam::find(1)->questions
)时,我得到了 NULL
。似乎两个模型之间没有关系。
我正在使用 Laravel 10 与 Homestead 和 Breeze。
This should cover the code portion without any additional information.
<details>
<summary>英文:</summary>
I'm having a model `Question` that belongs to model `Exam`. `Exam` can have many questions (one-to-many relation). I've no problem with storing the data, but when I try to get the questions for the exam with `Exam::find($exam->id)->questions;` I get `NULL`. I've `dd` the result for `Exam::find($exam->id)` and the relations are empty: `#relations: []`.
This is how I've registered my `Question` model:
```php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Question extends Model
{
public $timestamps = false;
protected $fillable = ['question', 'correct_answer', 'category_id', 'type'];
public function questionCategory():belongsTo {
return $this->belongsTo(QuestionCategory::class);
}
public function exam(): BelongsTo {
return $this->belongsTo(Exam::class);
}
}
And this is the Exam
model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Exam extends Model
{
public $timestamps = false;
protected $fillable = ['name'];
public function question(): HasMany
{
return $this->hasMany(Question::class);
}
}
Here is the DB schema for the question's table from the migration file:
Schema::create('questions', static function (Blueprint $table) {
$table->id();
$table->string('question');
$table->integer('correct_answer')->nullable();
$table->integer('question_category_id')->default(1);
$table->integer('exam_id');
$table->string('type')->default('text');
});
Also this is how I'm saving the data for the questions:
$request->validate([
'examId' => ['required', 'numeric'],
'questions' => 'required'
]);
$exam = Exam::find($request->input('examId'));
$questions = $request->input('questions');
foreach ($questions as &$question_entry) {
// create the question if it doesn't exist
if ((int)$question_entry['id'] === 0) {
$question = new Question();
} else {
$question = Question::find($question_entry['id']);
}
$question->exam_id = $exam->id;
$question->question = $question_entry['body'];
$question->type = $question_entry['type'];
$question->question_category_id = $question_entry['category'];
$question->save();
}
The saving to the DB is successful and I can see the entries in the DB. And if I try to do something like Question::where('exam_id',1)->get()
I will get the questions from that exam, but I don't understand why when I try to get the results from the parent model (like Exam::find(1)->questions) I get
NULL`. It seems like there is no relation between the two models.
I'm using Laravel 10 with Homestead and Breeze.
答案1
得分: 1
I see you are using the singular question
in your relation definition in the Exam
model. That could be the reason why it is null.
Did you also try the plural questions
, like this:
public function questions(): HasMany
{
return $this->hasMany(Question::class);
}
The rest of the code seems fine, but you could also create the foreign id like this:
$table->foreignId('exam_id');
You can read more about foreign keys in the documentation.
英文:
Is see you are using the singular question
in your relation definition in the Exam
model.
That could be the reason why it is null.
Did you also try the plural questions
, like this:
public function questions(): HasMany
{
return $this->hasMany(Question::class);
}
The rest of the code seems fine, but you could also create the foreign id like this:
$table->foreignId('exam_id');
You can read more about foreign keys in the documentation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论