1
votes

I am following a Laravel course on Udemy and while I followed everything the instructor did, for some weird reason I am not getting the expected result.

This is the Relationship One to One lesson and I added a function in User Model to check if it has any posts.

Then added the route to display post if user_id equals.

app\User.php

public function post() {

    return $this->hasOne('App\Post');

}

app\Http\routes.php

    Route::get('/user/{id}/post', function($id) {

    return User::find($id)->post;

});

Below is the screenshot from the database showing that I have a post with user_id = 1 in the posts table. I also have a user with id=1 in the user's table. MySQL data Why do I get a blank page when visiting domain/user/1/post?

Sohel, i got a result from your function, but had to use

var_dump(User::with('post')->where('id',1)->first());

Then tried something else:

return User::with('post')->where('id',$id)->first();

And this is the result:

{"id":1,"name":"Nick","email":"[email protected]","created_at":"2018-03-15 09:49:51","updated_at":"2018-03-15 09:49:51","post":null}
4
Did you check laravel.log file for any errors? - N.B.
No error in log. Also, it would display an error if i had one, however, all i get is a blank page and no error in log file. - Kriogen Mihalcea
Try return $this->hasOne('App\Post', 'post_id', 'id');. Also, enable error reporting if you want to see errors. - Ron van der Heijden
please show what dd(User::with('post')->where('id',1)->first()) gives - Sohel0415
Sorry, just figured it out and added as answer exactly what you said and then saw your comment. Thank you, yes, that was the issue i was having. - Kriogen Mihalcea

4 Answers

0
votes

Your one to one relationship should go as: app\User.php

public function post() {
    return $this->hasOne('App\Post');
}

app\Post.php

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

you can try doing this function in controller:

public function getPost {
    $user= User::find($id);
    return $user->post();
}
0
votes

The issue was not with the functions, the issue was in the database. Column deleted_at was not NULL and it was marking the post as being soft deleted, therefore not being displayed.

0
votes

Since the "user_id" field is in the "posts" table, the relation in the App\User model need to be:

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

then, call it without the parenthesis to get the result:

public function getPost {
    $user= User::find($id);
    return $user->post;
}

When you use the parenthesis, you get the builder and not the result. example:

public function getPost {
    $user= User::find($id);
    return $user->post()->get();
}

OR

public function getPost {
    $user= User::find($id);
    return $user->post()->where('name', 'like', '%hello%')->get();
}
-1
votes

I think you need ->hasMany() relation if you want to check if user has any posts because the user can has many posts... and the code would be:

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

The call:

User::find($id)->posts;