how to make laravel seeder for multable tables? - faker

I am using Laravel 7.
Question) inside Job factory, I need to create [0 to 10 ]
companies per Profile, how to do it?
'companyName' => $faker->company()->randomElement[0, 10],
What's wrong in this code? Thank you.
use App\Model;
use Faker\Generator as Faker;
use App\Job;
use App\Profile;
$factory->define(Job::class, function (Faker $faker) {
return [
'companyName' => $faker->company()->randomElement[0, 10],
'profile_id' => function () {
return Profile::inRandomOrder()->first()->id;
},
];
});
-- 'companyName' => $faker->company() -- this part is okey.

I found the way:
in JobFactory:
'companyName' => $faker->company(([0, 10])),
and in JobTableSeeeder
public function run()
{
$usersQuantity = 100;
factory(Job::class, $usersQuantity)->create();
}

Related

Custom field not saved

I try to add a custom user field to the user by using WPGraphQL. Therefore I tried to recreate the example in the official WPGraphQL documentation https://docs.wpgraphql.com/extending/fields/#register-fields-to-the-schema :
add_action('graphql_init', function () {
$hobbies = [
'type' => ['list_of' => 'String'],
'description' => __('Custom field for user mutations', 'your-textdomain'),
'resolve' => function ($user) {
$hobbies = get_user_meta($user->userId, 'hobbies', true);
return !empty($hobbies) ? $hobbies : [];
},
];
register_graphql_field('User', 'hobbies', $hobbies);
register_graphql_field('CreateUserInput', 'hobbies', $hobbies);
register_graphql_field('UpdateUserInput', 'hobbies', $hobbies);
});
I already changed the type from \WPGraphQL\Types::list_of( \WPGraphQL\Types::string() ) to ['list_of' => 'String'].
If I now execute the updateUser mutation my hobbies don't get updated. What am I dowing wrong?
Mutation:
mutation MyMutation {
__typename
updateUser(input: {clientMutationId: "tempId", id: "dXNlcjox", hobbies: ["football", "gaming"]}) {
clientMutationId
user {
hobbies
}
}
}
Output:
{
"data": {
"__typename": "RootMutation",
"updateUser": {
"clientMutationId": "tempId",
"user": {
"hobbies": []
}
}
}
}
Thanks to xadm, the only thing I forgot was to really mutate the field. I was a bit confused by the documentation, my fault. (I really am new to WPGraphQL btw)
Here's what has to be added:
add_action('graphql_user_object_mutation_update_additional_data', 'graphql_register_user_mutation', 10, 5);
function graphql_register_user_mutation($user_id, $input, $mutation_name, $context, $info)
{
if (isset($input['hobbies'])) {
// Consider other sanitization if necessary and validation such as which
// user role/capability should be able to insert this value, etc.
update_user_meta($user_id, 'hobbies', $input['hobbies']);
}
}

How join two Vuefire collection?

