当管理员在<select></select>
中选择一个类别时我想获取其ID以将其插入问题表
表格问题:
Schema::create('questions', function (Blueprint $table) {
$table->increments('id');
$table->string('question_text');
$table->string('type');
$table->integer('points');
$table->integer('temps_reponse');
$table->integer('categories_id')->unsigned();
$table->foreign('categories_id')->references('id')->on('categories');
$table->timestamps();
});
表格类别:
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('categorie');
$table->timestamps();
});
问题模型
class Question extends Model
{
public function categorie()
{
return $this->belongsTo(Categorie::class, 'categories_id');
}
}
分类模型
class Categorie extends Model
{
public function question()
{
return $this->hasMany(Question::class, 'question_id')->withTrashed();
}
}
我被封锁了几个小时请任何帮助来解决它
我的HTML
<label class="form-label">Choisir la categorie :</label>
<select class="" id="s2example-1" name="categorie">
<option></option>
@foreach ($categories as $categorie)
<option>{{ $categorie->categorie }}</option>
@endforeach
</select>
答案 0 :(得分:0)
为了将id而不是字符串发送回服务器,请在每个选项元素上提供value属性:<option value={{$categorie->id}}>{{ $categorie->categorie }}</option>