从 select 多个数据库列中保存多个数据
Save multiple data in database column from select multiple
我想在一个学生下保存不止一所大学。对于 html select 我使用了
<select class="selectpicker" multiple data-live-search="true" name="university">
@foreach($districts as $row)
<option value= {{$row->name}} > {{$row->name}} </option>
@endforeach
</select>
这次考取以前保存的大学。我把学区当成大学。
控制器->
public function store(Request $request)
{
Seller_info::create([
'name' => $request['name'],
'email' => $request['email'],
'passport' => $request['passport'],
'phone_number' => $request['phone_number'],
'address' => $request['address'],
'dateofbirth' => $request['dateofbirth'],
'ielts' => $request['ielts'],
'openingcharge' => $request['openingcharge'],
'serviceCharge' => $request['serviceCharge'],
'applydate' => $request['applydate'],
'visaStatus'=> $request['visaStatus'],
'country' => $request['country'],
'university'=> $request['university'],
]);
return back();
}
这是在救学生。这是一个混乱的代码,我对此感到抱歉。我正在为快速学习而努力。这里我使用 seller_info 作为学生。
数据正在保存大学,但数据库中只有一所大学。我试图在存储的情况下使用 for 循环。但无法正确实施。
我的table文件->
Schema::create('seller_infos', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('email')->unique();
$table->string('passport')->nullable();
$table->string('phone_number')->unique();
$table->string('address')->nullable();
$table->string('dateofbirth')->nullable();
$table->string('ielts')->nullable();
$table->string('openingcharge')->nullable();
$table->string('serviceCharge')->nullable();
$table->string('applydate')->nullable();
$table->string('visaStatus')->nullable();
$table->string('country')->nullable();
$table->string('university')->nullable();
$table->timestamps();
});
和型号
protected $fillable = [
'name', 'email', 'passport', 'phone_number','address','dateofbirth','ielts', 'openingcharge','serviceCharge','applydate',
'visaStatus','country','university'
];
如果您想为一个学生存储多所大学的数据,首先您需要 3 table、学生(因此您的卖家信息)、大学和 seller_info_university(将成为枢轴 table),
seller_info_university迁移一定要像;
Schema::create('seller_info_university', function (Blueprint $table) {
$table->bigInteger('seller_info_id')->unsigned();
$table->bigInteger('university_id')->unsigned();
});
将此代码添加到您的 SellerInfo 模型中;
public function universities()
{
return $this->belongsToMany(University::class); // Your University model
}
将此代码添加到您的大学模型中;
public function students()
{
return $this->belongsToMany(SellerInfo::class); // Your Student(SellerInfo) model
}
在你的控制器中试试这个
$student = Seller_info::create([
'name' => $request['name'],
'email' => $request['email'],
'passport' => $request['passport'],
'phone_number' => $request['phone_number'],
'address' => $request['address'],
'dateofbirth' => $request['dateofbirth'],
'ielts' => $request['ielts'],
'openingcharge' => $request['openingcharge'],
'serviceCharge' => $request['serviceCharge'],
'applydate' => $request['applydate'],
'visaStatus'=> $request['visaStatus'],
'country' => $request['country'],
// 'university'=> $request['university'], you dont need there anymore
]);
$student->universities()->attach($request['universities']); // You can use attach(), detach() or sync() functions, you can search in Laravel Documentation
return back();
并查看;
<select class="selectpicker" multiple data-live-search="true" name="universities[]">
@foreach($districts as $row)
<option value= {{$row->name}} > {{$row->name}} </option>
@endforeach
</select>
我想在一个学生下保存不止一所大学。对于 html select 我使用了
<select class="selectpicker" multiple data-live-search="true" name="university">
@foreach($districts as $row)
<option value= {{$row->name}} > {{$row->name}} </option>
@endforeach
</select>
这次考取以前保存的大学。我把学区当成大学。
控制器->
public function store(Request $request)
{
Seller_info::create([
'name' => $request['name'],
'email' => $request['email'],
'passport' => $request['passport'],
'phone_number' => $request['phone_number'],
'address' => $request['address'],
'dateofbirth' => $request['dateofbirth'],
'ielts' => $request['ielts'],
'openingcharge' => $request['openingcharge'],
'serviceCharge' => $request['serviceCharge'],
'applydate' => $request['applydate'],
'visaStatus'=> $request['visaStatus'],
'country' => $request['country'],
'university'=> $request['university'],
]);
return back();
}
这是在救学生。这是一个混乱的代码,我对此感到抱歉。我正在为快速学习而努力。这里我使用 seller_info 作为学生。
数据正在保存大学,但数据库中只有一所大学。我试图在存储的情况下使用 for 循环。但无法正确实施。
我的table文件->
Schema::create('seller_infos', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('email')->unique();
$table->string('passport')->nullable();
$table->string('phone_number')->unique();
$table->string('address')->nullable();
$table->string('dateofbirth')->nullable();
$table->string('ielts')->nullable();
$table->string('openingcharge')->nullable();
$table->string('serviceCharge')->nullable();
$table->string('applydate')->nullable();
$table->string('visaStatus')->nullable();
$table->string('country')->nullable();
$table->string('university')->nullable();
$table->timestamps();
});
和型号
protected $fillable = [
'name', 'email', 'passport', 'phone_number','address','dateofbirth','ielts', 'openingcharge','serviceCharge','applydate',
'visaStatus','country','university'
];
如果您想为一个学生存储多所大学的数据,首先您需要 3 table、学生(因此您的卖家信息)、大学和 seller_info_university(将成为枢轴 table),
seller_info_university迁移一定要像;
Schema::create('seller_info_university', function (Blueprint $table) {
$table->bigInteger('seller_info_id')->unsigned();
$table->bigInteger('university_id')->unsigned();
});
将此代码添加到您的 SellerInfo 模型中;
public function universities()
{
return $this->belongsToMany(University::class); // Your University model
}
将此代码添加到您的大学模型中;
public function students()
{
return $this->belongsToMany(SellerInfo::class); // Your Student(SellerInfo) model
}
在你的控制器中试试这个
$student = Seller_info::create([
'name' => $request['name'],
'email' => $request['email'],
'passport' => $request['passport'],
'phone_number' => $request['phone_number'],
'address' => $request['address'],
'dateofbirth' => $request['dateofbirth'],
'ielts' => $request['ielts'],
'openingcharge' => $request['openingcharge'],
'serviceCharge' => $request['serviceCharge'],
'applydate' => $request['applydate'],
'visaStatus'=> $request['visaStatus'],
'country' => $request['country'],
// 'university'=> $request['university'], you dont need there anymore
]);
$student->universities()->attach($request['universities']); // You can use attach(), detach() or sync() functions, you can search in Laravel Documentation
return back();
并查看;
<select class="selectpicker" multiple data-live-search="true" name="universities[]">
@foreach($districts as $row)
<option value= {{$row->name}} > {{$row->name}} </option>
@endforeach
</select>