简体   繁体   中英

Eloquent relation query returning null value

I am trying to get some relational data. All the methods work well but only one method is not working. I am trying to call App\ModeltestMcqQuestion::find(id)->chapterwiseMcqs from tinker, but it is returning null. My database is well populated. I have tried with many id, but nothing is working.

My Models are:

<?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);
    }

}

Another Model:

<?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);
    }
}

Migrations:

<?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');
    }
}

Another migration:

<?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');
    }
}

All the relational queries work well except App\ModeltestMcqQuestion::find(id)->chapterwiseMcqs . Can anyone help me what is wrong with the code? Here, id represents id of the model.

I have found a solution. Defining the funtion name caused the problem, I think.

public function chapterwiseMcqs()

Thu function name should be chapterwiseMcq() , not chapterwiseMcqs() . Changing the name worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM