How to call a laravel factory dynamically? - laravel

I have the following seeder.
User::factory(100)
->has(Loan::factory()->count(5))
->has(Transaction::factory()->count(5)
->has(TransactionDetail::factory()->count(1), 'transaction_detail'))
->create();
Now I want to call another Verification class based on a verified property in the User class. So verified could have three possible values - 'pending', 'unverified', 'verified'. Now when it's verified or pending, I want to create a Verification class. But when it's unverified, I don't want to call that class. Something like this...
User::factory(100)
->($user->verified !== 'unverfied') ? has(Verification::factory()) : null
->has(Loan::factory()->count(5))
->has(Transaction::factory()->count(5)
->has(TransactionDetail::factory()->count(1), 'transaction_detail'))
->create();
Now, I know this syntax doesn't make any sense. But I am just trying to explain my problem.
How do I call this verification factory based on $user->verified?
Thanks in advance.

Related

security with method not working

I'm trying to set up security on my entities in API Platform and found that I can call a method instead of the property example used in the docs (found this in some example somewhere on the web):
* attributes={"access_control"="is_granted('ROLE_USER') and object.belongsTo(user)"},
This is necessary in my case as I keep the user account data seperate from the user profile data and the entity in question is linked to the profile, not the user entity.
This works like charm on an individual getter (/api/data/{id}) but fails with a server 500 error on the list (/api/data):
"hydra:description": "Unable to call method \"belongsTo\" of object
\"ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator\".",
Wondering what is going wrong and how to fix it.
the "belongsTo()" method is fairly simple:
public function belongsTo(User $user) {
return $user == $this->owner->user;
}
Try this way
Create custom extension

Laravel Gate resource with policy not existing

I am trying to implement Policies in my project and I have a custom method askFriend that I want to add to my UserRelationPolicy.
So I implemented in my UserRelationPolicy the askFriend method but when trying to call it from the UserRelationPolicy#askFriend I asked myself how to call it from this method.
Something like $this->authorize('askFriend', $friend); but it was not working, kind of ignoring it at all. So I searched further in the documentation and found that I could bind with a Gate method the specific method in the UserRelationPolicy to a resource name like this :
Gate::resource('userrelation', 'UserRelationPolicy', [
'userrelation.askfriendrelation' => 'askFriendRelation'
]);
You can find the representation here : Documentation Writing Gate
When I try to execute this code I get the following error :
Call to undefined method Illuminate\Auth\Access\Gate::resource()
And nothing more. The Resource method doesn't seem to exist at all. After many search, trying to include every Gate in the header. Trying to call it staticly or with an instance. Nothing work and the method is nowhere near to be found...
Is it something forgotten ? How can I call a custom method from a controller in a policy class ?
Are you sure you are using 5.4? The method Gate::resource was implemented only in 5.4.
If you are using any version behind you will have to use the Gate::define.
Set the Gate abilities in the App\Providers\AuthServiceProvider like this:
Gate::define('userrelation.askfriendrelation', 'UserRelationPolicy#askFriend');

where do I apply logic code for user registration in laravel

I'm using custom registration in Laravel.
In register.blade.php the user gives the following data
$data['User_name','User_Coll_name','Branch_name','email','password']
And I want to create new user with Class_id
$class= DB::select('select * from classes where College_id = ? And Branchc_id = ? ',[$data['User_Coll_name'],$data['Branch_name']]);
I'm new to laravel please help me, where do I apply above logic. And also how to create new class if doesn't exist
$class = new class();
I can think of two ways of doing this.
1) In App/Http/Controllers/Auth/RegisterController.php, there is a create method. You can add your logic to create a new class and associate it with the user in this method.
2) My favorite way of doing it is by using Events & Listeners. So each time somebody has registered, you can fire the event and then through the listener, you do the appropriate logic.

Grails: object is being saved although it has errors

I'm having a problem while trying to validate and save domain object.
First I'm making the validation, when the validation is wrong, I'm putting error inside my future to be saved object, like this:
myDomain.errors.reject("An Error")
myDomain.discard()
Then, when I'm trying to save this object, I can see that error list has one error but 'validate()' returns 'true', also, when the function is finished the object is being saved automatically.
I must say that all the called functions are in the same class which is a controller.
I need to know how to code my save function (in the controller class) which shows only the error without saving the object, and when the validation is good, to save the object.
Thanks!
myDomain.validate() will overwrite myDomain.errors clearing the reject(). You could do something like this:
// bind etc.
...
// do grails validation
if (!myDomain.validate()) {
myDomain.discard()
}
// do custom validation
if (custom validation has errors) {
myDomain.errors.reject ()
myDomain.discard()
}
If you can move you custom validation into a validator on myDomain you do not need the custom validation.
You don't need to call save() explicitly.
The fix was to use:
#Transactional(readOnly = true)
For the function which located in the service file.
It makes the function to avoid from saving transactions in the end of it, which caused the problem in the first place.

Axapta: Validate Access to return value from display method

The Dynamics AX 2009 Best Practice add-in is throwing the following error on a display method override.
"TwC: Validate access to return value from the display/edit method."
Here is my display method.
display ABC_StyleName lookupModuleName(ABC_StyleSettings _ABC_StyleSettings)
{
;
return ABC_Styles::find(_ABC_StyleSettings.StyleID).StyleName;
}
I'm assuming it wants me to check a config or security key before returning a result. Any suggestions/examples on where to start?
Thanks
This is a reminder that you need to consider whether the user should have access to the data you are returning from the function. For table fields, the kernel normally does this for you based on the security groups the user is in and the security keys set on fields.
To check if a user has access to a field, use the hasFieldAccess function. To see how this is used, look at the table methods BankAccountStatement.openingBalance() or CustTable.openInvoiceBalanceMST(). There are other helper functions to check security keys such as hasMenuItemAccess, hasSecuritykeyAccess, and hasTableAccess.
In your case, add this code:
if(!hasFieldAccess(tablenum(ABC_Styles),fieldnum(ABC_Styles,StyleName)))
{
throw error("#SYS57330");
}
Even after you add that code, you will still get the Best Practice error. To tell the compiler you have addressed the issue, you need to add the following comment immediatly before the function declaration:
//BP Deviation Documented

Resources