点击按钮

时间:2017-05-30 02:16:19

标签: php laravel laravel-5

我正在使用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);
    }
}

2 个答案:

答案 0 :(得分:1)

因为你的刀片是这样的:

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

你的路线应该是

Route::get('apply/{id}', 'ApplyController@apply')->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,
    ]);
}

unique:apply - 表示它将检查user_id已经应用的apply表。

然后在上面的代码中做

$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
}

然后检索数据,假设apply已经属于用户以及用户hasOne apply

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

答案 1 :(得分:0)

你的路线应该是:

Route::post('apply/{$user_id}/{company_id}/{ad_id}','ApplyController@apply');

我认为您已经为广告制作了模型。

所以,只需保存这样的数据:

你的功能就像

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
}

还有一件事,你的帖子请求中应该有ad_id。