2017-07-18 22 views
0

我想在三個不同的表中同時添加插入數據。但是,一旦數據插入到第一個表中,我需要獲取該ID並繼續將該數據插入其他兩個表中。這些是申請表,語言表(每個申請人可能知道多種語言)和技能表(也是一個或多個)。將數據添加到多個表(laravel 4)

下面是我一直在努力代碼:

型號:

class Apply extends Eloquent { 

protected $table = 'application'; 
protected $fillable = ['user_id', 'gender', 'dob', 'ic', 'marriage_status', 'phone', 'city', 'street', 'course', 'university', 
    'academic_degree', 'uni_start_date', 'uni_end_date', 'addinfo', 'working_company', 'position', 'start_date', 'end_date', 'reason']; 

public function lang() { 
    return $this->hasMany('Lang'); 
} 
} 

class Lang extends Eloquent 
{ 
    protected $table = 'language'; 

public function apply(){ 
    return $this->belongsTo('Apply'); 
} 
} 

class Skills extends Eloquent 
{ 
    protected $table = 'skills'; 

public function apply(){ 
    return $this->belongsTo('Apply'); 
} 
} 

控制器:

public function createcv() { 
    $input = Input::all(); 
    $rules = array('ic' => 'required', 
     'phone' => 'required', 
     'course' => 'required'); 
    $v = Validator::make($input, $rules); 

    if ($v->passes()) { 
     $apply = Apply::create(array('user_id' => Auth::user()->id, 'gender' => $input['gender'], 
        'dob' => $input['dob'], 'ic' => $input['ic'], 'marriage_status' => $input['mstatus'], 
        'phone' => $input['phone'], 'city' => $input['city'], 'street' => $input['street'], 
        'course' => $input['course'], 'university' => $input['institution'], 
        'academic_degree' => $input['adegree'], 'uni_start_date' => $input['uni-startdate'], 
        'uni_end_date' => $input['uni-enddate'], 'addinfo' => $input['addinfo'], 
        'working_company' => $input['jobinstitution'], 'position' => $input['jobposition'], 
        'start_date' => $input['startdate'], 'end_date' => $input['enddate'], 'reason' => $input['reason'])); 
     $apply->save(); 

     $app_id = $apply->id; 
     $lang = Lang::create(array('app_id' => $app_id, 'lang' => $input['lang1'], 
        'reading' => $input['read1'], 'writing' => $input['write1'], 
        'speaking' => $input['speak1'], 'institution' => $input['inst1'] 
     )); 
     $lang->save(); 

     $skills = Skills::create(array('app_id' => $app_id, 'prog_name' => $input['prog1'], 
        'level' => $input['level1'], 'institution' => $input['instprog1'])); 
     $skills->save(); 

     return Redirect::to('home'); 
    } else { 
     return Redirect::to('create-cv')->withInput()->withErrors($v); 
    } 
} 
+0

您的問題是什麼? –

+0

@JoseRojas謝謝你的提問。我不知道如何在同一時間在三張表上添加數據。 –

回答

0

學習碼L後發現,使L已經使用了laravel所做的Laravel關鍵字「Lang」不允許你一起工作。還有很多其他關鍵字,如: 郎,事件等不能使用。 此代碼適用於將數據添加到多個表(laravel 4)

+1

是的,您需要謹慎使用Laravel或Symfony基類名稱。它們通常被定義爲application.php文件底部的別名。它們通常在名稱空間中定義,但別名使它們更容易在不使用名稱空間的情況下調用。如果您必須爲您的應用程序使用「Lang」,則可以將'Lang' =>'Illuminate \ Support \ Facades \ Lang'更改爲其他內容。但請注意,一旦你改變了全局別名,那個別名的所有引用都必須被重構 –

+0

@ serdar.sanri那是對的 –

1

如果需要記錄ID存儲等,這些切不可同時保存,因爲當你保存數據的時候你可以檢索到這個id。

當你調用模型的save方法,你可以檢索數據庫中,如果您的數據具有相同的密鑰($鍵=> $值)列的名稱,你可以使用創建記錄的id方法。

從Laravel頁:

您還可以使用create方法來保存新模式在一行。插入的模型實例將從方法返回給您。但是,在這樣做之前,您需要在模型上指定可填寫或保護的屬性,因爲所有Eloquent模型都可以防止大規模分配。

利用該模型建立方法

// Create a new user in the database... 
$user = User::create(array('name' => 'John')); 

// Retrieve the user by the attributes, or create it if it doesn't exist... 
$user = User::firstOrCreate(array('name' => 'John')); 

// Retrieve the user by the attributes, or instantiate a new instance... 
$user = User::firstOrNew(array('name' => 'John')); 

所以,你的代碼應該是這樣的:

$apply = new Apply(); 
... 
$apply->save(); 
$id_of_your_apply = $apply->id; 
+0

比我如何在我的代碼中應用這個//在數據庫中創建一個新用戶... $ user = User :: create(array('name'=>'John')); –

+0

你可以看看編輯後的代碼。我試圖使用create方法,但我得到錯誤//調用未定義的方法Illuminate \ Translation \ Translator :: create() –

0

https://laravel.com/docs/4.2/eloquent#insert-update-delete

public function createcv(){ 
    $input = Input::all(); 

    $rules = array('ic' => 'required', 
     'phone' => 'required', 
     'course' => 'required'); 
    $v = Validator::make($input, $rules); 

    if ($v->passes()) { 
     $apply = Apply::create(array('user_id' => Auth::user()->id,'gender' => $input['gender'], 'dob' => $input['dob'], 'ic' => $input['ic'], 'marriage_status' => $input['mstatus'], 'phone' => $input['phone'],'city' => $input['city'],'street'=> $input['street'], 'course' => $input['course'], 'university' => $input['institution'], 'academic_degree' => $input['adegree'],'uni_start_date' => $input['uni-startdate'], 'uni_end_date' => $input['uni-enddate'],'addinfo' => $input['addinfo'], 'working_company' => $input['jobinstitution'],'position' => $input['jobposition'], 'start_date' => $input['startdate'],'end_date' => $input['enddate'], 'reason' => $input['reason'])); 
     $apply->save(); 

     **$app_id = $apply->id;** 

     $lang = Lang::create(array('app_id' => $app_id,'lang' => $input['lang1'], 
      'reading' => $input['read1'], 'writing' => $input['write1'], 
      'speaking' => $input['speak1'],'institution' => $input['inst1'] 
      )); 
     $lang->save(); 

     $skills = Skills::create(array('app_id' => $app_id,'prog_name' => $input['prog1'], 'level' => $input['level1'], 'institution' => $input['inst1'])); 
     $skills->save(); 

     return Redirect::to('home'); 
} 
else { 
     return Redirect::to('create-cv')->withInput()->withErrors($v); 
    } 
} 
+0

它不允許我這樣做,我不斷收到此錯誤。 //調用未定義的方法Illuminate \ Translation \ Translator :: create() –