0

i just created users * stories tables, and model , define relationship as shown below

In User Model

public function stories()
    {
        return $this->hasMany(\App\Story::class);
}

In Story Model

public function user()
    {
        return $this->belongsTo(App\User::class);
    }

In tinker

$user1=App\Models\User::find(1)
=> App\Models\User {#3356
     id: 1,
     name: "user1",
     email: "user1@localhost.com",
     email_verified_at: null,
     created_at: "2020-12-25 19:48:58",
     updated_at: "2020-12-25 19:48:58",

But when i try to get all stories belong to user 1

$user1->stories

PHP Error: Class "App\Story" not found in C:\xampp\laravel\storify\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasRelationships.php on line 745

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Ahmed
  • 7
  • 3

2 Answers2

0

It looks you are using wrong namespaces. Model files are located probably in \App\Models namespace (app\Models directory), so relationships should look like this:

In User Model

public function stories()
{
    return $this->hasMany(\App\Models\Story::class);
}

In Story Model

public function user()
{
   return $this->belongsTo(\App\Models\User::class);
}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0

make sure that you are accessing right to Story.php model

use App\Models\Story;