0
votes

What's wrong here save Null column image in table but other value is okay only image value is saving Null why??

public function store(Request $request)[enter image description here][1]
    {
            $govOffice = new GovOffice;

            $govOffice->type=Input::get('type');
            $govOffice->contact=Input::get('contact');
            $govOffice->details=Input::get('details');
            $govOffice->url=Input::get('url');
        if (Input::hasFile('image')) 
        {
            $file = Input::file('image');  
            $file->move(public_path(). '/', $file->getClientOriginalName());

            $govOffice->image = $file->getClientOriginalName();
        }  

        $govOffice->save();
        return 'save';
   }
4
Could verify that the $file variable is returning some information.rikardo_paiva
what should i do here ? why don't save the information in table accept image all value is saved in table but not image name and extension saved what the wrongajay

4 Answers

0
votes

You had mistake when you use "Input::hasFile". you need to use "Request::hasFile".

So code example below:

if (Request::hasFile('image'))
{
    $file = Input::file('image');
    $govOffice->image = $file->getClientOriginalName();
    $file->move(public_path(). '/', $file->getClientOriginalName());
}
0
votes

I noticed you are not taking into account image extension anywhere. Try this code. I've modified it to consider filename with extension.

public function store(Request $request){
    $govOffice = new GovOffice;
    $govOffice->type = $request->type;
    $govOffice->contact = $request->contact;
    $govOffice->details = $request->details;
    $govOffice->url = $request->url;

    if( $request->hasFile('image')) {
        $file = $request->file('image');
        $image_name = $file->getClientOriginalName();   //eg: flower
        $image_extension = $file->getClientOriginalExtension(); //eg: jpg
        $filename = $image_name . '.' . $image_extension;   //eg: flower.jpg
        $file->move(public_path(). '/', $filename);

        $govOffice->image = $filename;
    }

    $govOffice->save();
    return 'save';
}

Hope this is helpful.

0
votes

You should try this easily store image in your table like:

public function store(Request $request)
{
  $govOffice = new GovOffice;

  $govOffice->type=Input::get('type');
  $govOffice->contact=Input::get('contact');
  $govOffice->details=Input::get('details');
  $govOffice->url=Input::get('url');
  if ($request->hasFile('image')) 
  {
    $file = Input::file('image'); 
    //Name of you image name
    $name = $file->getClientOriginalName(); 
    $file->move(public_path(). '/', $name);

    $govOffice->image = $name;
  }  

  $govOffice->save();
  return 'save';
}
0
votes
$profiles = new Profile;
if(Input::hasFile('image')){
  $file = Input::file('image');
  $file->move(public_path().'/uploads/',
  $file->getClientOriginalName());
  $url = URL::to("/").'/uploads/'.$file->getClientOriginalName();
}
$profiles->image = $url;
$profiles->save();