Laravel - adding relationships to a factory-created model - php

I am testing an eager loading relationship which contains many to many relations. Right now I have the queries and attachments within the test. I'm wondering if there is a way to move them into the factory, rather than including it as part of your test. This would limit the size of the test and then these relations could be created and used every time a film factory is created.
test
public function grabFilmTest()
{
$film = factory(Film::class)->create();
$categories = Category::where('main-cat', 'Science')->where('sub-cat', 'Fiction')->first();
$languages = Languages::where('name', 'english')->first();
$film->categories()->attach($categories->id);
$film->languages()->attach($languages->id);
$response = $this->json('GET', '/film/' . $film->id)
->assertStatus(200);
$response
->assertExactJson([
'id' => $film->id,
'name' => $film->name,
'description' => $film->description,
'categories' => $film->categories->toArray(),
'languages' => $film->languages->toArray()
}
filmFactory
$factory->define(\App\Models\Film::class, function (Faker $faker){
return [
'id' => $faker->uuid,
'name' => $faker->text,
'description' => $faker->paragraph,
];
});
If anyone could help with how i could do this or an example it would be great :D

You could use factory states and factory callbacks.
$factory->define(\App\Models\Film::class, function (Faker $faker){
return [
'id' => $faker->uuid,
'name' => $faker->text,
'description' => $faker->paragraph,
];
});
$factory->define(\App\Models\Category::class, function (Faker $faker){
return [
// Category fields
];
});
$factory->define(\App\Models\Language::class, function (Faker $faker){
return [
// Language fields
];
});
$factory->afterCreatingState(\App\Models\Film::class, 'with-category', function (\App\Models\Film $film) {
$category = factory(\App\Models\Category::class)->create();
$film->categories()->attach($category->id);
});
$factory->afterCreatingState(\App\Models\Film::class, 'with-language', function (\App\Models\Film $film) {
$language = factory(\App\Models\Language::class)->create();
$film->categories()->attach($language->id);
});
Then you can use in tests like this:
public function grabFilmTest()
{
$film = factory(Film::class)->create();
$filmWithCategory = factory(Film::class)->state('with-category')->create();
$filmWithLanguage = factory(Film::class)->state('with-language')->create();
$filmWithCategoryAnLanguage = factory(Film::class)->states(['with-category', 'with-language'])->create();
// ...
}
PS: I don't recommend using existing data. From experience, I can tell you that can become really painful.

You can use factory callbacks to do it in the factory file:
<?php
use \App\Models\Film;
use \App\Models\Category;
use \App\Models\Languages;
$factory->define(Film::class, function(Faker $faker){
return [
'id' => $faker->uuid,
'name' => $faker->text,
'description' => $faker->paragraph,
];
});
$factory->afterCreating(Film::class, function(Film $film, Faker $faker) {
$category = Category::where('main-cat', 'Science')->where('sub-cat', 'Fiction')->first();
$language = Languages::where('name', 'english')->first();
$film->categories()->attach($category);
$film->languages()->attach($language);
});

Related

Save multi-level array using Laravel Model

I have problem to save this data using Laravel-7 model
this is my data
$supplier = [
'name' => 'Supplier 1',
'pic' => [
[
'name' => 'PIC 1',
'phone_number' => [
['number' => '111111'],
['number' => '123456']
]
],
[
'name' => 'PIC 2',
'phone_number' => [
['number' => '222222']
]
]
]
];
And this is my models
Supplier.php
// Supplier.php
public function supplier_pic()
{
return $this->hasMany('SupplierPIC');
}
and the other models
// SupplierPIC.php
public function supplier()
{
return $this->belongsTo('Supplier');
}
public function pic_phone_number()
{
return $this->hasMany('SupplierPICPhoneNumber');
}
// SupplierPICPhoneNumber.php
public function supplier_pic()
{
return $this->belongsTo('SupplierPIC');
}
How to save those data on controller ?
Thank you
You just need to break it down into it's constituent objects.
In your case, it is one Supplier object with two SupplierPIC objects, each of which has a SupplierPICPhoneNumber
Create Supplier
$supplier = Supplier::firstOrCreate([
'name' => 'Supplier 1'
]);
Create Supplier PIC(s)
collect($data['pics'])->each(function ($pic) use ($supplier) {
// Create the PIC
$x = SupplierPIC::create([
'name' => $pic['name']
]);
// Attach it to the supplier
$supplier->supplier_pic()->save($x);
// Attach phone numbers
collect($pic['phone_number'])->each(function ($number) use ($x) {
// Create the PIC Phone number
$y = SupplierPICPhoneNumber::create([
'number' => $pic['number']
]);
// Attach the number to the PIC
$x->pic_phone_number()->save($y);
});
});
Suggestions
The naming of your relationships doesn't follow best practice which is a little confusing. Try naming things that are use a hasMany type relationship with a plural (i.e. pic_phone_numbers rather than pic_phone_number)
Do you need an entire model for SupplierPICPhoneNumber? A json column may be better suited.

Laravel API Resource not returning correct value with condition

I am new to Laravel API, I want to return a book where recommended === 1
In my resources, I have this
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'about' => $this->about,
'content' => $this->content,
// 'image' => asset('/storage/'.$this->image),
'image' => $this->image_url,
// 'recommended' => $this->recommended,
'recommended' => $this->when($this->recommended === 1, $this->recommended),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'author' => $this->author,
];
I want to return books when recommended === 1
My table is Like this
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('about');
$table->string('image');
$table->string('image_url');
$table->string('epub_url');
$table->integer('author_id');
$table->string('publisher');
$table->year('year');
$table->boolean('recommended')->default(0);
$table->timestamps();
});
I was able to achieve the same thing on web using this
public function index()
{
$data = array();
$data['recommends'] = Book::where('recommended', 1)->take(10)->get();
$data['latests'] = Book::orderBy('created_at', 'desc')->take(10)->get();
return view('welcome', compact("data"));
}
But I don't know how to replicate the same using Laravel API.
UPDATE
I was able to achieve the same thing on web using this
public function index()
{
$data = array();
$data['recommends'] = Book::where('recommended', 1)->take(10)->get();
$data['latests'] = Book::orderBy('created_at', 'desc')->take(10)->get();
return view('welcome', compact("data"));
}
But I don't know how to replicate the same using Laravel API.
Normally I will get all Books or Post like this using API Resource
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'about' => $this->about,
'content' => $this->content,
'image' => $this->image_url,
'recommended' => $this->recommended,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'author' => $this->author,
];
and call it like this in my controller
public function indexapi()
{
return BookResource::collection(Book::with('author')->Paginate(16));
}
But there some cases recommended is == 1 and some recommended == 0, in this case, I want to return data only when recommended == 1
I know my question is quite confusing
Thanks.
Thanks.
If I get it right, you want to filter & get only the books with ( recommended attribute == 1 ). If thats the case you shouldn't do it in your Collection file. You should do this filtering process in your Controller before passing any data to Collection.
Here is some code example from one of my project.
In ProductController.php FILE
public function index()
{
return new ProductCollection( Product::where('recommended','1')->get() );
}
As you can see , I'm filtering the products to get only the recommended ones. Then I'm sending this filtered data to the ProductCollection. This way The collection will only return the data I want.
In ProductCollection.php FILE
public function toArray($request)
{
return [
'data' => $this->collection->map( function($data) {
return [
'id' => (integer) $data->id,
'name' => $data->name,
'category_id' => $data->category_id,
'brand_id' => $data->brand_id,
'photos' => json_decode($data->photos),
'gtin' => $data->gtin
];
})
];
}
I don't have to make any changes in Collection. Because in this way , Collection should do the job for every data it gets.

