你好我正在尝试用 laravel 做一个测验应用程序并且我正在努力检索我的类别问题..
我对此有一个难看的解决方案,但它不是动态的..
public function startQuiz(Request $request){
$questions = Question::all();
foreach ($questions as $key => $question) {
dd($question->where('categories_id', 'LIKE', 2)->get()); // i want to change the 2 to the real category id
}
}
我以为我可以通过 $question->category->id 之类的关系来解决这个问题,但行不通。
那是我的模型:
class Question extends Model
{
protected $fillable = ['question_text', 'categories_id', 'correct_answer', 'options'];
protected $casts = ['options' => 'array'];
public function category(){
return $this->belongsTo(Category::class, 'categories_id');
}
}
class Category extends Model
{
protected $fillable = ['name', 'slug'];
public function question()
{
return $this->hasMany(Question::class, 'questions_id');
}
我不能以某种方式传递 id 并检查它我不知道为什么..那是我传递类别的表格:
@section('content-categories')
<div class="card">
<div class="card-header">Choose Category to Start</div>
<div class="card-body">
@foreach($categories as $category)
<form class="form-group" action="{{route('user.quiz', $category->slug)}}" method="post">
@csrf
<input type="submit" name="categoryTest" class="form-control" value="{{$category->name}}">
</form>
@endforeach
</div>
</div>
@endsection
红糖糍粑
猛跑小猪