PHP Developers Network
http://forums.devnetwork.net/

where did the class validate come from ??
http://forums.devnetwork.net/viewtopic.php?f=72&t=143273
Page 1 of 1

Author:  gautamz07 [ Mon Jan 09, 2017 10:57 am ]
Post subject:  where did the class validate come from ??

I was just going through this article here on how to upload images in laravel: .

I see the below function in the article:

Syntax: [ Download ] [ Hide ]
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:

Syntax: [ Download ] [ Hide ]
 $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.

Author:  requinix [ Mon Jan 09, 2017 11:24 am ]
Post subject:  Re: where did the class validate come from ??

It's a method on the parent Controller class (or perhaps a superclass of it). I'm not sure where Controller is defined.

Author:  Celauran [ Mon Jan 09, 2017 2:00 pm ]
Post subject:  Re: where did the class validate come from ??

https://laravel.com/api/5.3/Illuminate/ ... uests.html

It's a trait used by the base controller.

Author:  gautamz07 [ Tue Jan 10, 2017 12:08 am ]
Post subject:  Re: where did the class validate come from ??

Thanks guys ! :)

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/