php - 雄辩的关系查询返回空值
问题描述
我正在尝试获取一些关系数据。所有方法都很好,但只有一种方法不起作用。我正在尝试App\ModeltestMcqQuestion::find(id)->chapterwiseMcqs
从修补程序调用,但它返回 null。我的数据库填充良好。我尝试了很多 id,但没有任何效果。
我的模型是:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class ChapterwiseMcq extends Model
{
use SoftDeletes;
protected $fillable = [
'topic_id',
'question_type',
'question',
'question_description',
'question_resource',
'a',
'b',
'c',
'd',
'answer',
'answer_description',
'answer_resource',
];
public function topic(){
return $this->belongsTo(Topic::class);
}
public function modeltestMcqAnswers(){
return $this->hasMany(ModeltestMcqAnswer::class);
}
public function modeltestMcqQuestions(){
return $this->hasMany(ModeltestMcqQuestion::class);
}
}
另一个型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class ModeltestMcqQuestion extends Model
{
use SoftDeletes;
protected $fillable = [
'modeltest_question_set_id',
'chapterwise_mcq_id',
];
public function modeltestQuestionSet(){
return $this->belongsTo(ModeltestQuestionSet::class);
}
public function chapterwiseMcqs(){
return $this->belongsTo(ChapterwiseMcq::class);
}
}
迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateChapterwiseMCQSTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('chapterwise_mcqs', function (Blueprint $table) {
$table->id();
$table->foreignId('topic_id')->constrained()->onDelete('cascade')->onUpdate('cascade');
$table->integer('question_type');
$table->string('question');
$table->text('question_description')->nullable();
$table->string('question_resource')->nullable();
$table->string('a');
$table->string('b');
$table->string('c');
$table->string('d');
$table->string('answer');
$table->text('answer_description')->nullable();
$table->string('answer_resource')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('chapterwise_m_c_q_s');
}
}
另一个迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateModeltestMcqQuestionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('modeltest_mcq_questions', function (Blueprint $table) {
$table->id();
$table->foreignId('modeltest_question_set_id')->constrained()->onUpdate('cascade')->onDelete('cascade');
$table->foreignId('chapterwise_mcq_id')->constrained()->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('modeltest_mcq_questions');
}
}
所有关系查询都运行良好,除了App\ModeltestMcqQuestion::find(id)->chapterwiseMcqs
. 谁能帮我代码有什么问题?这里,id
表示模型的 id。
解决方案
我找到了解决方案。我认为定义函数名称会导致问题。
public function chapterwiseMcqs()
Thu 函数名应该是chapterwiseMcq()
,不是chapterwiseMcqs()
。更改名称对我有用。
推荐阅读
- jquery - 使用 jQuery-Validation-Unobtrusive 时如何使用 ajax 提交表单
- jquery - Select all child elements of body with 6 degrees of separation
- c# - Is there a way to manually start BackgroundService in Asp.Net core 3.1
- r - Subset a data frame into 2 data frames based on breaks in the tables
- javascript - How to incorporate heatmaps with addTimeline using leaflet in R
- python - Is Python's gzip.writer known to be slow?
- javascript - 单击计算按钮后,javascript代码不显示未来值的答案
- android - LifecycleObserver doesn't call onPause event on custom View - Android
- datatable - Dividing within a column based on other columns matching
- pine-script - previous quarter high and low in Pine script?