2017-09-19 14 views
0

私は2つのテーブルdonoraddressを持っています。Laravelのコントローラから1つ以上のテーブルにデータを追加

donorのテーブルとアドレスにすべての基本的な詳細をドナーidのaddressテーブルに挿入したいと思います。 addressテーブルに詳細を入力する方法

donor

address

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\Donor; 

class DonorController extends Controller 
{ 
    /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     return view('donor.index'); 
    } 

    /** 
    * Show the form for creating a new resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function create() 
    { 
     // 
    } 

    /** 
    * Store a newly created resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function store(Request $request) 
    { 
     $donor = new Donor; 
     $donor->salutation = $request->input('salutation'); 
     $donor->gender = $request->input('gender'); 
     $donor->first_name = $request->input('first_name'); 
     $donor->last_name = $request->input('last_name'); 
     $donor->phone = $request->input('phone_home'); 
     $donor->mobile = $request->input('phone_mobile'); 
     $donor->email = $request->input('email'); 
     $donor->occupation = $request->input('occupation'); 
     $donor->is_active = $request->input('status'); 
     $donor->is_deleted = 0; 
     $donor->created_by = 1; 
     $donor->updated_by = 1; 
     $donor->save(); 


     return redirect('/donor')->with('success', 'Hurray!! New donor Created.'); 
    } 

    /** 
    * Display the specified resource. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function show($id) 
    { 
     // 
    } 

    /** 
    * Show the form for editing the specified resource. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function edit($id) 
    { 
     // 
    } 

    /** 
    * Update the specified resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function update(Request $request, $id) 
    { 
     // 
    } 

    /** 
    * Remove the specified resource from storage. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function destroy($id) 
    { 
     // 
    } 
} 

+0

これはあまり明確ではなく、保留になる可能性があります。 – halfer

答えて

1

寄付者に住所があり、その住所が独自の表を持つ一意の項目である場合は、住所用のモデルを作成し、これらの2つを一緒に接続するために雄弁な関係を使用する必要があります。

public function address(){ 
    return $this->hasOne('App\Address'); 
} 

そして、あなたのアドレスクラスに:

あなたはだからあなたのドナーモデルでは、あなたがこのような何かを行くと雄弁関係

hereについての詳細を読むことができるように

public function donor(){ 
    $this->belongsTo('App\Donor'); 
} 

あなたのドナーコントローラーでは、住所の新しいインスタンスを作成し、それをドナーに接続します:

$address = new App\Address; 
... Your stuff to populate the address 
$donor->address()->save($address); 

注:これはあなたが望むものを達成するためのコード全体ではなく、メモリから書きました。あなたはそれを理解するためにいくつかの研究を行うべきです。

関連する問題