Laravel - 在列中的json / array中保存多个字段值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel - 在列中的json / array中保存多个字段值相关的知识,希望对你有一定的参考价值。

我有3个表单字段name, address, profile_photo。我想在一个列中以json / array格式保存3个字段,以便稍后在视图刀片中检索它。

我的表格看起来像这个Dynamic fields

我试过了

$customer_details= new Customer;
  {

   $profile_photo = $request->file('profile_photo');
   for ($i=0; $i < count(request('profile_photo')); $i++) {

   $pro_fileExt =  $profile_photo[$i]->getClientOriginalExtension();
   $pro_fileNameToStore = time().'.'.$pro_fileExt;

   $pro_pathToStore = public_path('images');

   Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR. $pro_fileNameToStore);
   $profile_ph = '/images/'.$pro_fileNameToStore; }

   $cust_details=json_encode(request(['name', 'address', $profile_ph]));
   $customer_details->details = $cust_details;
   $customer_details->save();
  }

使用此profile_photo不会保存,但其他数据保存为:

{"name":["john","Sam"],"address":["CA","FL"]}

如何保存所有字段,包括profile_photo,并在视图刀片上稍后清楚地显示每个细节?

答案

希望这会帮助你。

    $customer_details = new Customer;

    $profile_photo = $request->file('profile_photo');
    $profile_ph = [];

    for ($i = 0; $i < count(request('profile_photo')); $i++) {

        $pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
        $pro_fileNameToStore = time() . '.' . $pro_fileExt;

        $pro_pathToStore = public_path('images');

        Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR . $pro_fileNameToStore);
        $profile_ph[$i] = '/images/' . $pro_fileNameToStore;
    }

    $data = [
        'name' => $request->name,
        'address' => $request->address,
        'profile_ph' => $profile_ph
    ];

    $customer_details->details = json_encode($data);

    $customer_details->save();

如果你使用mysqldetails列应该是text

另一答案

单个数据库查询尝试此操作

$profile_photo = $request->file('profile_photo');
$finalData = [];
$names = [];
$addresses = [];
$images = [];
for ($i=0; $i < count(request('profile_photo')); $i++) {
  $pro_fileExt =  $profile_photo[$i]->getClientOriginalExtension();
  $pro_fileNameToStore = time().'.'.$pro_fileExt;
  $pro_pathToStore = public_path('images');
  Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR. $pro_fileNameToStore);
  $profile_ph = '/images/'.$pro_fileNameToStore; }
  $names[] = $request->input('name');
  $addresses[] = $request->input('address');
  $images[] = $profile_ph;
}
 $cust_details=json_encode(['name' => $names, 'address' => $addresses, 'images' => $images]);
 $customer_details->insert($cust_details);
另一答案

试试这个:

$names = $request->input('name');
$addresses = $request->input('address');
$profile_photos = $request->file('profile_photo');
for ($i = 0; $i < count($names); $i++) {
    $customer_details = new Customer;
    $pro_fileExt = $profile_photos[$i]->getClientOriginalExtension();
    $pro_fileNameToStore = time() . '.' . $pro_fileExt;

    $pro_pathToStore = public_path('images');

    Image::make($profile_photos[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR . $pro_fileNameToStore);
    $profile_ph = '/images/' . $pro_fileNameToStore;

    $cust_details = json_encode([
        'name' => $names[$i],
        'address' => $addresses[$i],
        'profile_photo' => $profile_ph,
    ]);

    $customer_details->details = $cust_details;
    $customer_details->save();
}

以上是关于Laravel - 在列中的json / array中保存多个字段值的主要内容,如果未能解决你的问题,请参考以下文章

如何将存储在列中的 JSON 数组中的每个元素的行拆分为一行?

使用 json 数据并将其显示在列中

在 Laravel 中使用诸如 JSON 函数之类的 Postgres 功能

Laravel 向 json 列中的数组添加一个元素

如何查询数据库以查找包含来自多选 (Laravel) 的 json 数据的列中的元素

在列中的两个值之间重复值