PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Dec 14, 2017 9:54 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Jan 09, 2017 10:57 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 331
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:

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.


Top
 Profile  
 
PostPosted: Mon Jan 09, 2017 11:24 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6604
Location: WA, USA
It's a method on the parent Controller class (or perhaps a superclass of it). I'm not sure where Controller is defined.


Top
 Profile  
 
PostPosted: Mon Jan 09, 2017 2:00 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6402
Location: Montreal, Canada
https://laravel.com/api/5.3/Illuminate/ ... uests.html

It's a trait used by the base controller.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
PostPosted: Tue Jan 10, 2017 12:08 am 
Offline
Forum Contributor
User avatar

Joined: Wed May 14, 2014 12:18 pm
Posts: 331
Thanks guys ! :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group