firstly thank you in advance.
I have the following Models , User , Location, Listing, Offer.
The relationships are:
User Model:
public function location()
{
return $this->hasOne(Location::class);
}
Location Model:
public function listings()
{
return $this->hasMany(Listing::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
Listing Model:
public function offers()
{
return $this->hasMany(Offer::class);
}
public function location()
{
return $this->belongsTo(Location::class);
}
Offer Model:
public function listing()
{
return $this->belongsTo(Listing::class);
}
In my Offer Resourse I would like to show only the offers that belongTo the listings that in turn belongsTo the location that in turn belongTo the authenticated user. I have tried the following.
public static function indexQuery(NovaRequest $request, $query)
{
$user = Auth::user();
if ($user->is_admin === 1){
return $query;
}elseif($user->is_admin === 0) {
return $user->location->listings->offers;
}
}
But get an error Property [offers] does not exist on this collection instance. Any assistance will be greatly appreciated.
I would try something like this.
More information here on querying relationship existence:
https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence
public static function indexQuery(NovaRequest $request, $query) {
$user = Auth::user();
if (!$user->is_admin) {
$query = $query->whereHas('listings.location.user', function ($builder) use ($user) {
return $builder->where('id', $user->id);
});
}
return $query;
}
The reason you are getting the "Property does not exist" error is due to you not pulling it from database.
Two alternative solutions include
Querying them separately: ...->listings()->offers()
Eager loading: ->with(['location.listings.offers']);
I try to create a relationship between user and post. 1 user have many posts and 1 post has 1 user. I already write a code relationship between model User.php and Post.php and I want to store user data in database. my question is how to save data in database? btw I use postgresql. when I click button there is no error and cannot redirect to admin.post.index.
PostController.php
public function store(Request $request)
{
$user = auth()->user();
$post = $user->posts()->create($request->only('title','body'));
return redirect()->route('admin.post.index');
}
Post.php
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function posts()
{
return $this->hasmany('App\Post');
}
Change User.php
public function posts()
{
return $this->hasMany('App\Post');
}
you spelled incorrect
You can try to Change User.php
public function posts()
{
return $this->hasMany('App\Post','user_id');
}
Post model
public function comments()
{
return $this->hasMany('App\Comment', 'post_id');
}
Comment model
public function post()
{
return $this->belongsTo('App\Post');
}
public function replies()
{
return $this->hasMany('App\Reply', 'comment_id');
}
Reply model
public function comments()
{
return $this->belongsTo('App\Comment');
}
My function
App\Post::with('comments.replies')->whereHas('comments.replies', function($query) {
$query->where('likes', '>', 0);
})->get();
I want to show ALL POSTS, and add comments if one or more replie has like
But if there is no any likes on reply, my post wont show.
I have this two queries to display information in 3 tables Users, Comments, and Posts. so I made this function:
public function show($id)
{
$posts = Post::with(['comments'])->findOrFail($id);
$user = User::find($posts->user_id);
echo "<h1>".$posts->title.'</h1>';
echo "<h2>".$user->name.'</h2>';
foreach ($posts->comments as $comment) {
echo $comment->body.'<br>';
}
}
on this function I using two variable $posts and $user, can I merge this two variable using eloquests command like Post::with(['user','comments'])'? so I can use just $posts variable and use it like $posts->users->name to access user name.
i was trying using this way:
$posts = Post::with(['comments','users'])->findOrFail($id);
but when i echo the post it showing that the user was null:
{"id":1,"user_id":1,"title":"Oleh id 1","body":"ini adalah content","created_at":"2017-10-18 03:25:54","updated_at":"2017-10-18 03:25:54","comments":[{"id":1,"post_id":1,"body":"Keren banget deh artikelnya, TOP","created_at":"2017-10-18 03:43:50","updated_at":"2017-10-18 03:43:50"},{"id":2,"post_id":1,"body":"Keren banget deh artikelnya, TOP","created_at":"2017-10-18 03:43:53","updated_at":"2017-10-18 03:43:53"},{"id":3,"post_id":1,"body":"Keren banget deh artikelnya, TOP","created_at":"2017-10-18 03:43:54","updated_at":"2017-10-18 03:43:54"}],"users":null}
Here's my model if you need it. My post model:
class Post extends Model
{
/**
* Get the comments for the blog post.
*/
public function comments()
{
return $this->hasMany('App\Comment');
}
public function users(){
return $this->belongsTo('App\User');
}
}
my Comment model
class Comment extends Model
{
/**
* Get the post that owns the comment.
*/
public function post()
{
return $this->belongsTo('App\Post');
}
}
You’ll have an easier time sticking to Laravel’s conventions for naming relationships.
A Post has many Comments, which belong to a User. Given this set up:
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
You can then query a post’s comments and users like this:
$comments = $post->comments()->with('user')->get();
You could also eager-load the user relation on comments if you always wanted the user returned with a comment:
class Comment extends Model
{
protected $with = ['user'];
public function user()
{
return $this->belongsTo(User::class);
}
}
Then your query would be simplified:
$comments = $post->comments;
Hope this helps you!!
What I am guessing is you want the list of all posts with the users. If relation is defined, try with the join
$posts = DB::table('posts')
->join('users', 'users.id', '=', 'posts.user_id')
->select('posts.*', 'users.name as user_name')
->where('posts.comments', '<>', NULL)
->get();
#NOTE: If you are using soft deletes you might wanna add where('posts.deleted_at', null)
After couple of days searching finally i got the answer, i can get merge the query like this:
$posts = Post::with(['comments','users'])->findOrFail($id);
but in the Post Model the function not "Users" but "User".
I have a User model, a Post model.
In my User model, I have the following:
public function followers()
{
return $this->belongsToMany(User::class, 'followers', 'follower_id', 'user_id')->withTimestamps();
}
public function following()
{
return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id')->withTimestamps();
}
public function posts()
{
return $this->hasMany(Post::class);
}
So basically, $user->following gives me a collection of users I am following and $user->posts gives me the collection of posts. I want to do this:
$posts = [];
$user->following->each(function($f) use($posts) {
$posts[] = $f->posts;
});
but more Eloquenty way, if that makes sense. Because I want to paginate the result after this. I was thinking of doing hasManyThrough, but cannot figure out how?
Here is link to similar question with an answer.
I think you wan tot do something like this:
User::where('id', $id)->with(['following.posts' => function ($q) use (&$posts) {
$posts= $q->get()->unique();
}])->first();