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

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

Related

Many-to-many relationships in Laravel Model Factory

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?

Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous

$banks_transactions = DB::table('finance_banking_transactions')
->join('finance_banks', 'finance_banking_transactions.bank_id', "=", 'finance_banks.id')
->latest()->paginate(20);
How do i solve the error SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous (SQL: select * from `finance_banking_transactions` inner join `finance_banks` on `finance_banking_transactions`.`bank_id` = `finance_banks`.`id` order by `created_at` desc limit 20 offset 0)" and I have the column
Because you use join, you need to specify latest().
for example, latest('finance_banking_transactions.created_at'), or instead using latest(), you can use orderBy('finance_banking_transactions.created_at')

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

Laravel observer Update Event in not Triggering

I used this in my update function:
$qty = $request->input('checked_out_qty');
DB::table('consumables')->decrement('remaining_qty',$qty );
i keep getting this error:
"SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (fixed.consumables_histories, CONSTRAINT 251075_5c35eb169cb40 FOREIGN KEY (checked_out_qty_id) REFERENCES consumables (id) ON DELETE CASCADE) (SQL: insert into consumables_histories (consumables_id, checked_out_qty_id, checked_out_by_id, status_id, location_id, assigned_to_id, updated_at, created_at) values (1368, 6, 1, 1, 1, , 2019-02-03 08:08:34, 2019-02-03 08:08:34))
but when u refresh the second time, the decrement still happens. what could i be doing wrong?

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