Property [id] does not exist on this collection instance Laravel 5.6

0

I want to ask for a little help, maybe your critical eye can help me solve that, I'm working with 2 tables, which have these migrations: Table questions:

 Schema::create('questions', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('topic_id')->unsigned();
        $table->text('question_text');
        $table->timestamps();
    });
    Schema::table('questions', function(Blueprint $table){
       $table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade')->onUpdate('cascade');
    });

Table options:

 Schema::create('question_options', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('question_id')->unsigned();
        $table->string('option');
        $table->tinyInteger('correct')->nullable()->default(0);
        $table->timestamps();
    });
    Schema::table('question_options', function (Blueprint $table){
        $table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade')->onUpdate('cascade');
    });

The respective models are: Questions:

    class Question extends Model
{
    protected $fillable = array('topic_id', 'question_text');
    public function topic(){
        return $this->belongsTo(Topic::class);
    }
    public function option(){
        return $this->hasMany(QuestionOption::class);
    }
}

Options model:

    class QuestionOption extends Model
{
    protected $fillable = array('question_id', 'option', 'correct');

    public function question(){
        return $this->belongsTo(Question::class);
    }
}

For example, if I call all options:

$quesOp = QuestionOption::all();
  Output: Toda la lista

But if I want to use the relationship with questions and see for example the question in this way:

  foreach($quesOp->option(Esa es la relación que está en el modelo) as $question){
 echo $question->question_text;
}

I get the error of Property does not exist on this collection instance.

I think I've done the steps well, that's why I do not explain the error, in case you can help me I'll be very grateful!

    
asked by Claudio Orellana 11.09.2018 в 04:55
source

0 answers