How to insert multi IDs and save in database

1 answer

im using Laravel 7 and im trying to make a automation system. For send Letters, i have to be able to choose a few receivers. Here is Letter table : Letter_Table as you can see i have recieve_id i can get IDs, but i dont know how i can save IDs in my database. This is my Select :

<select class="col-12 border mt-2 pt-2" multiple="multiple" name="recieve_id[]">     @foreach($users as $user)          <option value="{{ $user->id }}">{{ $user->name }}</option>     @endforeach </select> 

This is my Controller:

Letter::create([         'indicator_id' => $request['indicator_id'],         'sender_id' => auth()->user()->id,         'recieve_id' => $request['recieve_id'],         'title' => $request['title'],         'image' => 'default.png',         'description' => $request['description'],         'date' => $request['date'],         'text' => $request['text'],     ]);     return redirect(route('Letter.index')); 

Thanks for your help <3


Here is Table structure : Table Structure

All answers to this question, which has the identifier 61235532

The best answer:

If I am understanding your question right, the code you have written in the controller is not working well or not creating database entry. Is it right?

If yes, then can you provide me with the table structure you are using for Letter as well as the migration you have written for that particular table?

I am not sure but it seems like you are passing an array $reciever_id instead of string and/or JSON string.

Also, if the table in the screenshot you provided, is to be used as the log, then I would suggest to iterate through $receiver_id and make a separate entry for each of the receiver if possible. This could be a better approach I think.

EDIT: After reading your comment, I am modifying my answer as below: In that case, you should either handle the request with foreach loop as below:

$receiver_ids = $request->receive_ids foreach($receiver_ids as $receiver_id) {     Letter::create([         'indicator_id' => $request['indicator_id'],         'sender_id' => auth()->user()->id,         'recieve_id' => $receiver_id,         'title' => $request['title'],         'image' => 'default.png',         'description' => $request['description'],         'date' => $request['date'],         'text' => $request['text'],     ]); } 

Put this code in your controller. And let me know.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data