Many-to-many relationships in Laravel Model Factory - laravel

I am building a SaaS using Tenancy for Laravel and I am having issues creating model factories to seed the database. This is a 2-part question.
I have the following data structure for tenants:
User model
accounts(): $this->belongsToMany(Account::class);
Account model
users(): $this->belongsToMany(User::class);
venues(): $this->hasMany(Venue::class);
Venue model
account(): $this->belongsTo(Account::class); (using an account_id column on the venues table)
I also have an account_user pivot table to store the relationship between the User and Account models with account_id and user_id columns.
Part one of my question:
I have created factories for the models and I would now like to seed the database.
This is my code:
TenantSeeder.php
User::factory()->count(10)->hasAccounts(10)->create();
However when I run it, I get the following error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'account_id' cannot be null (SQL: insert into account_user (account_id, user_id) values (?, 2), (?, 2), (?, 2), (?, 2), (?, 2), (?, 2), (?, 2), (?, 2), (?, 2), (?, 2))
I tried to specify the tables at the belongsToMany Relationships of the User and Account model, however I still get this error.
I have also tried to add the relationship with the ->has() method of the Factory instead of the magic method, but I get the same results.
User::factory()->count(10)->has(Account::factory()->count(10))->create();
The second part of my question:
Is there a way to seed the Venues at the same time as the users and accounts?
Ie. something like this:
User::factory()
->count(10)
->has(
Account::factory()
->count(10)
->has(
Venue::factory()
->count(10)
)
)
->create();
In my VenueFactory class I am adding the account_id column's value like this:
return [
'account_id' => Account::factory(),
.....
]
This, however, throws the following error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'account_id' cannot be null (SQL: insert into `venues` (`account_id`, `updated_at`, `created_at`) values (?, 2023-02-14 12:35:40, 2023-02-14 12:35:40))
According to the documentation, however, this should work?
Any idea what could cause these two issues? Is it related to Tenancy?

Related

Laravel import/export. The csv file I am importing does not work

SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into users (name, email, updated_at, created_at) values (?, ?, 2021-06-03 12:25:18, 2021-06-03 12:25:18))
it's looks like you dont fill the password field, however it's mandatory

laravelx8 QueryException error while save model (ex:Reservation) need Apostrophes insert value items

I am using laravelx8 having problem with models query insert
use App\Models\Reservation;
from Reservation Models
$reservation = new Reservation();
$reservation->name = $username;
$reservation->email = $email;
$reservation->phone = $Phone;
$reservation->read = 0;
$reservation->payed = 0;
$reservation->tableId = $tableId;
$reservation->save();
Error is:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' (SQL: insert into resorvations (name, email, phone, read, payed, tableId, updated_at, created_at) values (bikash dash, bikash#omsysinfo.in, 9937090484, 0, 0, 7, 2020-12-29 11:14:46, 2020-12-29 11:14:46))
the found that
the QueryException if laravel support only mysql 8 but in my case the mysql5.5(mariaDB) need Apostrophes so it will insert into database eassly by laravel Query Builder
SQL: insert into resorvations (name, email, phone, read, payed, tableId, updated_at, created_at) values ('bikash dash', 'bikash#omsysinfo.in', '9937090484', 0, 0, 7, '2020-12-29 11:14:46', '2020-12-29 11:14:46')

Magento unable to add a product to a category

Whenever I try to add a product to a category I get this error.
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (db2018.catalog_category_product, CONSTRAINT FK_CAT_CTGR_PRD_PRD_ID_CAT_PRD_ENTT_ENTT_ID FOREIGN KEY (product_id) REFERENCES catalog_product_entity_oud (entity_id) ON DE), query was: INSERT INTO catalog_category_product (category_id,product_id,position) VALUES (?, ?, ?), (?, ?, ?)
Seems like there are no values being submitted?
Can anyone point me in the right direction to get this fixed?

Lumen 5.1 - many to many sync is missing data

I'm trying to create a reusable method for creating a relationship for a many to many pivot table but it seems to be missing the listing_id when trying to sync the data.
$model = $this->model->findOrFail($model_id)->with($relation);
return $model->getRelation($relation)->sync($data);
Returns:
integrity constraint violation: 1048 Column 'listing_id' cannot be null (SQL: insert into `tenants_listings` (`created_at`, `listing_id`, `tenant_id`, `updated_at`) values (2019-03-01 11:10:36, , ef4c9d60-a7a3-3340-8dd0-a901d624cd97, 2019-03-01 11:10:36)
This works perfectly fine when done like this:
$model = $this->model->findOrFail($model_id)->tenants();
return $model->sync($data);

Passing an id to other controller

$job->created_by = $input['created_by'];
I want to pass user id in this array which is in other table what should I do?
The field is a foreign key.
When I run this it throws an exception
SQLSTATE[23000]: Integrity constraint violation: 1452
Cannot add or update a child row: a foreign key constraint fails
(`freight`.`jobs`, CONSTRAINT `approved_by` FOREIGN KEY (`created_by`)
REFERENCES `users` (`id`))
(SQL: insert into `jobs`
(`company_id`, `origin`, `commodity`, `destination`,
`created_by`, `approved_by`, `date`, `carrier`, `consolidator`,
`overseas_agt`, `prepaid_fob`, `free_time`, `wt_pcs`,
`updated_at`, `created_at`)
values (2, , , , asdsadasdsad, asdsadasdsad, , , , , , , ,
2015-11-26 07:32:02, 2015-11-26 07:32:02)
)
You have initialize the field as unsigned in the DB Migration.
as:
$table->integer('user_id')->unsigned();
Here user_id is a primary key of another table.
Alter the table and run migration again.
It will work definitely.
I found what I did wrong I have to give the id of the user that is currently active the correct way is as follow
$job->created_by = \Auth::user()->id;

Resources