Automating a update function in laravel - php

I have a table called users in my laravel application database.
There is a column called active_status in the user's table
if the active_status=0 then user is inactive and active_status=1 user is active.
Also there is a column called, user_score in the users table. If the user score is less than 50 user's active_status should change to 0 and greater than 50 then user will remain as active user.
I have already written an update function to change this active_status but How can I automate the process, checking the user_score and updating the active_status accordingly?

You are reffering something related to the laravel task scheduling.
Here is the official documentation.

I would prefer using Event is this case - since it is fully Model Related:
You can set up your listener following the instructions here:
https://laravel.com/docs/7.x/events#registering-events-and-listeners
You can listen to the Model Update event as seen here:
https://laravel.com/docs/master/eloquent#events

It's better if you can use eloquent observers to do this.
https://laravel.com/docs/7.x/eloquent#observers

Related

Laravel hasActiveOneOfMany relation

Obviously this does not exist but I am looking for a way to implement it.
Example, I am playing a game where I have 3 teams but I can only be actively playing for one team at any time and when I switch team I essentially starting from the beginning but I would like to retain the progress if I switch team.
My tables would look something like this:
Users:
active_team_id
TeamProgress:
user_id
team_id
team_level
team_xp
TeamNames:
id
team_name
So there would only be 3 teams defined in TeamNames and while any one of these 3 are active (as set in active_team_id in the users table) then I want to be able to directly target it with something like
$user->teamProgress->update()
and update the active team directly.
Similarly I would like to do something like
$user->teamProgress->get()
and just get the team progress for that users active team while a few others may exist within team progress.
Then if I switch the active_team_id on the user table and do the same calls above I now update / get info for the second team specified in active_team_id
Is there a way to do this with a relation or am I overthinking this and better off doing this by just directly targeting the TeamProgress model using the information I already have as a user?
$teamProgress->where('user_id', $user->id)->where('team_id', $user->active_team_id)->get()
You can try this package https://github.com/topclaudy/compoships. It allows you to define relations by matching multiple columns. For example
class User extends Model
{
use \Awobaz\Compoships\Compoships;
public function activeTeamProgress()
{
return $this->hasOne(TeamProgress::class, ['id', 'active_team_id'], ['user_id', 'team_id']);
}
}

How do I make a notification table that makes eloquent relationships to other tables without making too many "<source>_id" columns

Suppose that I have a Notification Table that gets generated when a new log from another table is generated. Suppose I have 3 different logs with different purpose namely: sms_logs, call_logs, and appointment_logs.
I want to make a relationship to each logs without using sms_logs_id, call_logs_id and appointment_logs_id. Instead, I want to build only two columns, one for the type, and the other for the ID. So for example an sms log is generated with an id of 187, it will also generate a notification log with a notification_id of 187 and a type of "sms".
How will I be able to create that? Thank you!
Nice question.
You have to put only two fields in notifications table. foreign_id and log_type.
Whenever you add a log, you have to set log_type accordingly. Then add this relationship in your Notification model.
public function foreignModel()
{
switch($this->log_type){
case "call_log":
return $this->belongsTo('App\Call', 'foreign_id');
break;
}
}
I didn't tried it, but hope it will work fine.
If you are looking for something more dynamic and less robust than this then I don't think that it exists.

Eloquent querying 1:n relationship

