Creating relations using Laravel model factories - php

I am trying to create a user using model factories but I am having an error with the relationships. In the User model, I have created a belongsTo relationship levels for the model JobLevel. In the ModelFactory.php I have the following code.
use App\User;
use App\JobLevel;
$factory->define(JobLevel::class, function (Faker\Generator $faker) {
return [
'name' => $faker->word,
'rank' => $faker->randomDigit,
];
});
$factory->define(User::class, function (Faker\Generator $faker) {
return [
'firstname' => $faker->name,
'middlename' => $faker->name,
'lastname' => $faker->name,
'email' => $faker->email,
'job_level_id' => factory(JobLevel::class)->create()->id,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
];
});
Then I am in my test class I use the factory as follows.
/** #test */
public function login_a_user()
{
$user = factory(User::class)->create();
...
}
When I run phpunit I get the following error
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity
constraint violation: 1452 Cannot add or update a child row: a foreign key
constraint fails (`testing_db`.`user`, CONSTRAINT `fk_user_job_level1`
FOREIGN KEY (`job_level_id`) REFERENCES `job_level` (`id`) ON DELETE NO
ACTION ON UPDATE NO ACTION) (SQL: insert into `user` (`updated_at`,
`created_at`) values (2016-11-08 12:01:14, 2016-11-08 12:01:14))

Could you, for testing purposes try this?
$factory->define(User::class, function (Faker\Generator $faker) {
return [
'firstname' => $faker->name,
'middlename' => $faker->name,
'lastname' => $faker->name,
'email' => $faker->email,
'job_level_id' => 1,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
];
});
public function login_a_user()
{
$job = factory(JobLevel::class)->create();
$user = factory(User::class)->create();
...
}
I am not sure you can embed those factories.

Related

How to fetch and store data from other table to user table. Laravel 8

Form
{!! Form::select('grade_level[]', $grade_level,[], array('class' => 'form-control','multiple')) !!}
Controller
Here is my create function that will return the a view and the list of roles and grade level for users
public function create()
{
$grade_level = GradeLevel::pluck('grade_level','id')->all();
$roles = Role::pluck('name','name')->all();
return view('users.create',compact('roles','grade_level'));
}
Here is the store function. and I have encounter a problem MySql Error: 1364 Field 'grade_level_id' doesn't have default value
public function store(Request $request)
{
$this->validate($request, [
'f_name' => 'required',
'm_name' => 'required',
'l_name' => 'required',
'sex' => 'required',
'id_number' => 'required',
'address' => 'required',
// 'email' => 'required|email|unique:users,email',
'username' => 'required|unique:users',
'password' => 'required|same:confirm-password',
'grade_level_id' => 'required',
'roles' => 'required'
]);
$input = $request->all();
$input['password'] = Hash::make($input['password']);
$user = User::create($input);
$user->grade_level_id = $request->grade_level;
$user->assignRole($request->input('roles'));
return redirect()->route('users.index')
->with('success','User created successfully');
}
I see you're using User::create() method to add new user, you'd need to add grade_level_id to the fillable property or your User model.
protected $fillable = [
'grade_level_id'
];
Find More about it here : https://laravel.com/docs/8.x/eloquent#mass-assignment
Either add 'grade_level_id' to the $fillable of your user model,
or
inside your migration, you can add the ->nullable(); extension to the grade_level_id field declaration

Laravel 8.x - Storing data with Eloquent Relationship

I've got a User model that hasOne Membership model, with a users table and a memberships table (each entry in the memberships table has a foreign key linked to a user_id).
I've made a registration page that lets the user have a 7 days trial period on the membership but I'm having trouble storing the data.
This is the dd() of the data in the registration form:
"_token" => "ckRlMligEyTwu7ssOi4TmesycbsPpVQlrJ4jQaBd"
"username" => "JaneDoe"
"password" => "password"
"password_confirmation" => "password"
"expiration" => "2021-04-30"
Now in my controller I've got the following store() method:
public function store(Request $request) {
// validating
$this->validate($request, [
'username' => ['required', 'max:200'],
'password' => 'required|confirmed',
'expiration' => 'required'
]);
// storing
User::create([
'username' => $request->username,
'password' => Hash::make($request->password),
'expiration' => $request->expiration
]);
}
This won't store anything in the memberships table and I have no idea how to correctly write the store method using the Model's Eloquent Relationships declared.
Thanks for the help.
EDIT:
While trying to make some sense i've modified the store() function, now looks like this:
public function store(Request $request) {
// validating
$this->validate($request, [
'username' => ['required', 'max:200'],
'password' => 'required|confirmed',
'expiration' => 'required'
]);
// storing
User::create([
'username' => $request->username,
'password' => Hash::make($request->password)
])->membership(Membership::create([
'expiration' => $request->expiration
]));
}
Now seems like Laravel doesn't know where to get the user_id of the newly created user, like the error suggests:
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `memberships` (`expiration`, `updated_at`, `created_at`)
Your solution is to do:
User::create([
'username' => $request->username,
'password' => Hash::make($request->password)
])->membership()->create([
'expiration' => $request->expiration
]);
Using the relation (membership() not membership as an attribute) will already know the relation key (user_id in this case).
You can see more info about this in the documentation.
Other way of doing same is:
$membership = new Membership([
'expiration' => $request->expiration
]);
User::create([
'username' => $request->username,
'password' => Hash::make($request->password)
])->membership()->save($membership);
More info about save() on the documentation.

How to check if submitted data to a database is unique with Laravel form validation?

