#Error: Class "app\Models\posts" not found

34 messages Β· Page 1 of 1 (latest)

brazen kraken
#

Hello! So for some reason laravel is not finding my model. I am really new to laravel and i do not really get why. Does someone want to help me?

my code:

The "model"

<?php

namespace app\Models\posts;

class posts {
    public static function all() {
        return [
            [
                'id' => 1,
                'title' => 'dunno',
                'desc' => 'also'
            ],
            [
                'id' => 2,
                'title' => 'Cheeseburger',
                'desc' => 'That food tastes nice :)'
            ]
            ];
}}

and the web.php

<?php

use Illuminate\Support\Facades\Route;
use app\Models\posts;

Route::get('/', function () {
    return view('index', [
        'posts' => posts::all()
    ]);
});

dapper scaffold
#

You don't need to have posts in your namespace you can use only
"namespace app\Models;"

brazen kraken
#

I have removed posts from the namespace so now the namespace is

namespace app\Models;

But it still gives the same error

dapper scaffold
#

Ok can you send your files tree?

brazen kraken
dapper scaffold
#

ok first you need to write posts with Uppercase like Posts

#

and at web.php you will need to rewrite too with Posts

#

class Posts {

#

use app\Models\Posts;

#

'posts' => Posts::all()

#

You can use User Model if you want to have a default structure for other Models...

#

or you can create Models with "php artisan make:model Posts"

brazen kraken
dapper scaffold
#

for example

dapper scaffold
# dapper scaffold

when you create with these imports you don't need to create methods to query

#

you can use only Posts::get() and you will take all results from database...

brazen kraken
#

Still says it cannot find app\Models\Posts

dapper scaffold
#

Ok can you send your files again?

#

with this new structure...

brazen kraken
#
<?php

use Illuminate\Support\Facades\Route;
use app\Models\Posts;

Route::get('/', function () {
    return view('index', [
        'posts' => Posts::all()
    ]);
});
#
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Posts extends Model
{
    public static function all() {
    return [
    [
        'title' => 'Hello World',
        'desc' => 'no idea'
    ]
    ];
    }
}
cinder iris
#

app vs App

dapper scaffold
#

yes please change app to App in your web.php

brazen kraken
#

okay

#

wow new error

#

Declaration of App\Models\Posts::all() must be compatible with Illuminate\Database\Eloquent\Model::all($columns = [...])

dapper scaffold
#

ok please remove extends Model because "all" function already exists in this class...

brazen kraken
#

πŸ™‚

#

works now

#

thx everyone!

dapper scaffold
#

πŸ˜€ πŸ‘πŸ»

#

You can search
laravel-8-from-scratch
if you want to know more about Laravel