Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 validation in model

I have model like this

class test extends Model
{

public   $rules = [
    'title' => 'required',
    'name' => 'required',
];
protected $fillable = ['title','name'];
}

And controller like this

public function store(Request $request)
{
    $test=new test; /// create model object
        $validator = Validator::make($request->all(), [
             $test->rules
        ]);
        if ($validator->fails()) {
            return view('test')->withErrors($validator)
        }
        test::create($request->all());
 }

Validation show error like this

The 0 field is required.

I want show this

The name field is required.
The title field is required.

like image 601
paranoid Avatar asked May 15 '16 15:05

paranoid


1 Answers

I solve it

public function store(Request $request)
{
  $test=new test; /// create model object
    $validator = Validator::make($request->all(),$test->rules);
    if ($validator->fails()) {
        return view('test')->withErrors($validator)
    }
    test::create($request->all());
}
like image 59
paranoid Avatar answered Sep 20 '22 12:09

paranoid