I am new to Laravel and I have run into an issue where when I check if data submitted is unique to a database is unique I get this error:
Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `users` where `0` in (tom#heekdevelopment.comasd, asd) limit 1)
http://127.0.0.1:8000/register
This is my code in Laravel:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class RegisterController extends Controller
{
public function index(){
return view('auth.register');
}
public function store(Request $request){
$this->validate($request, [
'name' => [
'required',
'max:255'
],
'username' => [
'required',
'max:255',
'unique:users,username'
],
'email' => [
'required',
'email',
'max:255',
'unique:users,email'
],
'password' => [
'required',
'confirmed'
]]);
User::create([
'name' => $request->name,
'username' => $request->username,
'email' => $request->email,
'password' => Hash::make($request->password)
]);
auth()->attempt([$request->only('email', 'password')]);
return redirect()->route('dashboard');
}
}
I thought the unique parameter checked if a value is unique or not.
It technically does check for it but when I submit a new value which is unique it throws the error, it does get entered into the database.
How to fix this?
You should remove the additional array [] from the attempt method Since the only method of $request object return an array.
auth()->attempt($request->only('email', 'password'));
Try like this
'email' => [
'required',
'email',
'max:255',
'unique:users'
],

Laravel Eloquent ORM inserts

is there a shorter way to insert data in Laravel over the Eloquent ORM?
at the moment i'm doing it this way:
$newCustomerGsale = new CustomersGsale();
$newCustomerGsale->email = $gsalesCustomer->getEmail();
$newCustomerGsale->customerno = $gsalesCustomer->getCustomerNumber();
$newCustomerGsale->created = $gsalesCustomer->getCreated();
$newCustomerGsale->company = $gsalesCustomer->getCompany();
$newCustomerGsale->firstname = $gsalesCustomer->getFirstname();
$newCustomerGsale->lastname = $gsalesCustomer->getLastname();
$newCustomerGsale->save();
like you see all the columns are named like the attributes.
I know u can use the $request object with all() function and fetch().
but I get the data from a other object (soap server).
is there a way to convert this? maybe over the model?
Your controller
Assume CustomersGsale is your model
public function store(CustomersGsaleFormRequest $request)
{
CustomersGsale::create($request->all());
}
CustomersGsaleFormRequest.php Laravel Form Requests Validation
public function rules()
{
return [
'email' => 'required|email',
'customerno' => 'required',
'created' => 'required',
'company' => 'required',
'firstname' => 'required'
];
}
CustomersGsale.php Model
your attributes and columns should be the same.
If you are using the create method for store the data then you will need to specify either a fillable or guarded attribute on the model
class CustomersGsale extends Model
{
protected $fillable = [
'email', 'customerno', 'created', 'company', 'firstname', 'lastname'
];
}
//MyControllerClass
public function store(myCustomValidator $request){
CustomersGsale::insert($request->all());
}
//myCustomValidator class
public function rules(): array
{
return [
'email' => 'required|numeric',
'customerno' => 'required',
'created' => 'required',
'company' => 'required',
'firstname' => 'required'
'lastname' => 'nullable'
];
}
Try the above method, hope this will work for you.

Laravel, phpunit fails due to empty foreign key

I'm getting this error while running a phpunit test:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'profile_id' cannot be null (SQL: insert into `comments` (`description`, `status`, `profile_id`, `project_id`, `updated_at`, `created_at`) values (Awesome Comment, active, , 21, 2016-01-29 00:05:21, 2016-01-29 00:05:21))
As you can see it is sending an empty id for the profile_id and I'm creating the profile before creating the comment. Here is the code of my test:
public function testProjectCommentCreation()
{
$category = factory(\App\Category::class)->create();
$category->projects()->save(factory(\App\Project::class)->make());
$profile = factory(\App\Profile::class)->make([
'name' => 'John',
'last_name' => 'Snow',
'skills' => 'php'
]);
$category->projects[0]->comments()->save(factory(\App\Comment::class)->make([
'description'=>'Awesome Comment',
'status'=>'active',
'profile_id'=>$profile->id
]));
$this->post(route('api.projects.comments.store', ["projects" => $category->projects[0]->id]), $category->projects[0]->comments->jsonSerialize(), $this->jsonHeaders)
->seeInDatabase('comments', ['project_id' => $category->projects[0]->id])
->assertResponseOk();
}
A Project belongs to a Category and a Comment belongs to a Project and a Profile, so I need to send both foreign keys values profile_id and project_id, the problem is that I'm not sure how to retrieve the id of the profile I created.
These are the factories I use:
$factory->define(App\Profile::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'last_name' => $faker->name,
'status' => 'active',
'avatar' => str_random(10),
'skills'=> str_random(10),
'notifications'=>'on'
];
});
$factory->define(App\Comment::class, function (Faker\Generator $faker) {
return [
'description' => str_random(10),
'status' => 'active',
'profile_id' => 1,
'project_id' => 1
];});
$factory->define(App\Category::class, function (Faker\Generator $faker) {
return [
'description' => str_random(10),
'status' => 'active'
];});
$factory->define(App\Project::class, function (Faker\Generator $faker) {
return [
'description' => str_random(10),
'status' => 'active',
'privacy' => 'false'
];});
I've tested the construction of each type of Object and it is working, what I'm failing is to create a Comment since I need to create a Profile first and retrieve the id and for some reason using $profile->id is pulling null
The problem is that your profile is not saved to the database, when using make().
For you to be able to use/assign the foreign key profile_id from $profile, you need to create() the factory model, and not just make() it.
$profile = factory(\App\Profile::class)->create([
'name' => 'John',
'last_name' => 'Snow',
'skills' => 'php'
]);
That should do the trick.

Categories