简体   繁体   English

Laravel-在单行中将多个图像存储在数据库中

[英]Laravel - Storing multiple images in the database in Single Row

How can I store multiple images in my database in laravel ? 如何在laravel的数据库中存储多个图像? I have this code but I cant pass any data it only passes one data. 我有这段代码,但是我无法传递任何数据,它只能传递一个数据。

I want to achieve this database format in my images Row 我想在我的图像行中实现这种数据库格式

在此处输入图片说明

Controller 控制者

 public function store(Request $request)
{


   //Handle File Upload
   if($request->hasFile('city')){
    // Get FileName
    $filenameWithExt = implode(' , ',$request->file('city')->getClientOriginalName());
    //Get just filename
    $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
    //Get just extension
    $extension =  implode(' , ',$request->file('city')->getClientOriginalExtension());
    //Filename to Store
    $fileNameToStore = $filename.'_'.time().'.'.$extension;
    //Upload Image
    $path =  implode(' , ',$request->file('city')->storeAs('public/city_image',$fileNameToStore));

}else{
    $fileNameToStore='noimage.jpg';
}

   $citi = new City;
   $citi->city =$fileNameToStore;
   $citi->save();

   return redirect('/lugar')->with('success', 'Data Inserted');
}

View 视图

                 <td> {{Form::file('city[]')}} </td>

You will want to JSON encode them. 您将要对其进行JSON编码。 Make that DB field as long text and when you are about to save the value to the DB do it so: 使该数据库字段为long text并在将值保存到数据库时执行以下操作:

$citi->city = json_encode($fileNameToStore);

And when you want to read the value you will do it as: 当您想读取该值时,您将执行以下操作:

json_decode($citi->city);

Now, I would advise to do an array of images and encode them, then decoding you can just do: 现在,我建议做一个图像数组并对它们进行编码,然后解码就可以了:

json_decode($obj->city, TRUE);

and you will have your array back 然后您将获得阵列

You should store the image in loop. 您应该将图像循环存储。 city field is an array. city字段是一个数组。

I modified your function. 我修改了您的功能。 Let me know if i did any wrong. 让我知道我做错了什么。

public function store(Request $request)
{
    if($request->hasFile('city'))
    {
       $file = Input::file('city');
       foreach($file as $key => $part) 
       {
          $filename = $part->getClientOriginalName();
          $filenameWithExt = implode(' , ',$filename);
          $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
          $extension =  implode(' , ',$part->getClientOriginalExtension());
          $fileNameToStore[$key] = $filename.'_'.time().'.'.$extension;
          $path =  implode(' , ',$part->storeAs('public/city_image',$fileNameToStore[$key]));
       }
       // converting images names array to comma separate string 
       $fileNameToStore = implode (", ", $fileNameToStore);
    }else{
       $fileNameToStore = null; // if no image found then it should be null
    }

   $citi = new City;
   $citi->city = $fileNameToStore;
   $citi->save();

   return redirect('/lugar')->with('success', 'Data Inserted');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM