I get this error when I insert a new post in the db "Call to a member function load() on null"

maurice

When I try to add a new post I get the error

Call to a member function load() on null

and this is my PostController:

public function index($id = NULL)
    $posts = Post::all();

    $user = User::find($id)->load(['posts']);     
    return view('post.index', compact('posts','user'));
}

TABLES:

countries
    id - integer
    name - string

users
    id - integer
    country_id - integer
    name - string

posts
    id - integer
    user_id - integer
    title - string

How can I fix it?

Natali

If there is probability of $id is null or don't exists I can advice at least use findOrFail instead of find

$user = User::findOrFail($id)->load(['posts']);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

reduce database query to one and avoid Call to a member function load() on null error

Call to a member function load() on null

react I get an error when trying to add a new post with a form

Why I'm getting an error when I'm trying to insert a new object in db?

Why when I insert into db server give 500 error

I get error "400 Bad request" when trying to post a new issue into a GitHub repository

I get an error when I try to show the comments for each post

Wordpress - db_insert_error when using insert_post

Why I get error message when transfer INSERT method to Function

Why i get error when want insert to table in sql server?

When connecting to Oracle DB I get External: SIGSEGV error

I get 'django.db.utils.IntegrityError: null value in column "user_id" violates not-null constraint' when I try to publish a new post

When I publish a new post in Wordpress, I cannot get the tags of the post

How do I get the post json when there is a 404 error?

How to make trigger in MYSQL that acts as counter when I insert new comment on my post

When I insert a Linear Layout I get 'The application has stopped unexpectedly' error

when i want to insert data into mysql using JdbcOdbc Bridge driver, i get no database selected error,

I get this error when i run heroku run rake db:migrate in production

When I put PDO db connect details in an include file I get an error

Error with the ID generated when i insert new data in my database Java Derby

Client added new sandbox for Dynamics 365, and when trying to insert data I'm getting a 401 error

How to properly notifyItemMoved, insert and removed in RecyclerView old data list when i get new fresh list to assign to it

Why I get error when I submit login form using jQuery Post?

When I try to do a https post request in node, I get a getaddrinfo ENOTFOUND error

Why do I get a Error 400 when I want to post a JSON file HTTP REQUEST

I get "posts.map is not a function" ERROR when I delete a post

Why do I get this error in my logcat when I try to post JSON data?

when I'm trying to update my post in swagger I get the folowing error

When I upload image as base64 in post request I get error 403