Passing a factory into another in Laravel 5

I am trying to generate a test on Laravel.
What I was trying is to create a fictitious position name, then add 10 people for this position.
PositionsFactory.php
$factory->define(App\Position::class, function (Faker $faker) {
return [
'p_id' => $faker->unique()->randomNumber($nbDigits = 8),
'name' => $faker->word,
'org' => $faker->word,
'user_id' => 1
];
});
Here is my EmployeeFactory.php
$factory->define(App\Employee::class, function (Faker $faker) {
return [
'FirstName' => $faker->name,
'LastName' => $faker->lastName,
'pid' => $position->p_id,
'org'=> $position->org,
'user_id' => 1,
];
});
Well here is one my my trials but it did not work
for ($i=0; $i < 5; $i++ ){
$position = factory('App\Position')->create();
factory('App\Employee',10)->create(
'pid' => $position->pid,
'org' => $position->org
);
}
I am trying to loop for 5 times and for each loop I want to create 10 employees with the same position Id. But obviously I am missing something.
I tried adding $position in the Employee factory, which works great.
$factory->define(App\Employee::class, function (Faker $faker) {
$position = factory('App\Position')->create();
return [
'FirstName' => $faker->name,
'LastName' => $faker->lastName,
'pid' => $position->p_id,
'org'=> $position->org,
'user_id' => 1,
];
});
Is there a way to make something like,
$factory('App\Position',5)->create($factory('App\Employee',10));
Maybe I am missing something with call back but kinda lost. Any help would be appreciated. Thank you!
I think you might be looking for the each method which can be called after create:
// Generate 5 positions and execute a callback
// function for each position created
factory(App\Position::class, 5)->create()->each(function ($position) {
// In the callback, generate 10 employees
// and manually override the foreign key
factory(App\Employee::class, 10)->create([
'pid' => $position->id
]);
});
Further information on each and handling relationships: https://laravel.com/docs/5.6/database-testing#relationships.
Hope it helps!
You can create them separatly and loop through collections.
$positions = factory('App\Position', 3)->make();
foreach ($positions as $position){
$employes = factory('App\Employee', 3)->make();
foreach ($employes as $employee){
$employee->p_id = $position->id;
//etc.. watever you want to connect
}
}
now you have 1 collection of positions and 1 collection of employes devided of the positions
note that the make method does not save to database you need to manually save them
you could also change your factory as is stated in the documentation https://laravel.com/docs/5.6/database-testing#using-factories
yours would look like:
$factory->define(App\Employee::class, function (Faker $faker) {
return [
'FirstName' => $faker->name,
'LastName' => $faker->lastName,
'org'=> $position->org,
'user_id' => 1,
'pid' => function () {
return factory('App\Position')->create()->id;
}
];
});
This will create a position for each user the factory creates.
You could also use existing eloquent models instead if you have existing positions.

