How to name models in Laravel 5?

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

 <?php namespace App\Models\Blog; use Illuminate\Database\Eloquent\Model; class Posts extends Model { protected $table = 'posts'; } 

After that I executed composer dump , and then in the 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 gives me an error message:

 FatalErrorException in BlogController.php line 14: Class 'Models\Blog\Posts' not found 
+6
source share
4 answers

Try changing the following:

 use Models\Blog\Posts as Posts; 

To that:

 use App\Models\Blog\Posts; 
+13
source

In Laravel 5.2 it is simple:

 use App\Blog; 

or

 use App\Blog\Posts; 
+1
source

Change the following

 class Posts extends Model { 

to

 class Posts extends \Eloquent { 
0
source

You need to check two points:

  • namespace should be first
  • use should be use App\Models\Blog in your case

Like this:

 <?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\Models\Blog; class BlogController extends Controller { public function index() { $post = Posts::all()->toArray(); dd($post); } } 

(verified with Laravel 5.4)

0
source

Source: https://habr.com/ru/post/982738/


All Articles