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

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

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?

Laravel - how to handle not required/optional field in $fillable insert and update

I have table with below fields.
Only name is required and rest of them is optional
'name','no_of_color','offset_printing_rate','screen_printing_rate','positive_rate','regular_plate_rate','big_plate_rate',
My model
protected $fillable = [
'name',
'no_of_color',
'offset_printing_rate',
'screen_printing_rate',
'positive_rate',
'regular_plate_rate',
'big_plate_rate',
];
but when I don't fill optional field it returns error
SQL: insert into table_name (name, no_of_color, offset_printing_rate, screen_printing_rate, positive_rate, regular_plate_rate, big_plate_rate, updated_at, created_at) values (name, ?, ?, ?, ?, ?, ?, 2021-10-09 14:47:36, 2021-10-09 14:47:36)
MySQL always want a value to all cols so in laravel you have to set default for each col or set it nullable but for performance purpose, i recommended you to split optional in other table and connect them to main table by relation this will prevent increasing of null records so you don't have to waste your host space

vendor/laravel/framework/src/Illuminate/Database/Connection.php:678

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into product_catagories (catagory_name, updated_at, created_at) values (Electronics, 2021-04-25 14:32:59, 2021-04-25 14:32:59))
How Can I solved this problem on a live server
Check your 'id' structure on your sql.
You got an error because 'id' field' doesn't have default value.
Also Make sure AUTO_INCREMENT checked

Checking the row existence before insertion using Cassandra within a Go application

I am using gocql with my Go application and trying to solve the issue described below.
CREATE TABLE IF NOT EXISTS website.users (
id uuid,
email_address text,
first_name text,
last_name text,
created_at timestamp,
PRIMARY KEY (email_address)
);
This query is going to override matching record which is Cassandra's expected behaviour.
INSERT INTO users (id, email_address, first_name, last_name, created_at)
VALUES (?, ?, ?, ?, ?)
In order to prevent overriding the existing record we can use IF NOT EXISTS at the end of the query.
INSERT INTO users (id, email_address, first_name, last_name, created_at)
VALUES (?, ?, ?, ?, ?)
IF NOT EXISTS
However, there is no way for me to know if the query affected any rows in DB or not. Somehow I need to return something like "Record exists" message back to caller but it is currently not possible. If there was something specific with session.Query(...).Exec() it would be useful but there isn't as far as I know.
I was thinking to SELECT by email_address before proceeding with INSERT if there was no matching record but as you can guess this is not feasible because by the time I INSERTed a new record after SELECT, some other operation could have INSERTed a new record with same email address.
How do we handle such scenario?
The solution is to use ScanCAS and the test case example from the library is here.
NOTE:
Order of the fields in ScanCAS() should match cqlsh> DESCRIBE keyspace.users; output for the CREATE TABLE ... block.
If you don't care about the scanned fields, prefer MapScanCAS instead.
func (r Repository) Insert(ctx context.Context, user User) error {
var (
emailAddressCAS, firstNameCAS, idCAS, lastNameCAS string
createdAtCAS time.Time
)
query := `
INSERT INTO users (email_address, created_at, first_name, id, last_name)
VALUES (?, ?, ?, ?, ?) IF NOT EXISTS
`
applied, err := r.session.Query(
query,
user.EmailAddress,
user.CreatedAt,
user.FirstName,
user.LastName,
user.CreateAt,
).
WithContext(ctx).
ScanCAS(&emailAddressCAS, &createdAtCAS, &firstNameCAS, &idCAS, &lastNameCAS)
if err != nil {
return err
}
if !applied {
// Check CAS vars here if you want.
return // your custom error implying a duplication
}
return nil
}
If you're using INSERT with IF NOT EXISTS, then in contrast to "normal" inserts that doesn't return anything, such query returns a single row result consisting of:
field with name [applied], and true value - if there was no record before, and new row was inserted
field with name [applied], and false value + all columns of existing row.
So you just need to get result of your insert query, and analyze it. See documentation for more details.

Deal with Radio Buttons in Laravel

Problem: I have a table categories on my database which contains the cat_id as primary key of type int and cat_name as name of the category. But I collect info from the under via radio.
Question: How can I convert the 'on' and 'off' from the user to integers?
Error Given:
Illuminate\Database\QueryException SQLSTATE[HY000]: General error:
1366 Incorrect integer value: 'on' for column 'type' at row 1 (SQL:
insert into users (user_name, name, type, email, password,
updated_at, created_at) values (malico, John Doe, on,
john.doe#gmail.com,
$2y$10$yyf4Vt01ukTNPFxGa1PIheJZvmcPbozGjdz8zo84vkK325Qk6rfna,
2018-05-19 12:56:01, 2018-05-19 12:56:01))
just do something like this on your request :
$request->merge(['type'=>$request->get('type') == 'on' ? 1 : 0]);
this will change "on" to 1 in your request

Resources