I have two queries in Vue.js with Firebase (vuefire). This queries have similar datas. I want somehow join it, for later iterate.
watch: {
searchQuery: {
handler(searchQuery) {
if (searchQuery) {
this.$bind('logos1', logosCollection
.where('tags', 'array-contains-any', [this.searchQuery]))
this.$bind('logos2', logosCollection
.where("name", '>=', this.searchQuery)
.where("name", '<=', this.searchQuery+'\uf8ff')
.orderBy('name')
);
this.logos= ====> SOMEHOW LOGOS1+LOGOS2
}
}
},
Is there any method to do this?
I've built a little helper for that using $watch.
export function bindCombineArrays(that, combined, props, sort) {
console.log(`bindCombineArrays "${combined}" <== ${props}`);
let combine = () => {
let arr = props.reduce((res, p) => res.concat(that[p]), []);
if (sort)
arr = sort(arr);
console.log(`refreshing combined "${combined}" =`, arr);
that.$set(that, combined, arr);
};
// watches each property
props.forEach((p) => { that.$watch(p, combine); });
}
Example:
this.$bind("convA", db.collection(`conversations`).where("u1.id", "==", this.eventUser.id));
this.$bind("convB", db.collection(`conversations`).where("u2.id", "==", this.eventUser.id));
bindCombineArrays(this, "conversations", ["convA", "convB"]);

Dispatch an action after another is executed in Effect

I Have the following function in my code
private loadDrones() {
this.store$.dispatch(new UsedDronesStoreActions.GetUsedDronesRequest({organizations_id : environment.organizationId}));
this.store$.pipe(select(UsedDronesStoreSelectors.selectAll)).subscribe((drones) => {
drones.forEach((drone) => {
if (this.drones.has(drone.id)) { return; }
this.drones.set(drone.id, drone);
this.store$.dispatch(new UsedDronesStoreActions.OpenUsedDroneUpdatePositionChannelRequest({ droneId: drone.id, projectId : environment.projectId }));
this.store$.dispatch(new UsedDronesStoreActions.OpenUsedDroneUpdateStatusChannelRequest({ droneId: drone.id, projectId : environment.projectId }));
});
});
}
I would like to move this function into an INIT Action in my effect.
Using
#Effect()
init$ = this.actions$.pipe(
ofType(ROOT_EFFECTS_INIT),
map(action => ...)
);
My question is, , the actual function load a list, and then thank to the result of that list, dispatch a serie of actions for each element of the list.
Is it possible to make this inside a single effect ? Or do I have to split it up in 2 different actions ?
https://medium.com/#amcdnl/dispatching-multiple-actions-from-ngrx-effects-c1447ceb6b22
#Effect() save = this.update$.pipe(
map(action => action.payload),
switchMap(payload => this.myService.save(payload)),
switchMap(res => [
new Notification('save success'),
new SaveSuccess(res)
])
);

Testing form with CKEditor

I want to test my ArticleForm which contains CKEditor field:
$builder->add('content', CKEditorType::class, array(
'config' => array('uiColor' => '#ffffff'),
'required' => true));
However when I run PHPUnit I got the following error:
Argument 1 passed to Ivory\CKEditorBundle\Form\Type\CKEditorType::__construct()
must be an instance of Ivory\CKEditorBundle\Model\ConfigManagerInterface, none given
My test config is the same as for dev and prod where CKEditor works fine:
ivory_ck_editor:
default_config: default
configs:
default:
filebrowserBrowseRoute: elfinder
filebrowserBrowseRouteParameters: []
The test case extends Symfonys' TypeTestCase which creates its' own factory. This probably is the cause. However I don't know how to force this factory to provide the proper CKEditor instance. Does anybody know how to do it?
Problem solved with PreloadedExtension:
class ArticleTypeTest {
protected function getExtensions() {
return array(new PreloadedExtension(array($this->getCKEditor()), array()));
}
...
protected function getCKEditor() {
$configManager = $this->getMockBuilder ( ConfigManagerInterface::class )->disableOriginalConstructor ()->getMock ();
$pluginManager = $this->getMockBuilder ( PluginManagerInterface::class )->disableOriginalConstructor ()->getMock ();
$stylesSetManager = $this->getMockBuilder ( StylesSetManagerInterface::class )->disableOriginalConstructor ()->getMock ();
$templateManager = $this->getMockBuilder ( TemplateManagerInterface::class )->disableOriginalConstructor ()->getMock ();
$type = new CKEditorType($configManager, $pluginManager, $stylesSetManager, $templateManager);
return $type;
}
}

Processing custom NGINX log with logstash

I have nginx access log that log request body in the form of json string. eg.
"{\x0A\x22userId\x22 : \x22MyUserID\x22,\x0A\x22title\x22 : \x22\MyTitle\x0A}"
My objective is to store those 2 values (userId and title) into 2 separate fields in Elastic Search.
My Logstash config:
filter {
if [type] == "nginxlog" {
grok {
match => { "message" => "%{COMBINEDAPACHELOG} %{QS:partner_id} %{NUMBER:req_time} %{NUMBER:res_time} %{GREEDYDATA:extra_fields}" }
add_field => [ "received_at", "%{#timestamp}" ]
add_field => [ "received_from", "%{host}" ]
}
mutate {
gsub => [
"extra_fields", x22 ,'"' ,
"extra_fields",x0A ,'\n'
]
}
json{
source => "extra_fields"
target => "extra_json"
}
mutate {
add_field => {
"userId => "%{[extra_json][userId]}"
"title"=> "%{[extra_json][title]}"
}
}
}
}
But it's not properly extracted, the value in ES userId = userId, instead of MyUserID. Any clue where is the problem? Or any better idea to achieve the same?

Resources