Related
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);
});
ProductsRequest.php code:
public function rules()
{
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this -> product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this -> product_id,
'category_id' => 'required
|exists:categories,id',
'seasons_id' => 'required
|exists:seasons,id',
// REQUIRED IMAGE ONLY IN STORE
'image' => 'required
|image|mimes:'.trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}
These rules apply for both store and update methods.
Problem is:
I want the image to be required only on store and not required in update, since user can just update the product basic info without choosing a new image for the product every time he update the product.
What I have tried:
I have tried to create two different ProductsRequest one for store and other for update but I know that this achievement is not the best achievement because my code must be DRY.
Use required_without rules
If primary key and element with name is id exist in your array
'image' => 'required_without:id`
If primary key and element with name is product_id exist in your array
'image' => 'required_without:product_id`
You can get more detail from laravel validation
You can do this in your ProductsRequest file;
public function rules()
{
if(request()->isMethod('put')) // could be patch as well
{
// Update rules here - Don't require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,
'category_id' => 'required|exists:categories,id',
'seasons_id' => 'required|exists:seasons,id',
// REQUIRED IMAGE ONLY IN STORE
'image' => 'required|image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}else{
// store rules here - require image here
return [
'name' => 'required
|min:'.trans('validation_standards.names.min').'
|max:'.trans('validation_standards.names.max').'
|unique:products,name,'.$this->product_id,
'barcode' => 'size:'.trans('validation_standards.barcode.size').'
|unique:products,barcode,'.$this->product_id,
'category_id' => 'required|exists:categories,id',
'seasons_id' => 'required|exists:seasons,id',
// REQUIRED IMAGE ONLY IN STORE
'image' => 'image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size'),
'description' => 'nullable
|min:'.trans('validation_standards.descriptions.min').'
|max:'.trans('validation_standards.descriptions.max'),
];
}
}
}
public function rules()
{
$image = request()->isMethod('put') ? 'nullable|mimes:jpeg,jpg,png,gif,svg|max:8000' : 'required|mimes:jpeg,jpg,png,gif,svg|max:8000';
return [
'image' => $image,
];
}
If your route is something like this one (which should be)
Route::post('products/{id}/update', 'ProductController#updateProduct')->name('products.update');
And you will call this route as
route('products.update', $product->id);
$product->id will be available in your form request and you can validate your form request for both create and update like this.
public function rules() {
return [
// All other rules
'image' => $this->id == null ? 'required|image|mimes:'.
trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size') :
'image|mimes:'.trans('validation_standards.images.extensions').'
|max:'.trans('validation_standards.images.file_size')
];
}
Just this few lines can solve your problems...
You have to check there image have or not, like this.
Rules in a private or protected function
private function validateRequest($request)
{
//This is for Update without required image, this will check that In DB image have or not
$product = Product::find($request->product_id);
$rules = [];
if ($product) :
if ($product->product_image == null):
$rules['product_image'] = 'required|image|max:1999';
endif;
//This is for regular validation
else :
$rules = [
'category_id' => 'required|integer|not_in:-- Select Category --',
'product_image' => 'required|image|max:1999',
];
endif;
return $rules;
}
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.
I'm trying to save multiple records via
AppSettings::create(
[
'name' => 'mail_host',
'type' => $emailsettingstype->id,
'value' => '',
],
[
'name' => 'mail_port',
'type' => $emailsettingstype->id,
'value' => '',
],
[
'name' => 'mail_username',
'type' => $emailsettingstype->id,
'value' => '',
],
);
But from the above, only the first array is getting created. Where am i going wrong? Any help is appreciated.
I think this should do
AppSettings::createMany([
[
'name'=>'mail_host',
'type'=>$emailsettingstype->id,
'value'=>'',
],
[
'name'=>'mail_port',
'type'=>$emailsettingstype->id,
'value'=>'',
],
[
'name'=>'mail_username',
'type'=>$emailsettingstype->id,
'value'=>'',
],
]);
Make sure you're passing an array of arrays, not a params of array.
UPDATE, you can use Model::insert() although according to what I've read, that method doesn't create/update the timestamps.
You can just use Eloquent::insert() link as below:
AppSettings::insert([
[
'name'=>'mail_host',
'type'=>$emailsettingstype->id,
'value'=>'',
],
[
'name'=>'mail_port',
'type'=>$emailsettingstype->id,
'value'=>'',
],
[
'name'=>'mail_username',
'type'=>$emailsettingstype->id,
'value'=>'',
],
]);
The problem with above is that it won't update timestamps, find examples here
The Create many Method createMany is available on relationship check reference to this link and this documentation from laravel
so far my example look like this.
I have two models Pricing and AvailableService Model
Pricing Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pricing extends Model
{
protected $fillable = ["name", "price"];
public function available(){
return $this->hasMany(AvailableService::class, "pricing_id", "id");
}
}
And the AvailableServiceMode look like this
namespace App;
use Illuminate\Database\Eloquent\Model;
class AvailableService extends Model
{
protected $fillable = ["pricing_id", "service_id"];
public function service(){
return $this->belongsTo(Service::class, "service_id", "id");
}
}
So createMany operation look like this
$insertMany = Pricing::create(['name'=>request('name')]);
$insertMany->available()->createMany([
['service_id'=>1],
['service_id'=>2],
['service_id'=>3],
['service_id'=>4],
['service_id'=>5],
]);
And it works for, you can give it a try too. THANKS
If you want to store multiple record in seeder use this method instead of insert because in my case I want to slug automatically created using spatie/laravel-sluggable pkg. If you used the insert or DB technique then you have to give the value for slug field also.
CategorySeeder
<?php
namespace Database\Seeders;
use App\Servcategory;
use Illuminate\Database\Seeder;
class CategorySeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$categories = [
[
'name' => 'Automotive',
// 'slug' => 'automotive',
],
[
'name' => 'Business Services',
// 'slug' => 'business-services',
],
[
'name' => 'Computer, Telecom & IT Services',
// 'slug' => 'computer-telecom-&-it-services',
],
[
'name' => 'Education & Training',
// 'slug' => 'education-&-training',
],
[
'name' => 'Finance',
// 'slug' => 'finance',
],
[
'name' => 'Hospitals, Clinic, Medical',
// 'slug' => 'hospitals-clinic-medical',
],
[
'name' => 'Real Estate, Construction, Property',
// 'slug' => 'real-estate-construction-property',
],
[
'name' => 'Travel,Toursim & Hotels',
// 'slug' => 'travel-toursim-&-hotels',
],
];
// Servcategory::insert($categories);
collect($categories)->each(function ($category) { Servcategory::create($category); });
}
}
In case some one searching for eloquent model, I used the following method:
foreach($arCategories as $v)
{
if($v>0){
$obj = new Self(); // this is to have new instance of own
$obj->page_id = $page_id;
$obj->category_id = $v;
$obj->save();
}
}
$obj = new Self(); is a must otherwise it only saves single record when $this is used.
in seeder create an array and do foreach with Model::create(). All your records will be with timestamps
protected $array = [
[...],
[...],
[...]
];
public function run()
{
foreach ($this->array as $value) {
Model::create($value);
}
}
i am need to sort some fields (asc,desc) in GridView, but same fields are calculated. Look at code below:
SearchModel:
class ObjectSearch extends Object {
use SearchModelTrait;
public function rules()
{
return [
['id', 'integer', 'min' => 1],
];
}
public function search($params)
{
$this->company_id = \Yii::$app->user->identity->companyId;
$query = Object::find()->where(['company_id' => $this->company_id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);
$dataProvider->setSort([
'attributes' => [
'id',
'name',
'lastReportResult' => [
'asc' => ['lastReportResult' =>SORT_ASC ],
'desc' => ['lastReportResult' => SORT_DESC],
'default' => SORT_ASC
],
'reportPercentDiff'
]
]);
if (!($this->load($params,'ObjectSearch') && $this->validate())) {
return $dataProvider;
}
$this->addCondition($query, 'id');
return $dataProvider;
}
Methods in Object model:
public function getLastReportResult()
{
$lastReport = $this->getLastReport();
$message = 0;
if (!empty($lastReport)) {
$statistic = new ReportStatistic($lastReport);
$message = $statistic->getPercent();
}
return $message;
}
/**
* #return int
*/
public function getReportPercentDiff()
{
$lastReport = $this->getLastReport();
$message = 0;
if (!empty($lastReport)) {
$statistic = $lastReport->getReportDiff();
if (!empty($statistic['diff'])) {
$message = $statistic['diff']['right_answers_percent_diff'];
} elseif (!empty($statistic['message'])) {
$message = $statistic['message'];
}
}
return $message;
}
So, by this methods, i am calculating a values of two fields, which are need's sorting. This way doesn't working, i have a Database Exception, because object table hasn't this fields. exception
How to do sorting of this fields ?
Update: I am the author of this answer and this answer is not accurate. Preferred way is to use database view
Add two public properties to ObjectSearch.php and mark it as safe
class ObjectSearch extends Object {
use SearchModelTrait;
public $lastReportResult, $reportPercentDiff;
public function rules()
{
return [
['id', 'integer', 'min' => 1],
[['lastReportResult', 'reportPercentDiff'], 'safe']
];
}
public function search($params)
{
$this->company_id = \Yii::$app->user->identity->companyId;
$query = Object::find()->where(['company_id' => $this->company_id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);
$dataProvider->setSort([
'attributes' => [
'id',
'name',
'lastReportResult' => [
'asc' => ['lastReportResult' =>SORT_ASC ],
'desc' => ['lastReportResult' => SORT_DESC],
'default' => SORT_ASC
],
'reportPercentDiff' => [
'asc' => ['reportPercentDiff' =>SORT_ASC ],
'desc' => ['reportPercentDiff' => SORT_DESC],
'default' => SORT_ASC
],
]
]);
if (!($this->load($params,'ObjectSearch') && $this->validate())) {
return $dataProvider;
}
$this->addCondition($query, 'id');
return $dataProvider;
}
Then in index.php (view file in which you are having grid view) add lastReportResult and reportPercentDiff in array of all attributes (list of all attributes ob Object model)
...
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// your other attribute here
'lastReportResult',
'reportPercentDiff',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
...
For more info you can visit Kartik's blog at Yii
Though this is an old thread, stumbled upon this and tried to find other method to achieve sorting of purely calculated field to no avail... and this post unfortunately is not an answer as well... It just that I feel the need to post it here to give a heads up to those that still looking for the solution so as not to scratch their heads when trying the solution given and still fail.
The given example from documentation or referred links as far as I have tested only works if you have a column within the database schema (whether in the main table or the related tables). It will not work if the virtual attribute/calculated field you create is based on calculating (as an example multiplication of 2 column on the table)
e.g:
table purchase: | purchase_id | product_id | quantity |
table product: | product_id | unit_price |
then, if we use a virtual attribute 'purchase_total' for model 'purchase' which is the multiplication of quantity and unit_price (from the join table of purchase and product on product_id), eventually you will hit an error saying 'purchase_total' column can not be found when you tried to sort them using the method discussed so far.