2

I am using these simple form and want to save multiple data at a single time how it is possible?

controller

  $status= new PostTable();
  $status->language = $request->language;

blade

<input type="checkbox" value="hindi" name="language[]" id="language"> Hindi

model

protected $fillable = [
  'language'
 ]
11
  • you can insert comma seperated values or json encoded data Commented Jan 8, 2020 at 6:03
  • 'language' => json_encode($language) i have tried like these but it's not working out.! Commented Jan 8, 2020 at 6:05
  • amit i want to retirve data how to do it.? Commented Jan 8, 2020 at 9:22
  • Just fetch the value and use json_decode for it. Like json_decode($language) Commented Jan 8, 2020 at 9:29
  • oh,yes but i am using right now controller $status = PostTable::where('id',$id)->get(); return view('post.profile', compact('status')); Commented Jan 8, 2020 at 9:45

7 Answers 7

1

Storing Array is not allowed inside mysql DB

so You can convert it to json and do that

$status->language = json_encode($request->language);

or

$status->language = implode(',', $request->language);
Sign up to request clarification or add additional context in comments.

Comments

1
You can insert comma
implode(",",$request['language'])

Comments

1

Here is how you need to do it in your controller

$status= new PostTable();
$language[] = $request->language
$status->language = json_encode($language);

Comments

1

please chnage your code becuase you are select multiple language so use implode function

 $status->language = $request->language;

CHANGE

 $status->language = implode(',', $request->language);

Comments

1

Here in from $request->language is an array.In php you gave a function implode() .The implode function in PHP is used to "join elements of an array with a string".So try This

$status->language = implode(',', $request->language); //in which how you want separte your array is the first param 

It will save your multiple data separate by a comma.For details: https://www.c-sharpcorner.com/UploadFile/051e29/implode-and-explode-function-in-php/

Comments

1

use serialize() function

$status->language = serialize($request->language);

you can unserialize later to use .

unserialize($request->language)

Comments

0

Try This method for multiple values:

 $user = User::find(auth()->user()->id);
 $categoriesString = implode(",", $request->get('categories'));
 $user->categories = $categoriesString;
 $user->save();

For Multiple Images:

$medical_record = [];
$image = $request->file('file_name');
if ($image) {
    foreach ($image as $files) {
        $destinationPath = storage_path('medical-records/');
        $profileImage = date('YmdHis') . uniqid() . $files->getClientOriginalExtension();
        $files->move($destinationPath, $profileImage);
        $medical_record[] ="$profileImage";         
    }
    $data['file_name'] = implode(",", $medical_record);
}

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.