2017-05-30 73 views
0

我工作的laravel 5.4和我有這樣的代碼:保存用戶數據

public function apply($id){ 
    $user = User::where('id', $id)->get()->first(); 
    $data = [ 
     'name' => $user->first_name, 
     'family' => $user->last_name, 
     'email' => $user->email, 
     'username' => $user->username, 
     'gender' => $user->gender, 
     'birthday' => $user->birthday, 
     'cv' => $user->cv, 
     'about' => $user->about, 
     'education' => $user->education, 
     'experiences' => $user->experiences, 
    ]; 
    $company = Company::get()->first(); 

    Mail::send('emails.apply', $data, function ($message) use ($company) 
    { 

     $message->from('[email protected]', 'Robert Nicjoo'); 
     $message->subject('New Apply'); 
     $message->to($company->email); 

    }); 
    Mail::send('emails.uapply', $data, function ($message) use ($user) 
    { 

     $message->from('[email protected]', 'Robert Nicjoo'); 
     $message->subject('You Applied successfully'); 
     $message->to($user->email); 

    }); 

    Session::flash('success', 'Your application was sent to company.'); 
     return redirect()->back()->with('session', $data); 
    } 

這將發送電子郵件至公司時,用戶點擊應用按鈕,併發送用戶信息給他們,現在我還希望將用戶的數據包括user_id, ad_id and company_id保存在另一個表中,以便用戶和公司所有者都可以訪問其應用廣告的歷史記錄。

我也有這個表,以節省數據:

public function up() 
    { 
     Schema::create('applies', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('user_id')->unsigned(); 
      $table->integer('ad_id')->unsigned(); 
      $table->integer('company_id')->unsigned(); 
      $table->timestamps(); 
     }); 

     Schema::table('ads', function($table) { 
      $table->foreign('user_id')->references('id')->on('users'); 
      $table->foreign('ad_id')->references('id')->on('ads'); 
      $table->foreign('company_id')->references('company_id')->on('ads'); 
     }); 
    } 

但在我的控制器(第一碼),我需要知道如何保存在新表(第二碼)的信息?

更新:

廣告模式>>

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Ad extends Model 
{ 

    protected $fillable = [ 
     'company_id', 'title', 'slug', 'image', 'description', 'address', 'job_title', 'salary', 
    ]; 

    public function company(){ 
     return $this->belongsTo(Company::class); 
    } 
    public function category(){ 
     return $this->belongsTo(Category::class); 
    } 
    public function location(){ 
     return $this->belongsTo(Location::class); 
    } 
    public function employment(){ 
     return $this->belongsTo(Employment::class); 
    } 
} 
+1

() - >第(),只需使用 - >第()。另一件事,你從哪裏得到company_id和ad_id? –

+0

@Sagar get() - > first()沒有理由只是老派的東西,我也更新了我的代碼,因爲它現在在我的系統中。 – djhru

+0

但是沒有包含ad_id的數據?你會從哪裏得到? –

回答

1

,因爲你的刀片是這樣的:

<a class="btn btn-info btn-round" href="{{ route('apply.btn', Auth::user()->id) }}"> 

你的路線應該像

Route::get('apply/{id}', '[email protected]')->name('apply.btn'); 

爲什麼只有ID?因爲我們有討論,我發現ad_idcompany_id從控制器採取..然後在你的控制器這應該工作

public function apply($id) 
{ 
    $ad = Ad::first(); 
    $company = Company::first(); 
    $apply = new Apply(); 

    $apply->user_id = $id 
    $apply->ad_id = $ad->id; 
    $apply->company_id = $company->id; 
    $apply->save(); 

    // some more codes // 
} 

,以避免重複使用USER_ID ..添加校驗功能一樣

function validateApply(array $data) 
{ 
    return Validator::make($data, [ 
     'user_id'  => 'required|numeric|unique:apply,user_id,NULL,id,ad_id,'.$data->ad_id, 
    ]); 
} 

獨特:申請 - 這意味着它會檢查已經應用的USER_ID的申請表..

然後在上面的代碼只是做

$validateApply= $this->validateApply(['user_id'=>$id,'ad_id'=>$ad->id]); 
if(!$validateApply->fails()) 
{ 
    // do the above code here 
} 
else 
{ 
    // duplicate !!! so do your code here 
} 

然後檢索數據假設應用已經屬於關聯的用戶,以及用戶hasOne你爲什麼要使用get申請

Auth::user()->apply->first()->somefield; 
// im not sure how the hasOne works but try 
Auth::user()->apply->somefield; 
+0

因此,它實際上支持我的第一個代碼,除了刪除的請求,它現在的作品非常感謝,但有2個問題... 1-如何停止重複?用戶只能申請一次。 2-我無法在刀片中獲得結果。 – djhru

+0

好吧要更新我的答案.. – Demonyowh

+0

你不覺得我們應該比較嗎?例如。如果我們只檢查用戶ID,這意味着用戶只能在一個廣告中申請,而且實際上用戶應該能夠申請多少個(他)只想不到同一個廣告。我認爲,如果我們比較user_id和ad_id,那麼最好是一個用戶只能在每個廣告中應用一次,因爲原因也會檢查ad_id。如果我錯了,請告訴我 – djhru

0

路線應該是:

Route::post('apply/{$user_id}/{company_id}/{ad_id}','[email protected]'); 

我想你已經創建的模型的廣告。

所以,簡單地保存數據是這樣的:

你的功能是一樣

public function apply(Request $request){ 

    // other code 

    $apply = new Apply(); 
    $apply->user_id = $request->user_id; 
    $apply->ad_id = $request->ad_id; 
    $apply->company_id = $request->company_id; 
    $apply->save(); 

    // other code 
} 

還有一件事,你應該在你的POST請求ad_id。

+0

所以你建議我把它添加到我的廣告模型中?以及模型如何保存數據? +我不希望保存任何apply_table直到用戶點擊應用按鈕,然後獲取這些信息。例如,如果'ad 1'沒有任何用戶申請,那麼'apply_table'中不會有任何保存。我希望我解釋得很好! – djhru

+0

當用戶點擊應用按鈕,然後你必須插入數據適用和廣告表我是吧? –

+0

沒有廣告表只是'應用'表。廣告表僅用於保存廣告,如帖子。這個信息將保存在'apply'表 – djhru