Laravel验证列中的唯一值为另一个

时间:2016-08-06 16:50:23

标签: php validation laravel-5.2

我正在尝试在我的请求中设置验证规则,以便names years Laravel 5.2中的<?php namespace App\Http\Requests; use Auth; use App\Http\Requests\Request; class AwardeeRequest extends Request { public function rules() { return [ 'name'=>'required', 'year'=>'required', ]; } } 唯一。

到目前为止,我有这个请求:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Awardee;
use App\Http\Requests;

class AwardeeController extends Controller
{
.
.
    public function store(Requests\AwardeeRequest $request)
    {
        $this->validate($request, [
            'name' => 'unique:awardee,name,NULL,year,year,' .$request->get('year'),
        ]);

        return redirect(route('awardees.index'));
    }

    public function update(Requests\AwardeeRequest $request, $id)
    {
        $awardee = Awardee::findOrFail($id);
        $this->validate($request, [
            'name' => 'unique:awardee,name,NULL,year,year,' .$request->get('year').',id,id,'.$awardee->id,
        ]);
        $awardee->fill($request->all())->save();

        return redirect(route('awardees.index'));
    }

}

我的控制员:

  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX uni:<http://www.owl-ontologies.com/aa.owl#>
select * where { 
  ?x uni:hasSibling  ?y .
}

我尝试了这里建议的内容:Laravel 4: Unique Validation for Multiple Columns但是当我更新年份时它失败了。

1 个答案:

答案 0 :(得分:0)

原来我过度思考了。

这是最终的代码。我将验证移回了请求规则。

<?php

namespace App\Http\Requests;
use Auth;
use App\Http\Requests\Request;

class AwardeeRequest extends Request
{
    public function rules()
    {
        return [
            'year'=>'required',
            'name'=>'required|unique:awardee,name,'.$this->get('id').',year,year,' .$this->get('year'),
        ];
    }
}