3

I have a question with model in laravel 5

I want to access my model and get return value. but that not work :(

app/Http/Controllers/MemberController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Model;
class MemberController extends Controller
{
    public function index()
    {
        return MemberModel::test();
    }
} 

app/Model/MemberModel.php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class MemberModel extends Model
{
    public function test(){
        return "123";
    }
}

routes.php

Route::get('test', 'MemberController@index');

and error message is ..

FatalErrorException in MemberController.php :
Class 'App\Http\Controllers\MemberModel' not found
Maras
  • 33
  • 1
  • 3
  • I know nothing about PHP but I'm guessing MemberController.php needs a line like `use App\Model\MemberModel;` instead of `use App\Model;` like you have. – Olson.dev Aug 19 '15 at 11:23

3 Answers3

3

You are keeping your models in Model folder so it should be

use App\Model\MemberModel;
  • nice ! that can work now , and i want to ask i see the some code that write like this: \MemberModel::test(); i want to know "\" have any means ? – Maras Aug 19 '15 at 11:21
  • @Maras.read this http://stackoverflow.com/questions/4790020/what-does-a-backslash-do-in-php-5-3 –  Aug 19 '15 at 11:34
1

there is 2 options one is you need to use namespace otherwise use that way

public function index()
{
    return \App\MemberModel::test();
}
Imtiaz Pabel
  • 5,307
  • 1
  • 19
  • 24
0

you can also add the location to psr-4 in your composer.json

...
"autoload": {
    "classmap": [
        "database",
        "app/models"
    ],
    "psr-4": {
        "App\\": "app/",
    }
},

run composer dump-autoload you can inject the mode by name

use Model;
mdamia
  • 4,447
  • 1
  • 24
  • 23