2017-02-15 11 views
0

の2つのコントローラへの1つの書式サブミディションなので、私のデータベースには2つのテーブルがあります。例の学生と趣味です。 これは、StudentControllerとHobbyControllerだけでなく2つのコントローラも意味します。 と同様に2つのモデル。laravel

私はそれが例えば取る形があります。

  • 1.student名

  • 2.age

  • 3.height

  • 4.weight

  • 5bmi

  • 6.hobby

  • 7.schedule

  • 8.intensity

  • 9.diet

最初の5つは 及び6- studentcontrollerために行かなければなりません9はホビーコントローラに行く.. どのように私はこれを行う恥?私は2つの異なるフォームがほしいとは思わない...

+0

[アクション](https://laravel.com/docs/5.4/responses#redirecting-controller-actions)メソッドへのリダイレクトを使用することができます。または、そのロジックをサービス – jarguez01

+0

thxに抽出して、提案用に送信することができます.going努力のためにサービス – Mikethetechy

答えて

0

これは最良の答えではないかもしれないが、コントローラに渡して複数のリポジトリにデータを渡すためにシングルフォームを使うことができる。

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 idhobby最も単純に渡したい場合は、次のドキュメントを参照してください詳細については

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ストアを変更する必要があります。

これは、拡張された問題を解決する可能性があります。

+0

thxxxに抽出してください!この方法はエキサイティングです!私は私の前の質問からあなたが答えたことを学んだ...私はそれを試してみる前に...私は私のフォームで... probum型を持っています...私は列挙型のテーブルの列を持っています。私はフォームを提出すると、それは私にデータを切り捨てたエラーを与える..任意のアイデア? – Mikethetechy

+0

@paladin ...列挙型のデータ型を修正しました。気にする必要はありません。D – Mikethetechy

+0

Okey great。 ;)あなたはすぐに最高の道を見つけることを願っています:D – PaladiN