I am new to laravel and try to add record in db. record is successfully inserted but now i need to getLast inserted id.
Insert record array here:
User::insert(
[
'first_name' => $data->first_name,
'last_name' => $data->last_name,
'email' => $data->signup_email,
'contact' => $data->signup_contact,
'role_id' => $data->role_id,
'password' => bcrypt($data->password_contact),
'confirm_passsword'=>bcrypt($data->confirmpassword_contact),
'created_at' => date('Y-m-d'),
'updated_at' => date('Y-m-d'),
]
);
Thanks to all for any help.
Just replace "insert" with "insertGetId". Thats it.
Related
this is my controller
$rules = [
'Group_Id'=>'required',
'Group_Code'=>'required',
'Group_Name'=>'required| string',
'Currency'=>'required',
'Country'=>'required',
'State'=>'required',
'City'=>'required',
]
I want group id Group code should be unique while create if group id /group code already present in table means I want to print error as group id/group id already exit .while updating don't want to change group id filed that's remains same
$rules = [
'Group_Id'=>'required|unique:group,id',
'Group_Code'=>'required|unique:group,code',
'Group_Name'=>'required| string',
'Currency'=>'required',
'Country'=>'required',
'State'=>'required',
'City'=>'required',
]
For solving this issue you should add unique identifier in your validation method mentioned as below
$data = $request->validate([
'Group_Id' => 'required',
'Group_Code' => 'required|unique:table_name_here',
'Group_Name' => 'required| string',
'Currency' => 'required',
'Country' => 'required',
'State' => 'required',
'City' => 'required',
]);
I'm using (latest) Lumen, which could be the culprit to my error.
When I'm using updateOrCreate():
User::updateOrCreate(
['username' => $user->username],
[
'email' => $user->email,
'password' => $user->password,
'foreign_id' => $user->foreign_id,
'client_id' => $user->client_id,
'status' => $user->active,
'user_level' => (integer) $user->user_level
]
);
on one of my models, I get mysql error:
"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'user#name.com' for key 'users_username_unique' (SQL: insert into `users` (`username`,
because it tries to insert duplicate value for one of my unique columns (username).
So, function itself exists, I dont get any errors, and it works for first inserts, but once it reaches functionality of checking if DB entry exists and needs only to update it, it still wants to create new entry, but with duplicated value.
Laravel itself has updateOrCreate(): https://laravel.com/docs/8.x/eloquent#upserts in its Eloquent. Is Eloquent in Lumen somehow crippled on this function?
When looking through the code of Laravel or Lumen, I cannot find this function implemented, the closest is updateOrFail()...
User::updateOrCreate(
[
'username' => $user->username,
'foreign_id' => $user->foreign_id
],
[
'email' => $user->email,
'password' => $user->password,
'client_id' => $user->client_id,
'status' => $user->active,
'user_level' => (integer) $user->user_level
])
// first array must contains all the primary/composite keys which makes the it a unique records
I am runing a bitcoin investment website. I want to give user free bonus of 0.005 points on registration. I have tried following in my registration Controller to fill database table. It works perfectly but after 100 user it creat some type of bug and I get error in user login it says:
But I remove all rows of depostit table all functions work perfectly.
trying to get property of non object
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'under_reference' => $data['reference'],
'password' => bcrypt($data['password']),
'verifyToken' => Str::random(40),
'reference' => Str::random(12),
'status' => $status,
'image' => $image25
]);
$deposit = Deposit::create([
'deposit_number' => date('ymd').Str::random(6).rand(11,99),
'user_id' => $user->id,
'plan_id' => "7",
'percent' => "1",
'time' => "30",
'compound_id' => "2",
'amount' => "0.005",
'status' => "0"
]);
I have 2 tables within one function that I'd like to save data to. One is the Users table, and the second one is a Clinic table.
My user's table is currently working correctly, but I'm unsure if it's 'best practice':
$user = User::create([
'name' => Str::title($request->get('name')),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
])
->clinic()->create($request->only([
'name' => Str::title('clinic_name'),
'telephone',
'address_1',
'address_2',
'city',
'postcode'
]));
My problem occurs at the 'name' column of the Clinic table. It just doesn't save it, even though it's in the $fillable array in my Clinic column:
protected $fillable = ['user_id', 'name', 'telephone'', 'address_1',
'address_2', 'city', 'postcode'];
I have attempted to 'Chain' the methods together, as I want to save the 'user_id' within the Clinic table because they're related.
Many thanks for your help.
You're overriding the name key in your $request->only() call:
$user = User::create([
'name' => Str::title($request->get('name')),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
])->clinic()->create($request->only([
'name' => Str::title('clinic_name'), // This is overriding your 'name' field.
'telephone',
'address_1',
'address_2',
'city',
'postcode'
]));
If you want to run Str::title() over the requests clinic_name, you'll need to assign the attributes manually like so:
$user = User::create([
'name' => Str::title($request->get('name')),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
])->clinic()->create([
'name' => Str::title($request->get('clinic_name')),
'telephone' => $request->get('telephone'),
'address_1' => $request->get('address_1'),
'address_2' => $request->get('address_2'),
'city' => $request->get('city'),
'postcode' => $request->get('postcode'),
]);
Note: Just as a tip, you can also just retrieve request input as a property like so:
->create([
'name' => $request->name // Performs $request->get('name').
])
You can't have 'name' => Str::title('clinic_name') when using create(), it must be a single key as 'name'.
You can use the following before creating the user:
$request->replace('name' => Str::title('clinic_name'));
Hi i have the next code for create my records
Institution::create($request->all());
User::create([
'name' => $request['name'],
'lastname' => $request['lastname'],
'phone' => $request['phone'],
'email' => $request['email'],
'password' => $request['password'],
'state' => 1,
'profile_id' => 1,
'institution_id' => Institution::max('id'),
]);
The last attributes for the User thats correct implement so?
The last 3 user attributes , it is correct to do it that way? or is there a better
Using Institution::max('id') creates a race condition. Since the create() static method of Eloquent::Model returns the newly-created model, you can just do:
$institution = Institution::create($request->all());
User::create([
'name' => $request['name'],
'lastname' => $request['lastname'],
'phone' => $request['phone'],
'email' => $request['email'],
'password' => $request['password'],
'state_id' => 1,
'profile_id' => 1,
'institution_id' => $institution->id,
]);
Creating a record with known parent ids is generally fine if your goal is to minimize the number of database queries and you have the ids of the related models.
Another way to do it, though it triggers more update queries, is to use Eloquent's built-in methods for adding related models. For example:
$institution = Institution::create($request->all());
$state = State::find(1);
$profile = Profile::find(1);
$user = new User([
'name' => $request['name'],
'lastname' => $request['lastname'],
'phone' => $request['phone'],
'email' => $request['email'],
'password' => $request['password']
]);
$user->state()->associate($state);
$user->profile()->associate($profile);
$user->profile()->associate($institution);
$user->save();
However, in this situation, since the related models are not already loaded, and you know their ids, there is no need to fetch them only to associate them with the User.