I have an issue in Laravel.
I am storing data into a table using eloquent and I am retrieving the newly created ID ($categoryId) of that record/model, when I dump out the ID it shows as an int (desired output)
However, when I try and use/store that ID in another table using an eloquent query ($newProduct->category_id = $categoryId;), it fails as it adds square brackets to the value e.g. [1].
So my question is how do I get the value without the brackets so I can use it as desired in my other eloquent query?
When I try and strip the brackets off it complains that the value is already an int, so I am a bit lost with this - Thank you for any help and guidance
Below is my code:
$newCategory = new Category;
$newCategory->short_desc = $category;
$newCategory->save();
$categoryId = $newCategory->id;
$newProduct = new Product;
$newProduct->name = $product_name;
$newProduct->category_id = $categoryId;
$newProduct->save();
$productId = $newProduct->id;
Below is the error message:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '[1]' for column 'category_id' at row 1 (SQL: insert into `products` (`name`, `description`, `category_id`, `container_id`, `user_id`, `updated_at`, `created_at`) values (Razer Blade 16, AMD \" 10, [1], [2], 1, 2022-09-14 15:24:36, 2022-09-14 15:24:36))"
Product migration:
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', '255')->index();
$table->text('description');
$table->unsignedBigInteger('category_id')->nullable()->index();
$table->unsignedBigInteger('container_id')->nullable()->index();
$table->unsignedBigInteger('user_id')->index();
$table->timestamps();
$table->softDeletes();
$table->foreign('category_id')->references('id')->on('categories');
$table->foreign('container_id')->references('id')->on('containers');
$table->foreign('user_id')->references('id')->on('users');
});
Category migration:
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('short_desc', '255');
$table->text('description');
$table->timestamps();
$table->softDeletes();
});
CodePudding user response:
please replace your Product migration , rollback and migrate again.
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', '255')->nullable();
$table->text('description')->nullable();
$table->integer('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')->on('categories');
$table->integer('container_id')->unsigned()->nullable();
$table->foreign('container_id')->references('id')->on('containers');
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
$table->softDeletes();
});
CodePudding user response:
I have solved my issue by using $newCategory->id instead of storing it in another variable - thanks for the guidance @Yai
Instead of this below:
$categoryId = $newCategory->id;
$newProduct->category_id = $categoryId;
Use this instead:
$newProduct->category_id = $newCategory->id;
For what was causing the issue when inserting into the table I have no clue.