これは最良の答えではないかもしれないが、コントローラに渡して複数のリポジトリにデータを渡すためにシングルフォームを使うことができる。
route.php
Route::resource('student', 'StudentController');
StudentController.php
public function __constructor(StudentRepository $student, HobbyRepository $hobby)
{
$this->student = $student;
$this->hobby= $hobby;
}
public function store(Request $request)
{
$data = $request->all();
$hobby = [
'hobby' => $data['hobby'],
'schedule' => $data['schedule'],
'intensity' => $data['intensity'],
'diet' => $data['diet'],
];
$student = [
'student_name' => $data['student_name'],
'age' => $data['age'],
'height' => $data['height'],
'weight' => $data['weight'],
'bmi' => $data['bmi'],
];
$this->student->store($student);
$this->hobby->store($hobby);
//your other codes.
}
StudentRepository.php
public function store($data)
{
// your implementation on storing the user.
}
HobbyRepository.php
public function store($data)
{
// your implementation on storing the hobby.
}
あなたは、コントローラからデータを渡すために任意のメソッドや変数を使用することができます。お役に立てれば。
編集:保存し、情報を取得する上で、あなたの拡張のために質問
。
https://laravel.com/docs/5.3/eloquent#inserts
をあなたはstudent id
hobby
最も単純に渡したい場合は、次のドキュメントを参照してください詳細については
The create method returns the saved model instance:
$flight = App\Flight::create(['name' => 'Flight 10']);
:ドキュメントに記載されているように
方法はStudentRepository
から学生を戻してに渡すことです0。
例えば:
StudentRepository.php
public function store($data)
{
// your implementation on storing the user.
$student = [] // array of the student informations to be stored.
return Student::create($student); //you will have student information here.
}
StudentController.php
$student = $this->student->store($student); //store the student information and get the student instance.
$this->hobby->store($hobby, $student->id); //pass it to the hobby to store id.
あなたはstudent id
を使用するhobbyRepository
ストアを変更する必要があります。
これは、拡張された問題を解決する可能性があります。
[アクション](https://laravel.com/docs/5.4/responses#redirecting-controller-actions)メソッドへのリダイレクトを使用することができます。または、そのロジックをサービス – jarguez01
thxに抽出して、提案用に送信することができます.going努力のためにサービス – Mikethetechy