我是 Laravel 的初學者,我有點困惑。所以我想要做的是通過一個表格創建一個家庭,然后保存這個家庭,然后我們被重定向到一個未成年學生表格,您可以在其中創建一個鏈接到您剛剛創建的家庭的學生。
我的模型上的一切,遷移都可以,因為在我的控制器中我可以看到學生和家庭,但我的問題是當我創建家庭時,我想將表“家庭”的“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");
}
這是您創建家庭的家庭控制器(表格),之后您將重定向到未成年學生表格
ps:不要擔心開頭的else
uj5u.com熱心網友回復:
在第二個 else 而不是這一行:
$minorstudent->familly_id()->attach($familly);
嘗試這個:
$minorstudent->update(['familly_id' => $family->id]);
uj5u.com熱心網友回復:
“在 null 上呼叫成員函式 update()”
在我的重定向中,我需要寫一些東西:
回傳重定向(“/家人/身份證/學生/新”);因為它在我的表單上重定向了我,而不是我之前創建的家庭的“id”,比如“/familly/1/student/new”“/familly/2/student/new”...
要清楚第一個表格是“newfamilly”鏈接到我之前發送的控制器。第二個表格是“newminorstudent”。
在“newminorstudent”bladeview 上,我使用了表單操作
//form action="{{ route('addchild',['id']) }}" method="POST"//
但我確定我必須寫一些東西而不是 ['id']。
Minorstudent migrations
Schema::create('minorstudents', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('familly_id');
$table->foreign('familly_id')->references('id')->on('famillies');
$table->string("first_name");
$table->string("last_name");
$table->string("email_address");
$table->string("phone_number");
$table->string("street_address");
$table->string("city");
$table->string("postal_code");
$table->integer("school_id")->nullable();
$table->string("level");
$table->enum('choices', ['Paiement en attente', 'Paiement effectué'])->default('Paiement en attente');
$table->timestamps();
});
Familly migration
Schema::create('famillies', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string("first_name");
$table->string("last_name");
$table->string("email_address");
$table->string("phone_number")->nullable();
$table->string("street_address");
$table->string("city");
$table->string("postal_code");
$table->boolean("absence");
$table->boolean("rules");
});
//
They are not on the same controller file.
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);
}
}
此外,將資料庫部分移動到模型中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434993.html
