Reputation: 33
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
Upvotes: 3
Views: 2723
Reputation: 4557
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;
Upvotes: 0
Reputation: 5443
there is 2 options one is you need to use namespace otherwise use that way
public function index()
{
return \App\MemberModel::test();
}
Upvotes: 1
Reputation:
You are keeping your models in Model folder so it should be
use App\Model\MemberModel;
Upvotes: 3