Minimize factories in test?

Currently I am using many factories factory() in Test class, is there a way to reduce to 1 so I can only use factory(Something::class) in a test method?
Reason I used many because I have to pass some foreign keys.
$user = factory(User::class)->create();
$token = factory(Token::class)->create([
'user_id' => $user->id,
]);
$provider = factory(Provider::class)->create([
'user_id' => $user->id,
'token_id' => $token->id,
]);
$something = factory(Something::class)->create([
'provider_id' => $provider->id,
]);
// Now test with $something
You can use such syntax:
$factory->define(Something::class, function ($faker) {
return [
'title' => $faker->title,
'content' => $faker->paragraph,
'provider_id' => function () {
return factory(Provider::class)->create()->id;
}
];
});
$factory->define(Provider::class, function ($faker) {
$user = factory(User:class)->create();
return [
'user_id' => $user->id,
'token_id' => function () {
return factory(Token::class)->create(['user_id' => $user->id])->id;
}
];
});
and then in your tests you can only use:
$something = factory(Something::class)->create();
Be aware depending on your needs it can cause some side effects - for example when using Provider factory, user will be always created what might be fine or not depending on your tests. Of course if needed you can always created helper method that will wrap all those methods you showed and return only something and then in your test you can only use:
$something = $this->createSomething();

Use sometimes() function in Laravel 5 request class

In laravel 4, I used the sometimes() method as below:
$validator = \Validator::make(
\Input::all(),
array(
'name' => array('required'),
'recurrence' => array('required_if:recurring,on'),
)
);
$validator->sometimes('recurrence', 'integer|min:1', function($input) {
return $input->recurring == 'on';
});
Notice integer|min:1 are applied to recurring only if recurrence is presented.
In laravel 5, I tried to implement the validation as a request class:
class CreateProductRequest extends Request {
public function authorize(){
return true;
}
public function rules(){
return [
'name' => array('required'),
'recurrence' => array('required_if:recurring,on'),
];
}
}
Looks like from a request class I am unable to call sometimes() method. The idea is to avoid validation code at controller.
Ok, I have emulated the behaviour expected using a custom condition without be 100% sure weather is the best practice:
$rules = [
'name' => array('required'),
'recurrence' => array('required_if:recurring,on'),
];
if ($this->has('recurring')){
$rules['recurrence'] = $rules['recurrence'] + ['integer', 'min:1'];
}
return $rules;

Categories