1

Hi i have this select checkbox which is the time when i check all the value does pass through it! Now my problem is when i saved into the database it is only one value saved. this is my HTML

<p>Sunday</p>
                <input type="checkbox" name="select-all-sunday" id="select-all-sunday" onclick="toggle(this);"> Check All
                <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-1" value="00:00"> 00:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-2" value="1:00"> 1:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-3" value="2:00"> 2:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-4" value="3:00"> 3:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-5" value="4:00"> 4:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-6" value="5:00"> 5:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-7" value="6:00"> 6:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-8" value="7:00"> 7:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-9" value="8:00"> 8:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-10" value="9:00"> 9:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-11" value="10:00"> 10:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-12" value="11:00"> 11:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-13" value="12:00"> 12:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-14" value="13:00"> 13:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-15" value="14:00"> 14:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-16" value="15:00"> 15:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-17" value="16:00"> 16:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-18" value="17:00"> 17:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-19" value="18:00"> 18:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-20" value="19:00"> 19:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-21" value="20:00"> 20:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-22" value="21:00"> 21:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-23" value="22:00"> 22:00 <br>
                <input type="checkbox" name="checkbox-sunday[]" id="checkbox-24" value="23:00"> 23:00 <br>

this is now my controller

$sundays = $request->input('checkbox-sunday');

        foreach($sundays as $sunday){
            echo  $sunday.",";



        }

Now the echo $sunday seems the value being passed from the form display correctly. Now upon saving into the database only one value is being saved.

Below is my code upon saving to the database

$sundays = $request->input('checkbox-sunday');

        foreach($sundays as $sunday){
            echo  $sunday.",";

            $postRoom = PostRoom::find($id);

            $postRoom->description = $request->get('description');
            $postRoom->checkin_date = $request->get('bookingDate');
            $postRoom->checkin_time = $request->get('checkinTime');
            $postRoom->room_price = $request->get('roomPrice');
            $postRoom->day_sunday = $sunday;
            $postRoom->your_neighbourhood = $request->get('yourNeighbourhood');

            $postRoom->save();

            return redirect('add-your-listing/next-step-3/'.$postRoom->id);



        }

what is the best way to saved this one?. Can someone help me? TIA.

3
  • where do you get $id from? Commented Oct 22, 2019 at 15:12
  • sorry i just added that. i will have to remove that Commented Oct 22, 2019 at 15:13
  • don't remove it, it's the most important part, show us where you get the ID from Commented Oct 22, 2019 at 15:21

4 Answers 4

1

Your problem is based on Array to String Conversion, you can either use json_encode() or json_decode() to transform your array when saving and retrieve while reading, or for me, i use serialize() and unserialize() to store and retrive array sets from a database as string datatype.

//read the checkbox input from the form.
$sundaysArray = $request->input('checkbox-sunday');

//while saving 
$postRoom->day_sunday = json_encode($sundaysArray)  //or 
$postRoom->day_sunday = serialize($sundaysArray);

//while reading from database
$sundaysArray = json_decode($postRoom->day_sunday)  //or
$sundaysArray = unserialize($postRoom->day_sunday);

either way this works.

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

Comments

0

normal you spend in your loop and you look for each time the id and save the current give loop so you crush it, the best thing would be to make a table and send this table in this field (sorry for my english)

me i try this.

    $sundays = $request->input('checkbox-sunday');

    $sundaysArray = array();

    foreach($sundays as $sunday){
       $sundaysArray[] = $sunday;
    }


    $postRoom = PostRoom::find($id);

    $postRoom->description = $request->get('description');
    $postRoom->checkin_date = $request->get('bookingDate');
    $postRoom->checkin_time = $request->get('checkinTime');
    $postRoom->room_price = $request->get('roomPrice');
    $postRoom->day_sunday = json_encode($sundaysArray);
    $postRoom->your_neighbourhood = $request->get('yourNeighbourhood');

    $postRoom->save();

    return redirect('add-your-listing/next-step-3/'.$postRoom->id);

6 Comments

it says (2/2) QueryException Array to string conversion
it work now! the data being saved is this one ["8:00","9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00"]
its in json . but i want that it is not something like that. something like this one 8:00, 9:00, 10:00, 11:00 and so on....
Nickel :) I hope you understood your mistake with my message :)
but i want upon saving not in json encode something like this 8:00, 9:00, 10:00, and so on.. hmmm
|
0

Model PostRoom

protected $fillable = [
        'bookingDate', 'description', 'checkinTime', 'roomPrice', 'day_sunday', 'your_neighbourhood'];

Code

     $sundays = $request->input('checkbox-sunday');
     $data = [];
     $hours = []
     foreach($sundays as $sunday){
           $hours[] = $sunday;
     }
     $room = PostRoom::find($id);
     $data['description'] = $request->get('description');
     $data['bookingDate'] = $request->get('bookingDate');
     $data['checkinTime'] = $request->get('checkinTime');
     $data['roomPrice'] = $request->get('roomPrice');
     $data['day_sunday'] = implode(', ' $hours);
     $data['your_neighbourhood'] = $request->get('yourNeighbourhood');
     $room->update($data);

     return redirect('add-your-listing/next-step-3/'.$room->id);

3 Comments

add fillable to PostRoom
you don't need fillable properties because OP is not doing mass assignment
did as you like
0

On your PostRoom model add a protected $casts variable with the following

protected $casts = [
    'day_sunday' => 'array',
];

Then update your save method as follows

$postRoom = PostRoom::find($id);

$postRoom->description = $request->get('description');
$postRoom->checkin_date = $request->get('bookingDate');
$postRoom->checkin_time = $request->get('checkinTime');
$postRoom->room_price = $request->get('roomPrice');
$postRoom->day_sunday = $request->get('sundays');
$postRoom->your_neighbourhood = $request->get('yourNeighbourhood');

$postRoom->save();

This will store the data in your database as a json_encoded object, but as far as your laravel app is concerned it's an array. When you read the $postRoom->day_sunday property it will return an array matching the one returned from the controller.

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.