我工作的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);
}
}
() - >第(),只需使用 - >第()。另一件事,你從哪裏得到company_id和ad_id? –
@Sagar get() - > first()沒有理由只是老派的東西,我也更新了我的代碼,因爲它現在在我的系統中。 – djhru
但是沒有包含ad_id的數據?你會從哪裏得到? –