使用laravel

时间:2017-08-25 19:05:08

标签: database image laravel

这里有什么错误保存表中的空列图像,但其他值没关系只有图像值保存为空?为什么?

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 个答案:

答案 0 :(得分:0)

使用“Input :: hasFile”时出错了。你需要使用“Request :: hasFile”。

下面是代码示例:

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

答案 1 :(得分:0)

您应该尝试在表格中轻松存储图像,如:

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';
}

答案 2 :(得分:0)

我注意到您没有在任何地方考虑图片扩展程序。试试这个代码。我已将其修改为考虑带扩展名的文件名。

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';
}

希望这有用。

答案 3 :(得分:0)

$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();