Home > front end >  In a relation, how can I get the data of table A but not have in table B?
In a relation, how can I get the data of table A but not have in table B?

Time:07-01

I have:

Questions table:

id question name
1 1 1 ?
2 2 2 = ?

Options table

id option name question_id is_correct
1 22 1 0
2 33 1 0

Model question:

class question extends Model{
use HasFactory;
public function options()
{
    return $this->hasMany(option::class, 'question_id', 'id');
}
}
  1. How to get the questions in the "questions" table but in the "options" table there are no options?
  2. How to get the questions in the "question" table but in the "options" table there is no correct option?

Thanks

CodePudding user response:

not sure if I understand your question correctly but

In your controller

$options = Options::with('question')->get();

In blade:


@foreach ($options as $option);

    {{$option->question->name}}

@endforeach

Kr

  • Related