lundi 4 décembre 2017

Accessing Model Through Custom Class

I'm trying to access a model through a class. I'm trying to get this done on a Laravel 5.5 installation. The folder structure is the default except for I have a folder in App/Libraries that holds my custom classes.

My issue is I get the below error when I'm trying to access a model through my class. Probably a namespace issue, but I just can't seem to find it. It's looking at the wrong place.

The error is

Class 'App\Libraries\App\AccountsModel' not found

Below is my code.

app/Libraries/Account.php

<?php
namespace App\Libraries;
use App\AccountsModel;
class Account {



public $accountNum;
    public $accountDescription;
    public $isMainAccount;
    public $parentAccount;
    public $createTime;
    public $updatedTime;

    public function getInfo($accountNum)
    {
        $account = App\AccountsModel::where('accountNumber', $accountNum)->take(1)->get(); //Error coming up due to this line
        //return $account;
        //return $this;
    }
}
?>

app\AccountsModel.php

<?php
namespace App\AccountsModel; 
use Illuminate\Database\Eloquent\Model;

class AccountsModel extends Model
{
    protected $table = 'accounts';

}

app/Http/Controllers/AccountsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Libraries\Account;

class AccountsController extends Controller
{
    public function create()
    {
        $account = new Account;
        var_dump($account->getInfo("1-1000"));
    }
}

I'm autoloading the model on composer.json as below. Any input on the below is also appreciated. I tried echoing the class name on a construct in the model, with no echo so I'm guessing I'm autoloading this incorrectly too.

"autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/",
            "AccountsModel\\" : "app/AccountsModel.php"
        }
    },

Ultimately I'm trying to access the model through my custom Account class when I get the error. Any help is greatly appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire