Laravel-在列中的json/array中保存多个字段值 [英] Laravel - Save multiple field values in json/array in a column

查看:143
本文介绍了Laravel-在列中的json/array中保存多个字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 3 form fields name, address, profile_photo. I want to save there 3 fields in json/array format in a single column so that I can retrieve it later in view blade.

我的表单如下

我尝试过

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

使用此个人资料照片不会保存,但其他数据另存为:

With this profile_photo is not saved but other data are saved as:

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

我该如何保存其中的所有字段,包括profile_photo并稍后在视图刀片上清楚地显示每个详细信息?

How can I save all there fields including profile_photo and show each details distinctly later on view blade?

推荐答案

希望这会对您有所帮助.

Hope this will help you.

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

如果使用的是mysql,则details列应为text.

The details column should be text if you are using mysql.

这篇关于Laravel-在列中的json/array中保存多个字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