Creating a structure with Jobs, Events, Listeners, Queues in Laravel 8 - laravel

I have a flow about that. I can solve the problem on paper. As a system admin, we have the following models: Clients, Departments, Users, Visitors, Calls, and Meetings.
Clients are our customers.
Departments are customers' departments.
Users have many roles, but we inspect the "Agent" role to define the
agent as a call center worker.
Visitors are those who want to meet
with an active agent by selecting a client and department.
Calls are a log table. If visitors can find the agent and send data to the
agent, I store data in the call table.
Meetings If the agent accepts the call, I store it in the meeting table and notify the
agent and visitor.
Client.php (Model)
public function users(){
return $this->hasMany(User::class);
}
public function departments(){
return $this->hasMany(Department::class);
}
Department.php (Model)
public function department_users(){
return $this->belongsToMany(User::class, 'department_user');
}
public function client(){
return $this->belongsToMany(Client::class, 'client_id');
}
User.php (Model)
public function departments(){
return $this->belongsToMany(Department::class, 'department_user');
}
Visitor.php (Model)
public function department(){
return $this->belongsTo(Department::class);
}
So as you can see, we define the relationships, and we want to do something like that:
Here is the flow...
The question is, how can I do that in Laravel 8?

Related

how to get data from polymorph in laravel

i'm have a order able in DB that has related to product and service and other models, between these tables and models there is a many to many polymorph
i want to show client all his order
in future, maybe, the model change in name or amount
is there anyway to use namespace of model that is stored middle table instead specific method?
for exammple i dont want use
public function products()
{
return $this->morphedByMany(Product::class, 'orderable');
}
public function services()
{
return $this->morphedByMany(Service::class, 'orderable');
}
and etc
instead:
public function morhToAll(){
?????
}
and then for example i can:
$orderItems=auth()->user()->orders()->morhToAll
foreach(orderItems as $item){
.....
}
the usr can see all his orders and their details.
i want to show client his/her orders details

Laravel Polymorphic get all models associated to a single model

I have an Attachment model which uses the morphs table and morphTo function. I have several models but the main ones include Client, Job, Project and Notes (which is also polymorphic) which can contain several Attachments.
A client is the top-level model. A Client has many Jobs. A Job has many Projects.
I am struggling with a single way to return all attachments of a client, including attachments of each Job, Project, and notes of each job/project under a client.
I currently am running several foreach loops and have a working way, but the queries on the page load range from 60-100 depending on the amount of jobs/projects/notes for each client. I run through each job to check if it has an attachment, if so, I loop through them. Then, I run through $job->notes->attachments and display those. From there, I dive into another foreach loop pulling all the job's projects, pulling the attachments from each project and then pulling all the notes and looping through that.
Is there a way within Laravel to get all of the Attachments that are somehow attached to a single Client without looping through the way I have? If not, is there a way I can optimize my loops so I don't have to request the attachments for each job/job's notes/project/project's notes?
I do this all the time. You just need a way to
"...get all of the Attachments that are somehow attached to a single
Client without looping through..."
You must consider custom joins, using Laravel Eloquent:
//client_id input here
$client_id = 10;
$att_from_client = Attachment::join('note', function ($join) {
$join->on('note.id', '=', 'attachment.object_id')
->where('attachment.object_type', 'App\\Note');
})
->join('project', 'project.id', '=', 'note.project_id')
->join('job', 'job.id', '=', 'project.job_id')
->join('client', 'client.id', '=', 'job.client_id')
->where('client.id', $client_id)
->get();
dd($att_from_client);
My advice is to use eloquent-has-many-deep. As example of you can do with that library you can look at the code of three models related with many to many:
class Role extends Model
{
public function users()
{
return $this->belongsToMany('App\Models\User')->withTimestamps();
}
public function permissions()
{
return $this->belongsToMany('App\Models\Permission')->withTimestamps();
}
}
class Permission extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function roles()
{
return $this->belongsToMany('App\Models\Role')->withTimestamps();
}
public function users()
{
return $this->hasManyDeep('App\Models\User', ['permission_role', 'App\Models\Role', 'role_user']);
}
}
class User extends Authenticatable
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function roles()
{
return $this->belongsToMany('App\Models\Role')->withTimestamps();
}
public function permissions()
{
return $this->hasManyDeep('App\Models\Permission', ['role_user', 'App\Models\Role', 'permission_role']);
}
}
With these relationships in place and 5 tables involved: users, role_user, roles, permission_role and permissions you can retrieve all the permissions of a User model with a call to $user->permissions, that resolves to only one query with all the joins needed.

