Search code examples
phplaraveleloquenttraitslaravel-query-builder

Laravel trait function not found


I have look all over stackoverflow and google and I cannot seem to solve my trait function not found. I have tried composer dump-autoload, my composer.json have the app directory connected and even checked my namespace and trait names. Here is my user controller.

<?php

namespace App\Http\Controllers;

use App\User;
use App\Traits\ControllerTrait;
use App\Http\Requests\UpdateUser;
use Illuminate\Http\Request;
use App\Http\Requests\IndexUser;

class UserController extends Controller
{
    use ControllerTrait;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show multiple users.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(IndexUser $request)
    {
        $per_page = 50;
        $order_by = 'id';
        $sort_by = 'ASC';

        if($request->has('per_page')) {
            $per_page = $request->input('per_page');
        }

        if($request->has('order_by')) {
            $order_by = $request->input('order_by');
        }

        if($request->has('sort_by')) {
            $sort_by = $request->input('sort_by');
        }

        $users = User::when($request->has('select'), function ($query) use ($request) {
            selectPrepare($query, $request->input('select'));
        })->when($request->has('include'), function ($query) use ($request) {
            if(!empty($request->input('include'))) {
                $includedTables = explode(',', $request->input('include'));
                $tables = array_map('trim', $includedTables);

                return $query->with($tables);
            }

            return $query;
        })->orderBy("{$order_by}", "{$sort_by}")
        ->paginate($per_page);

        return response()->json($users);
        }
    }
}

Here is my Trait

    <?php

    namespace App\Traits;

    trait ControllerTrait
    {
        /**
         *  Function: scopeSelectPrepare

    public function selectPrepare($query, $select) {
        if(!empty($select)) {
            $selectedColumns = explode(',', $select);
            $columns = array_map('trim', $selectedColumns);

            return $query->select($columns);
        }

        return $query;
    }
}

As you can see my name space for the Trait is App\Traits and call the use App\Traits\ControllerTrait in my controller then can the use ControllerTrait to get the functions from the trait. When I try to get the function to use in my query it says: Call to undefined function App\Http\Controllers\selectPrepare()

Is there something I am missing? I am new to the traits functionality of laravel but I thought I was following all of the examples and naming conventions. Can anyone see what I am doing wrong.


Solution

  • You need to use $this when accessing trait methods, just like you would for any other method:

    $users = User::when($request->has('select'), function ($query) use ($request) {
        $this->selectPrepare($query, $request->input('select'));