简体   繁体   English

检索一对多关系laravel上的错误

[英]Retrieving error on one to many relationship laravel

Been learning laravel for 4 days and im trying to fix this error for 2 hours and i cant still fix it. 一直学习laravel 4天,即时消息试图修复此错误2个小时,我仍然无法修复它。 I can save on one to many relationship but i cant retrieve data i think there something wrong with the relationship. 我可以保存一对多的关系,但我无法检索数据,我认为该关系存在问题。 Im trying to retrieve posts on user using this line but im getting not empty results on users but empty result on posts. 我试图使用此行来检索用户的帖子,但我在用户上获得的结果不是空的,而是在帖子中的结果是空的。 Same thing happening on categories and posts which is many to many relationship but i cant save on many to many. 在类别和帖子上发生相同的事情,这是多对多关系,但我无法节省多对多关系。

$users = User::with('posts')->get();

ANd im getting an error when i use this the error is 我在使用此错误时收到错误消息是

Undefined property: Illuminate\\Database\\Eloquent\\Collection::posts() 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: posts()

 $users = User::where('user_id','=','2')->get();

 $posts = $users->posts()->get();

Heres my user Model 这是我的用户模型

use Illuminate\Database\Eloquent\Model;

class User extends Model
{

       protected $primarykey = 'user_id';
        protected $table = 'users';

    public function posts(){
        return $this->hasMany("App\Post");
    }
}

Heres my posts Model 这是我的帖子模型

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

    protected $primarykey = 'id';
        protected $table = 'posts';

    public function post_validation_rules(){
        return [
                'post_title' => 'required|min:5|unique:posts',
                'post_body' => 'required'
            ];
    }

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

     public function categories(){
        return $this->belongsToMany('App\Category', 'category_id', 'category_id');
    }
}

Categories Post 分类发布

class Category extends Model
{

    protected $primarykey = 'category_id';
        protected $table = 'categories';

    public function posts(){
        return $this->belongsToMany('App\Post', 'post_id', 'id');
    }
}

Database

Posts Table
id
user_id
post_title
post_body
createad_date
updated_date

Users Table
user_id
username
email
pass
createad_date
updated_date

You can only call relations on a single object, not on an entire collection. 您只能在单个对象上调用关系,而不能在整个集合上调用关系。 $users is a collection of User objects. $usersUser对象的集合。

If you want a single user object, use the first() function to get the first User object that matches. 如果要使用单个用户对象,请使用first()函数获取匹配的第一个User对象。

$user = User::where('user_id','=','2')->first();
$posts = $user->posts;

Update: To get the posts directly in the user object, you need to use the with function: 更新:要直接在用户对象中获取帖子,您需要使用with函数:

$user = User::with('posts')->where('user_id','=','2')->first();

Try to declare the field that have the relation between your tables then, for example: 然后尝试声明具有表之间关系的字段,例如:

$this->hasMany(App\\Post::class, 'user_id', 'user_id');

Laravel is searching for a field id in User table but it does not exist. Laravel在用户表中搜索字段id ,但该字段id不存在。 so with this way you will tell it that the field you look is user_id 这样,您将告诉它您所查找的字段是user_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM