3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Class 'App\Post' not found Laravelで作ったモデルクラスがないよと言われる

Posted at

しょうもないミスで時間を使ってしまったので投稿。
ディレクトリModelsにモデルを作ったら当然ながらhasManyの引数にするときや、useするときにそのパスを書かないとクラスがないと言われる。

当たり前なんだけど、ドキュメントに載ってなくてそのままコピペでだめなケースもあるので手でコード書いていくって大切だと思う。

IDEを使うとuseのところとかうまく補完してくれそう。

Class 'App\Post' not found
app/Models/Post.php

class User extends Model
{
    /**
     * ユーザーの全ポストの取得
     */
    public function posts()
    {
        return $this->hasMany('App\Models\Post');
    }
}
app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

// 当然ながらここも
use App\Models\Post;

class UserController extends Controller
{

Laravel 5.7 Eloquent:リレーション
https://readouble.com/laravel/5.7/ja/eloquent-relationships.html#one-to-one

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?