I have 2 tables in my database, a notifications table and a notifications_user table.
A notification can have many notification users. And a result in my notification model I have the following relationship,
public function user()
{
return $this->belongsToMany('User')->withPivot('is_read');
}
What I am wanting to achieve is to get all notifications that are unread (or is_read = 0), the is_read column is in the notification user table though and I cannot work out how to run a query on it becuase of that.
Current I have this,
$unread = Notification::has('user')->with('user')->get();
Now this pulls the relationship into the results, but takes no account of the is_read value.
Is there a way to select all the data from a table based on a value in its 1:n relationship.
You can query on the relationship by doing something like:
$unread = Notification::has('user')->with('user')->where('is_read','!=', 0)->get();
Check the docs: http://laravel.com/docs/5.1/eloquent-relationships#querying-relations
So while it may make sense to have the relation that notifications have many users it really doesn't make sense from an ownership. Notifications are one-time things while users persist in a way. So try to think of users as the base object and notifications as the abundant resource.
The goal here is to get the users notifications so out of this you have to choose whether to duplicate notifications for each user or have one notification for many users. In one case where it's universal notifications (admin panel maybe) and the other is notifications that are personal to the user. If you're doing the latter you don't really need a pivot table and just a notifications table.
User -> hasOne -> Notification
Notification -> belongsTo -> User
This enables you to really customize the notifications per-user than relying on maybe another table for read notifications you can just mark it as "read" in the row.
If however you need universal notifications the structure just implements a third table called a pivot as you know. (I noticed you have the class names pluralized which is not recommended)
User -> Notification_User -> Notification
For ease you also could just soft-delete the notification_user row or notification themselves. You can simply just say ->withTrashed()->limit(x) to get previous notifications.
This really simplifies the work done by the DB and your code. The personal notifications allows you to order by creation/update and deal with read in two ways, soft-deletion and IsRead variables.
Your code becomes as simple as this.
Auth::user()->notifications()
Your User class has the following (assuming standard naming schemes)
public function notifications()
{
//You're free to append other requirements here
return $this->hasMany('App\Notification','id','user_id');
}
The Notification class has the inverse
public function user()
{
//You're free to append other requirements here
return $this->belongsTo('App\User','user_id','id');
}
If for some reason you require to know all unread notifications universally just query the Notification table.
Notification::where('isRead','null')->get();
Alternatively you can lazy load the users for each notification or group by users in this case for whatever purpose you need.
If this helped you to your solution could you mark it as the answer?
You can use the wherePivot and orWherePivot functions provided by laravel for relations. Link

PHP User based Data Edit and Delete Using YII

I am working on PHP YII (Version 1.15) User management application.
My Scenario: A user can be assigned as supervisor and there are 'n' numbers of subordinates can assign to the user. Likewise, there are many supervisor available in the application. All users details falls in one table.
Objective: I want to give access to the supervisor to Edit and Delete only to the assigned subordinates. Other users data, he can't able to modify.
What I Tried: I am validating the call everytime before the supervisor clicks the edit button of the subordinates. (I have many forms, so have to use the code many places repeatedly)
Please help me to solve this issue.
Is it possible for you to add some column to your user table?
If it's possible, you should add one column in your user table that indicates the user's supervisor. It can contain the supervisor ID. Supervisors have null value for this column, so this column should be nullable. You can use the value of this column to validate supervisor's access to Edit and Delete by matching the ID of supervisor with this column's value.

CakePHP - conditions on associated model

Problem in short: I want to set
conditions on an associated model. How
do I do it?
I'm having a problem with the associations in my CakePHP application.
The associations looks like this:
Event has many EventSum belongs to Account has many AccountUser belongs to User
Event has many EventDebt ... (the rest is the same as above)
Event also belongs to User
The application is a private econonmy program in PHP and uses the CakePHP framework.
An Event is a financial event, a purchase, transaction between accounts etc. It only holds information about date, title and user.
An EventSum holds information about an Account and how much to debit or credit (in one column, just positive or negative).
Account holds information about title of the account.
AccountUser holds an id of an Account and a User. This indicates that
So, now I want to fetch Events based on what accounts a User is associated to. How can I do this?
I want to fetch the following info:
Event, together with the EventSum. The Events are fetched from Accounts where the User has access.
Thanks for any help,
/Magnus
It seems like you want to be able to query your Event class with the following conditions:
'Account.user_id =' => $userId
Is my assumption correct?
When doing queries which require conditions on associated models, you can use either the Containable behaviour (comes with CakePHP 1.3) or the 'Linkable' behaviour (which can be found here).
What happens when you try this (be sure to attach the Containable behaviour to your models first):
$condition = array('Account.user_id =' => $userId);
$contain = array('EventSum' => array('Account'), 'EventDebt' => array('Account'));
$result = $this->Event->find('all', compact('condition', 'contain'));
Note that you might experience issues when 'containing' both EventSum and EventDebt if both of their associations to Account use the same alias name.

Categories