skip to Main Content

I just re-structured my database to have a category table instead of using a category column on my main form. How can I pass the category_id to my form so my form POSTs properly? Right now i’m getting this error, and I believe it’s because my category_id is type integer and i’m submitting a string.

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value

Also I manually inserted Comp Time Used, OT Accrued, Sick Time and Vacation Time Used Into my category table via PHPMYADMIN. Is there a better way to do this? Like maybe inside my Model?

I have a form that looks like this:

<div class="form-group">
    <label for="categoryForm">Category:</label>
    <select class="form-control" name="category_id" id="categoryForm" placeholder="Category">
      <option name="category_id">Comp Time Used</option>
      <option name="category_id">OT Accrued</option>
      <option name="category_id">Sick Time</option>
      <option name="category_id">Vacation Time Used</option>
    </select>
</div>

My migration for my form looks like:

Schema::create('times', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id');
    $table->date('start_day');
    $table->unsignedBigInteger('category_id');
    $table->time('start_time');
    $table->time('finish_time');
    $table->time('duration');
    $table->text('notes');
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users');
    $table->foreign('category_id')->references('id')->on('categories');

});

My migration for my categories table:

public function up() {
    Schema::create('categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->timestamps();
        $table->string('type');
    });
}

2

Answers


  1. The problem is not with category_id, it is with start_date, which you are passing an integer instead of a datetime, as it says the error.

    Also, it is not correct to add name="category_id" on every option, you have to add it to the select.

    On your <option> you have to set a value for each option. For instance: <option value="1">Comp Time Used</option>. That way, you will be able to retrieve the correct category_id with request('category_id').

    Login or Signup to reply.
  2. Let’s assume you want to pass an array from a controller to your form on your blade view in laravel.

    Your Controller method should be something like this.

    use AppCategory; //import model
    public function create()
    {
       $categories = Category::all();
       return view('folder.bladefile', compact('categories')); //by this you will be able to access categories in your bladefile.
    }
    

    Inside of your blade file, you can do something like this

    <select class="form-control" name="category_id" id="categoryForm" placeholder="Category">
       @foreach($categories as $category)
         <option value="{{$category->id}}">{{$category->name}}</option>
       @endforeach;
    </select>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search