0

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.

My form looks like this Dynamic fields

I tried

$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"]}

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

6
  • Hi, Why do you want to save 3 fields in json/array format in a single column? Really I dont understand that. Commented Mar 27, 2018 at 8:04
  • Previously I saved in individual columns/rows. But later, I couldn't update (add/remove) data. So Commented Mar 27, 2018 at 8:29
  • Are you using mysql? Commented Mar 27, 2018 at 8:31
  • Yes. That column is text. What I really want is option to update (dynamically add or remove customer details upon updating) Commented Mar 27, 2018 at 8:37
  • 1
    That is really simple with laravel. Here you have an explanation in only 11 minutes. Laravel 5.4 Tutorial | CRUD (Update) Operation . Also you have The Full Serie Commented Mar 27, 2018 at 8:45

3 Answers 3

2

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Ya. It's text. But data are saved as {"name":["name1","name2"],"address":["addr1","addr2"],"profile_photo":"\/images\/432rd-1522133964.PNG"}
1

Try this:

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

3 Comments

It saved as {"name":["John","Sam"],"address":["CA","FL"],"profile_photo":"\/images\/32fdwf fds f-john-1522133136.PNG"}. Ican't save 2 images while there are 2 names & addresses.
@universal do you want to have each of these details in a separate row in the database, or all in the same row?
I want whatever. But I should be able to add/delete records later upon updating. I saved in separate rows before, but I couldn't achieve that, So I decided this approach. BTW I saved in the same format as above,
0

Try this with single database query

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.