I have created a relationship between User
model and StoryModel
. But it give me the Error:-
Call to undefined relationship [userStories] on model [App\User].
May be I'm missing something. Following is my code which I'm using
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notification;
use App\CheckIn;
use App\Travel;
use Carbon\Carbon;
use App\NewInterest;
use App\UserStory;
class User extends Authenticatable
{
use Notifiable;
protected $table = "users";
protected $primaryKey = 'id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstname','lastname', 'user_id','email',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function userStories(){
return $this->hasMany(UserStory::class, 'user_id', 'user_id');
}
}
Controller Logic
$usersStories = User::with('userStories')
->select('id','user_id','stories')
->get();
print_r($usersStories);
exit;