Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call models in Laravel 5?

So, in L5 I created folder like app/Models/Blog where is file Posts.php which looks like:

<?php namespace App\Models\Blog;

use Illuminate\Database\Eloquent\Model;

class Posts extends Model {
    protected $table = 'posts';
}

After it I executed composer dump and then in my controller:

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Models\Blog\Posts as Posts;

class BlogController extends Controller {

    public function index()
    {
        $post = Posts::all()->toArray();

        dd($post);
    }
}

It throws me an error:

FatalErrorException in BlogController.php line 14: Class 'Models\Blog\Posts' not found
like image 984
user1692333 Avatar asked Feb 19 '15 13:02

user1692333


2 Answers

Try changing this:

use Models\Blog\Posts as Posts;

To this:

use App\Models\Blog\Posts;
like image 156
Kussie Avatar answered Oct 13 '22 12:10

Kussie


In Laravel 5.2 it's just:

use App\Blog;

or

use App\Blog\Posts;
like image 20
Peon Avatar answered Oct 13 '22 12:10

Peon