How to retrieve data from database with laravel and multiply by a certain percentage ever24 hours
Hello guys, please I am building a laravel app where users make payment online and the amount is stored on the database. I want to be able to call the amount from the database so it displays on the specified user's dashboard that made the payment.
Secondly, I want the amount to multiply by a certain percentage every 24 hours.
Please any help or assist on how to go around this... I'm new to php
That depends. Do you still want to keep the old value? You can either update the data using a cronjob every night or calculate the new value whenever you show it to someone.
Thanks for the quick assist. I want the original value multiplied by the specified percentage and display the new value. For instance a user makes payment with PayPal on my site..I am able to retrieve the amount paid and stored in the database but I want that amount multiplied by a certain % every 24 hours automatically.
E.g:
User paid $50 with PayPal or credit card.
$50 is saved in the amount column in my database.
Amount is echoed in user dashboard
I want amount to multiply by 5% every 24hours on user dashboard.
You don't really need a command for this right? You simply need to do something like this on your dashboard
// Payment Model
class Payment extends Model
{
public function getCalculatedPriceAttribute()
{
$days = $this->created_at->diffInDays(\Carbon\Carbon::now());
// 1.05 stands for 5%, pow is used for the power operator
return $this->price * pow(1.05, $days);
}
}
// Dashboard Controller
public function index()
{
$payment = Payment::first();
return view('dashboard', compact('payment'));
}
// View
<p>Old price {{ $payment->price }}</p>
<p>New price {{ $payment->calculatedPrice }}</p>
Note: Above is assuming that you have a payment model with a payments table. The paid value is stored in the price column. Also, it assumes you have the timestamps stored in the database (created_at and updated_at)
@bobbybouwmann . Greetings sir! I know I shouldn't be asking non-laravel related questions here but I was wondering if this could be done in Codeigniter?. I'll gladly appreciate if it's possible and how to go around it. I mean multiplying a certain value say like "price" from the database by a certain percentage every 24 hours. I did this with laravel like you described and it worked fine..I'm just trying to improve on my programming skills. Thank you
@Babara Well, Laravel or CodeIgnitor, it doesn't really matter. In the end, it's all PHP. If you get it to work in Laravel, you should be able to do the same thing in CodeIgnitor ;)
@bobbybouwmann The issue here is that I've tried to make it work in Codeigniter but I can't seem to figure it out. I'll gladly appreciate if you'll just help me work around it. Thank you
Good morning Mr Bobby, I have a little challenge file upload to the database..I have been able to create a form with fields that displays product details.i want to add an image field that will accompany the other product details but don't know how to go around it
I already have a field in my database called "image"..my challenge is pulling the request in the product controller so that the image uploads along side with other fields.
My own case
I want to display total revenue by using the amount in the order table for different users the total amount of each users is displayed and the daily interest is also display by percentage from the amount in the order table, I want the daily amount to start increasing immediately there is value in the order table amount. Which I did as follow in my controller: $id=Auth::user()->id;
$order=order::where(‘user_id’,$id)->get();
$total_revenue=0;
Foreach($order as $order){
$total_revenue=$total_revenue + $order->sum(‘price’);
} // it sum the price of each user in order table and display in user page.
$daily_interest= $total_revenue /20; // it display 5%of the each user total price.
$today= $order->created_at->diffInDays(Carbon Carbon::now());
$total_interest=$daily_interest * $today; // it count the days and multiply it by daily interest and the value changes everyday in user page. But the issue am
Facing is that if user has’nt make order yet means the created_at is empty it display:
Exception
Property [created_at] does not exist on this collection instance.
But if users have already make an order and payment it display perfectly