0

I have written a model function which has to give me all the posts with user's id, but it returns a model object instead. My model:

public static function my_posts(){

    $userId = Auth::id();
    $getPosts = DB::table('posts')->where('user_id',$userId)->get();
    return $getPosts;
}

My Controller function:

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

How can I fix this error?

1
  • This is what I get when I do a dd($posts) in my view: posts_model {#239 ▼ #table: "posts" #primaryKey: "id" +incrementing: true #connection: null #keyType: "int" #with: [] #withCount: [] #perPage: 15 +exists: false +wasRecentlyCreated: false #attributes: [] #original: [] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [] #fillable: [] #guarded: array:1 [▶] } Commented Jun 19, 2019 at 13:05

1 Answer 1

1

Can you please change Model

public static function my_posts(){
    $userId = Auth::id();
    $getPosts = Post::where('user_id',$userId)->get();
    return $getPosts;
}

Then Change Your controller

public function myPosts(){
    $getPosts = new posts_model();
    $data = $getPosts->my_posts();
    return view("myposts", ["posts" => $data]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.