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');
}
}
- How to get the questions in the "questions" table but in the "options" table there are no options?
- 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
