Search code examples
laravellaravel-facadelaravel-helper

How to use DB:: from helper class


I have a helper class name common.php in App\Helpers folder. I want to use DB:: facade here to use some db table. In helper method My code is -

<?php

use DB;
function ref_number()
{
    $last_row = DB::table('model_table')->latest('id')->first();
    if($last_row) {
        $ref = explode('-', $last_row->ref);

        if($ref[0] === date('y')) {
            // if running year
            $ref = $ref[1] + 1;
            $ref = date('y') .'-'. $ref;
        }
        else{
            $ref = date('y') .'-'. 1; // if new year
        }
    }
    else{
        $ref = date('y') .'-'. 1; // if first data
    }

    return $ref;
}

But this not working. Following error has been arrived.

Following error has been arrived.

So how can i solve this?


Solution

  • Change your using to: Illuminate\Support\Facades\DB;