Reputation: 33
hello i have this in my one controller let say this is my customer controller
$allCustomers= Customer::all();
foreach ($allCustomers as $customer){
// Created At In Days //
$customer_created_at= $customer->created_at;
$now = Carbon::now();
$created_at_in_days = $customer_created_at->diffInDays($now);
if ($created_at_in_days >= 0 and $created_at_in_days <= 90) {
Customer::whereId($customer->id)->update(['collection_level' => 'Pre Collection',]);
} elseif ($created_at_in_days >= 91 and $created_at_in_days <= 120) {
Customer::whereId($customer->id)->update(['collection_level' => 'Collection Level 1',]);
} elseif ($created_at_in_days >= 121 and $created_at_in_days <= 150) {
Customer::whereId($customer->id)->update(['collection_level' => 'Collection Level 2',]);
} elseif ($created_at_in_days >= 151) {
Customer::whereId($customer->id)->update(['collection_level' => 'Collection Level 3',]);
}
}
this works how i want this to work but the problem i get is
that i also have a agent Controller and a client Controller where agent can work on customer accounts if that makes sense and client can see there customers
now if i copy and paste that code in all of the controller it works like it suppose to work but i would like to know how i can update that at one place without copy and pasting it in every method view and controller because now i have to copy it inside the index method in the show method in all the controllers
i just want to let it update if the created at days is past as certain day then the STATUS(or in this case the collection level)
i want it to check the records and update it every-time an agent or client is loading the view basically
i have found one other way by running a php artisan comand and let it run everyminute but i need it to check every second
protected function schedule(Schedule $schedule)
{
$schedule->command('second.update')->everyMinute();
}
public function handle()
{
$allCustomers= Customer::all();
foreach ($allCustomers as $customer){
// Created At In Days //
$customer_created_at= $customer->created_at;
$now = Carbon::now();
$created_at_in_days = $customer_created_at->diffInDays($now);
if ($created_at_in_days >= 0 and $created_at_in_days <= 90) {
Customer::whereId($customer->id)->update(['collection_level' => 'Pre Collection',]);
} elseif ($created_at_in_days >= 91 and $created_at_in_days <= 120) {
Customer::whereId($customer->id)->update(['collection_level' => 'Collection Level 1',]);
} elseif ($created_at_in_days >= 121 and $created_at_in_days <= 150) {
Customer::whereId($customer->id)->update(['collection_level' => 'Collection Level 2',]);
} elseif ($created_at_in_days >= 151) {
Customer::whereId($customer->id)->update(['collection_level' => 'Collection Level 3',]);
}
echo 'Done';
}
Sorry for my bad English and i hope it make sense
Upvotes: 0
Views: 440
Reputation: 3045
As I understand, you want to show the collection_level
to the user.
Then you don't have to update the customer anytime a user view a web page. That's inefficient and unnecessary.
First, to simplify the code, lets create a custom attributes in the customer model.
class Customer extends Model
{
public function getExistingDaysAttribute()
{
return $this->created_at->diffInDays(now());
}
public function getCollectionLevelAttribute()
{
if ($this->existing_days >= 0 and $this->existing_days <= 90) {
return 'Pre Collection';
} elseif ($this->existing_days >= 91 and $this->existing_days <= 120) {
return 'Collection Level 1';
} elseif ($this->existing_days >= 121 and $this->existing_days <= 150) {
return 'Collection Level 2';
} else {
return 'Collection Level 3';
}
}
}
Then, in any view, you can simply call the collection_level
custom attribute.
@foreach($customers as $customer)
<div>{{ $customer->collection_level }}</div>
@endforeach
Upvotes: 1