skip to Main Content

I have made an API and a trait with a function to handle responses.

namespace AppTraits;

use AppModelsUser;
use AppModelsDiseases;
use AppModelsExaminations;
use AppModelsRec_Doc;
use AppHttpResourcesfailter;

trait ApiTraits
{
    public function apiGet($id = null, $table)
    {
        if ($table::find($id)) {
            $status = '200';
            $message = 'ok';
            $data = new failter($table::find($id));
        } elseif ($id === null) {
            $data = failter::collection($table::get());
            $status = '303';
            $message = 'getting all data';
        } else {
            $data = null;
            $status = '404';
            $message = 'not found';
        }
        
        $array = [
            'data' => $data,
            'status' => $status,
            'message' => $message
        ];
        
        return $array;
    }
}

After that, in the controller, I use this function:

public function users()
{
    return $this->apiGet('','User');
}

The expected scenario is getting =>

$data = failter::collection($table::get());
$status = '303';
$message = 'getting all data';

Getting error =>

Class "User" not found

But I already added a User model.

3

Answers


  1. The class isn’t called User it’s actually called AppModelsUser. When you put use AppModelsUser at the top of your file, it installs a shorthand for that file where every time you refer to the bareword User what you actually mean is AppModelsUser. Strings (e.g. $cls='Foo';new $cls;) do not get the same treatment.

    Try using the ::class notation in the controller:

    // This line is required at the top of the file as well.
    use AppModelsUser;
    
    public function users()
    {
        return $this->apiGet('',User::class);
        // Meaning the same thing as:
        return $this->apiGet('','AppModelsUser');
        // And the same thing as:
        return $this->apiGet('',AppModelsUser::class);
    }
    

    ::class makes the bareword into a string with the fully qualified class name.

    Using ::class instead of the stringified class name is a good habit to get into because it makes refactoring easier, and the code is easier to read too, since User::class is clear that it’s a class name, whereas 'User' (string) could be anything.

    Login or Signup to reply.
  2. you must use the full path to the class AppModelsUser

    public function users()
    {
        return $this->apiGet('',User::class);
    }
    
    
    public function apiGet($id = null, $className)
    {
        if (!class_exists($className)) {
            throw new LogicException("Unable to load class: $className");
        }
    
        $query = $className::query();
    
        if ($query->find($id)) { ...
    
    
    Login or Signup to reply.
  3. Instead of just defining the model name (User), you can try defining it with full namespaces; AppModelsUser or User::class.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search