Laravel 查询返回一个对象而不是数组

Laravel 查询返回一个对象而不是数组,laravel,Laravel,我已经编写了一个模型函数,它必须给我所有带有用户id的帖子,但它会返回一个模型对象。 我的模型: public static function my_posts(){ $userId = Auth::id(); $getPosts = DB::table('posts')->where('user_id',$userId)->get(); return $getPosts; } 我的控制器功能: public function myPosts(){

我已经编写了一个模型函数,它必须给我所有带有用户id的帖子,但它会返回一个模型对象。 我的模型:

public static function my_posts(){

    $userId = Auth::id();
    $getPosts = DB::table('posts')->where('user_id',$userId)->get();
    return $getPosts;
}
我的控制器功能:

public function myPosts(){
        $getPosts = new posts_model();
        $getPosts->my_posts();
        return view("myposts",["posts" => $getPosts]);
    }

如何修复此错误?

您可以更改型号吗

public static function my_posts(){
    $userId = Auth::id();
    $getPosts = Post::where('user_id',$userId)->get();
    return $getPosts;
}
然后更换控制器

public function myPosts(){
    $getPosts = new posts_model();
    $data = $getPosts->my_posts();
    return view("myposts", ["posts" => $data]);
}

这就是我在视图中执行dd($posts)时得到的结果:posts_model{#239▼ #表:“posts”#primaryKey:“id”+递增:true#连接:null#keyType:“int”#with:[]#with count:[]#perPage:15+exists:false+wasnrecentlycreated:false#属性:[]#原始:[]#更改:[]#强制类型:[];日期:[]#日期格式:null#附录:[];DispatcheEvents:[]可观测值:[]#关系:[]#接触:[]+时间戳:真#隐藏:[]#可见:[]#可填充:[]#受保护:数组:1[▶] }