Page 1 of 1

where did the class validate come from ??

Posted: Mon Jan 09, 2017 9:57 am
by gautamz07
I was just going through this article here on how to upload images in laravel: http://devartisans.com/articles/image-upload-laravel-5 .

I see the below function in the article:

Code: Select all

public function store(Request $request)
	{
        $image = new Image();
        $this->validate($request, [
            'title' => 'required',
            'image' => 'required'
        ]);
        $image->title = $request->title;
        $image->description = $request->description;
		if($request->hasFile('image')) {
            $file = Input::file('image');
            //getting timestamp
            $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
            
            $name = $timestamp. '-' .$file->getClientOriginalName();
            
            $image->filePath = $name;

            $file->move(public_path().'/images/', $name);
        }
        $image->save();
        return $this->create()->with('success', 'Image Uploaded Successfully');
	}
I don't quite understand the below method:

Code: Select all

 $this->validate($request, [
            'title' => 'required',
            'image' => 'required'
  ]);
where did the validate method come into the class ??

I believe that line of code is checking if the fields title and image are populated , but where did the class validate come from ??

Thank you.
Gautam.

Re: where did the class validate come from ??

Posted: Mon Jan 09, 2017 10:24 am
by requinix
It's a method on the parent Controller class (or perhaps a superclass of it). I'm not sure where Controller is defined.

Re: where did the class validate come from ??

Posted: Mon Jan 09, 2017 1:00 pm
by Celauran
https://laravel.com/api/5.3/Illuminate/ ... uests.html

It's a trait used by the base controller.

Re: where did the class validate come from ??

Posted: Mon Jan 09, 2017 11:08 pm
by gautamz07
Thanks guys ! :)