Babara's avatar

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

0 likes
13 replies
Babara's avatar

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:

  1. User paid $50 with PayPal or credit card.
  2. $50 is saved in the amount column in my database.
  3. Amount is echoed in user dashboard
  4. I want amount to multiply by 5% every 24hours on user dashboard.
Snapey's avatar

Laravel has a scheduler which you can use to run a piece of code at regular intervals

This code gets the record, multiplies it, writes it back to the database.

Its all very straightforward if you take it piece by piece

Start by creating an artisan command that updates the value. You can then schedule it later.

https://laravel.com/docs/6.x/artisan#writing-commands

Babara's avatar

I will gladly appreciate it if you could practically walk me through. Thank you

bobbybouwmann's avatar
Level 88

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)

Babara's avatar

@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

bobbybouwmann's avatar

@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 ;)

Babara's avatar

@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

Babara's avatar

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

Babara's avatar

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.

Richieupdate's avatar

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

Please or to participate in this conversation.