Laravel ORM relationship - manager-employee over the same table of users

I have a user_managers pivot table that gets both keys from the users table:
employer_user_id
employee_user_id
I believe users would have a many to many relationship as a user can be managed by more than 1 manager there will be users who manage 1 or more users, while other users (excluding those under them) would manage them while there will be users who don't manage at all and a User can have only 1 manager.
My first try at defining this was to build another model named Manager representing the user_managers pivot table, so in User model I wrote the following 2 functions:
public function managedBy()
{
return $this->belongsToMany('Manager', 'employer_user_id');
}
public function manages()
{
return $this->hasMany('Manager', 'employee_user_id', 'employer_user_id');
}
Does this make sense or do you know of a better way to implement this kind of structure?
If a user can have only 1 manager then you can define you relationship as one to many like
//User model
public function managedBy()
{
return $this->belongsTo(User::class, 'manager_id');
}
public function managees()
{
return $this->hasMany(User::class, 'manager_id');
}
you don't need to pass $id to your relationship definition.
For multiple managers, Yes you would need a many to many relationship by adding a junction/pivot table which i guess you already have user_managers, Now you need to define your relationships using belongsToMany for managers and mangees like
public function managers()
{
$this->belongsToMany(User::class, 'user_managers', 'employer_user_id')
}
public function managees()
{
$this->belongsToMany(User::class, 'user_managers', 'employee_user_id')
}

Create multiple model in laravel to belongsToMany

I have 2 models, Service and Schedule.
Schedule model has this relationship:
public function service() {
return $this->belongsToMany('App\Service','services_has_schedules','services_service_id','schedules_schedule_id);
}
Service model have this relationship:
public function schedule() {
return $this->belongsToMany('App\Schedule','services_has_schedules','services_service_id','schedules_schedule_id');
}
In frontend, this send me arrays day[] (day name), init[] (open hour), finit[] (close hour)
In the controller I want to know how to create multiple Schedule to sync() with Service
You can use syncWithoutDetaching to keep the existing records and add the new ones.
If you do not want to detach existing IDs, you may use the
syncWithoutDetaching method:
$user->roles()->syncWithoutDetaching([1, 2, 3]);
https://laravel.com/docs/5.6/eloquent-relationships

Laravel - Multiple Arrays for Multiple Recipients (Mailable/Notifications)

I'm looking for advice for a project and an interesting predicament I've put myself in.
I have multiple fields in my shipment model, they are:
shipto_id
shipfrom_id
billto_id
They all link (through different relationships) to my customer model:
public function billtoAccount()
{
return $this->belongsTo('App\Customer', 'bill_to');
}
public function shiptoAccount()
{
return $this->belongsTo('App\Customer', 'ship_to');
}
public function shipfromAccount()
{
return $this->belongsTo('App\Customer', 'ship_from');
}
and these customers (in reality would likely be better described as customer accounts (these are NOT USER ACCOUNTS, they're more just like a profile for each company that business is done with)) can have a multitude of users associated with them.
public function users()
{
return $this->belongsToMany(User::class);
}
Now, while I know how to send off mailables and notifications, I was curious to know how I would go about sending off those to multiple user's emails. So let me describe the following: Something is created and the following customers (and in turn, their users) are referenced.
(billto_id) - Customer Account 1 - User 1 (email1#example.com)
(shipto_id) - Customer Account 2 - User 2 (email2#example.com) & User 3 (email3#example.com)
(shipfrom_id) - Customer Account 37 - User 6 (email4#example.com)
Now, how would I go about moving the emails of the users over to an array of emails to have a notification or mailable sent to them?
So it should pop out: email1#example.com, email2#example.com, email3#example.com, email4#examples.com
Elaborating on #Devon 's comment:
This is business logic. You could have a method on your Shipment model that returns the customer instances to be notified as an array, e.g. getNotifiables() : array
Then in your Customer model you may use the Notifiable trait
use Illuminate\Notifications\Notifiable;
And looping over your Notifiables, i.e. Customers
$notification = new ShipmentWasCreated()
foreach ($shipment->getNotifiables() as $notifiable) {
$notifiable->notify($notification);
}
Better alternative:
Notification::send($shipment->getNotifiables(), new ShipmentWasCreated());

Resources