我要做的是通過表格創建一個家庭,然后保存這個家庭,然后我們將重定向到一個未成年學生表格,您可以在其中創建一個鏈接到您之前創建的家庭的學生。
我的模型上的一切,遷移都可以,因為在我的控制器中我可以看到學生和家庭,但我的問題是當我創建家庭時,我想將表“家庭”的“id”傳遞給具有在它的表中,外鍵'familly_id'......但我不知道如何在我的控制器上做到這一點。感謝您未來的幫助。
else {
$minorstudent = new Minorstudent;
$minorstudent->first_name = $request->first_name;
$minorstudent->last_name = $request->last_name;
$minorstudent->phone_number = $request->phone_number;
$minorstudent->street_address = $request->street_address;
$minorstudent->postal_code = $request->postal_code;
$minorstudent->city = $request->city;
$minorstudent->email_address = $request->email_address;
$minorstudent->level = $request->level;
if ($request->school!="none") {
$minorstudent->school()->associate($request->school);
}
$minorstudent->save();
return redirect('/familly');
}
我想要在傳遞給未成年學生之前創建的家庭的“id”到作為外鍵的“familly_id”中。
else {
$minorstudent = Minorstudent::where('id',$request->id)->first();
if ( $request->get('add_student_next') !== null ) {
$familly = new Familly;
$familly->first_name = $request->first_name;
$familly->last_name = $request->last_name;
$familly->phone_number = $request->phone_number;
$familly->street_address = $request->street_address;
$familly->postal_code = $request->postal_code;
$familly->city = $request->city;
$familly->email_address = $request->email_address;
$familly->absence = 1;
$familly->rules = 1;
$minorstudent->familly_id()->attach($familly);
$familly->save();
return redirect("/familly/id/student/new");
}
這是您創建家庭的家庭控制器(表格),之后您將重定向到未成年學生表格
一開始不要擔心其他
uj5u.com熱心網友回復:
創建 Family 后,您只需將 family_id 傳遞給 student 表單并將 family_id 保存在 student 表中(假設 family 和 student 是兩個表)。
$familly->save();
return redirect()->route( 'familly.student.new' )->with( [ 'id' => $familly->id ] );
我寫 $familly->id 的原因是因為在您的遷移中,id 將是家庭的主鍵。
您還希望在家庭和學生之間創建關系(假設家庭和學生是兩個表)以干凈地訪問資料。在你的家庭模型中寫
class Family extends Model
{
/**
* Get the student for the family.
*/
public function students()
{
return $this->hasMany(Student::class);
}
}
在您的學生模型中
class Student extends Model
{
/**
* Get the family that has students.
*/
public function family()
{
return $this->belongsTo(Family::class);
}
}
此外,將資料庫部分移動到模型中。
uj5u.com熱心網友回復:
在第二個 else 而不是這一行:
$minorstudent->familly_id()->attach($familly);
嘗試這個:
$minorstudent->update(['familly_id' => $family->id]);
uj5u.com熱心網友回復:
感謝所有幫助過我的人!
解決方案是:
FamillyController:這是接收我之前創建的家庭 id 的函式:
else {
$minorstudent = new Minorstudent;
$minorstudent->familly()->associate($id);
$minorstudent->update(["familly_id" => $id]);
$minorstudent->first_name = $request->first_name;
$minorstudent->last_name = $request->last_name;
$minorstudent->phone_number = $request->phone_number;
$minorstudent->street_address = $request->street_address;
$minorstudent->postal_code = $request->postal_code;
$minorstudent->city = $request->city;
$minorstudent->email_address = $request->email_address;
$minorstudent->level = $request->level;
$minorstudent->option = $request->option;
if ($request->school!="none") {
$minorstudent->school()->associate($request->school);
}
$minorstudent->save();
return redirect('/familly');
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/451165.html
