【发布时间】:2017-05-30 02:16:19
【问题描述】:
我正在使用 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('noreply@gmail.com', 'Robert Nicjoo');
$message->subject('New Apply');
$message->to($company->email);
});
Mail::send('emails.uapply', $data, function ($message) use ($user)
{
$message->from('noreply@gmail.com', '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);
}
}
【问题讨论】:
-
你为什么用get()->first(),直接用->first()。另一件事,您从哪里获得 company_id 和 ad_id ?
-
@Sagar get()->first() 没有理由只是老派的东西,我也更新了我的代码,因为它现在在我的系统中。
-
但是没有包含 ad_id 的数据?你会从哪里得到?
-
好的,该按钮位于
ad(发布)页面内,该页面从另一个控制器处理并且数据已经传递给该控制器,因此当用户单击此按钮时,它确切地知道向哪个公司发送电子邮件the ad owner company所以我的页面中已经有那些id's也许我可以在这里使用它们,或者如果有必要我也可以在这里添加它。 (这是我的问题如何获取和传递数据) -
你能把你的路由添加到这个函数吗?