Software Developer
Software Developer

Reputation: 197

saving multiple checkboxes values and save to the database using Laravel

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.

Upvotes: 1

Views: 5878

Answers (4)

Ande Caleb
Ande Caleb

Reputation: 1204

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.

Upvotes: 1

Alec Joy
Alec Joy

Reputation: 1979

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.

Upvotes: 0

Nick
Nick

Reputation: 546

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

Upvotes: 0

Snoxik
Snoxik

Reputation: 400

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

Upvotes: 0

Related Questions