carrany
carrany

Reputation: 156

Displaying Calculations from Controller In View blade Laravel

i am building an asset management system.I would like to have the following calculations done in the controller to be displayed in the view.

  public function depreciation()
    {

    $assets = Asset::all();

        $price = DB::table('assets')
            ->where('category_id', 1)
            ->sum('purchase_price');
        $dep = $price * 0.3333;
        $netprice = $price - $dep;

        return $netprice;

        return view('admin.assets.index')->with(['price','dep', 'netprice' => $netprice]);


}

My Route

    Route::post('assets_depreciation', ['uses' => 'Admin\AssetsController@depreciation', 'as' => 'assets.depreciation']);

My View

 <tbody>
                @if (count($assets) > 0)
                    @foreach ($assets as $asset)
                        <tr data-entry-id="{{ $asset->id }}">
                            @can('asset_delete')
                                <td></td>
                            @endcan

                            <td field-key='title'>{{ $asset->title }}</td>
                            <td field-key='serial_number'>{{ $asset->serial_number }}</td>
                            <td field-key='barcode'>{{ $asset->barcode }}</td>
                            <td field-key='photo1'>@if($asset->photo1)<a href="{{ asset(env('UPLOAD_PATH').'/' . $asset->photo1) }}" target="_blank"><img src="{{ asset(env('UPLOAD_PATH').'/thumb/' . $asset->photo1) }}"/></a>@endif</td>
                            <td field-key='category'>{{ $asset->category->title ?? '' }}</td>
                            <td field-key='status'>{{ $asset->status->title ?? '' }}</td>
                            <td field-key='location'>{{ $asset->location->title ?? '' }}</td>
                            <td field-key='assigned_user'>{{ $asset->assigned_user->name ?? '' }}</td>
                            <td field-key='vendor'>{{ $asset->vendor->name ?? '' }}</td>
                            <td field-key='purchase_price'>{{ $asset->purchase_price }}</td>
                            <td field-key='warranty'>{{ $asset->warranty }}</td>

                            <td field-key='depreciation'>{{ $netprice }}</td>

                            <td>

How can This be achieved?

Upvotes: 0

Views: 727

Answers (3)

Alex M.
Alex M.

Reputation: 26

You should be using a GET request instead of a POST for your route. It should look like this:

Route::get('assets_depreciation', ['uses' => 'Admin\AssetsController@depreciation', 
'as' => 'assets.depreciation']);

Laravel's documentation gives you correct usage examples of their framework components, you can check it out here: Laravel/Routing. Hope this helps!

Upvotes: 1

Kamil Kiełczewski
Kamil Kiełczewski

Reputation: 92447

You need to create file index.blade.php in your_project/resources/views/admin/assets/ with html-blade content e.g:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>my page</title>

    </head>
    <body>
        <div>Price: {{ $price }}</div>
        <div>Dep: {{ $dep }}</div>
        <div>Net price: {{ $netprice }}</div>
    </body>
</html>

more info here. And remove first return statement and change last return to

return view('admin.assets.index', compact('price','dep','netprice'));

Upvotes: 0

Kamal Paliwal
Kamal Paliwal

Reputation: 1301

If you are looking to send the price, dep and netprice to the admin.assets.index view then you can use this:

public function depreciation()
{
    $assets = Asset::all();

    $price = DB::table('assets')
            ->where('category_id', 1)
            ->sum('purchase_price');
    $dep = $price * 0.3333;
    $netprice = $price - $dep;

    return view('admin.assets.index')->with(['price' => $price,'dep' => $dep, 'netprice' => $netprice]);    
}

Then you can use these variables in you view file {{ $price }}, {{ $dep }}, {{ $netprice }}

Upvotes: 1

Related Questions