Home > Enterprise >  Tried to retrieve data in the database into my checkboxes, the problem is I can't insert the ch
Tried to retrieve data in the database into my checkboxes, the problem is I can't insert the ch

Time:09-06

This is my code in blade

<div >
                    <div >
                        <form id="contact" action="{{url('/reservation')}}" method="post">
                        @csrf
                          <div >
                            <div >
                                <h4>Reservation</h4>
                            </div>
                            <div >
                              <fieldset>
                                <input name="name" type="text" id="name" placeholder="Your Name*" >
                              </fieldset>
                            </div>
                            <div >
                              <fieldset>
                              <input name="email" type="text" id="email" pattern="[^ @]*@[^ @]*" placeholder="Your Email Address" >
                            </fieldset>
                            </div>
                            <div >
                              <fieldset>
                                <input name="phone" type="number" id="phone" placeholder="Phone Number*" >
                              </fieldset>
                            </div>
                            <div >
                              
                                <input type="text" name="address" placeholder="Address">

                            </div>
                            <div >
                                <div id="filterDate2">    
                                  <div  data-date-format="dd/mm/yyyy">
                                    <input name="date" id="date" type="text"  placeholder="dd/mm/yyyy">
                                    <div  >
                                      <span ></span>
                                    </div>
                                  </div>
                                </div>   
                            </div>
                            <div >
                              
                                <input type="time" name="time">

                            </div>

         @foreach($data as $data)
         <div >
                      <div >
                        <p class='mybox text-dark'><input type="checkbox" name="productz[]" value="{{$data->title}}"/>{{$data->title}}</p>
                      </div>
          @endforeach
        </div>
                            <div >
                              <fieldset>
                                <button type="submit" id="form-submit" >Make A Reservation</button>
                              </fieldset>
                            </div>
                          </div>
                        </form>
                    </div>
                </div>

THIS IS WHAT'S INSIDE MY MODEL

 <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Reservation extends Model
{
    use HasFactory;
}

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Reservation extends Model
{
    use HasFactory;
}

FOR MY CONTROLLER

$reservation = new reservation();
        $reservation->productz= implode(", " ,$request->productz);
        $reservation->save();
        return view('reservation');
    }

FROM MY MIGRATED TABLE

 <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateReservationsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('reservations', function (Blueprint $table) {
                $table->id();
                $table->string('name')->nullable();
                $table->string('email')->nullable();
                $table->string('phone')->nullable();
                $table->string('address')->nullable();
                $table->string('date')->nullable();
                $table->string('time')->nullable();
                $table->string('productz')->nullable();
    
    
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('reservations');
        }
    }

Tried to look for tutorial similar to what I'm doing where I retrieved the data from the database such as title and inserted it to checkbox which is successfully displayed. But I can't insert the data checked into the database.

CodePudding user response:

You should use the with function, which adds a piece of data into your view, in conjunction with the view function.

So something like:

return view('reservation')->with('data', $reservation->productz);

should work. Also do make sure the name of each variable that you're looping over is different from the name of the array itself -> e.g. foreach($datum as $data).

CodePudding user response:

Try to print_r($request->productz) and look the data from checkedbox is printing the correct data or not

  • Related