0

This Query Of get bookmarks is working with condition without relation with user_topics

$bookmarkedposts = BookmarkedPost::where('leader_id',$user_id)->get(); 

But When I add add relation to other model it didn't work

$bookmarkedposts = BookmarkedPost::where('leader_id',$user_id)->with('user_topics.first_media')->get(); 

Here is Bookmarked.php

namespace App\Models; use Illuminate\Database\Eloquent\Model; class BookmarkedPost extends Model { public function user_topics() { return $this->hasOne('App\UserTopic','id')->where('isdelete',0)->where('isactive',1); } } 

bookmarked_posts_table_sturucture user_topics_table_stucture

3
  • Can you show us the model BookmarkedPost.php Commented Nov 14, 2019 at 16:48
  • Yes, I have Added In Description. @YJRB , Please Help me. Commented Nov 14, 2019 at 16:48
  • "it didn't work" - What do you mean by that? Using ->with() doesn't exactly do anything on its own... What is your expected result, and what is actually happening? Commented Nov 14, 2019 at 17:13

2 Answers 2

1

BookmarkedPost Model:

class BookmarkedPost extends Model { public function user_topic() { return $this->belongsTo('App\UserTopic','user_topic_id', 'id'); } } 

UserTopic Model:

class UserTopic extends Model { public function bookmarder_posts() { return $this->hasMany('App\BookmarkedPost','user_topic_id', 'id'); } } 

Inside the function now you can use:

$bookmarkedposts = BookmarkedPost::where('leader_id',$user_id)->with('user_topic')->get(); 
Sign up to request clarification or add additional context in comments.

17 Comments

Hello @AlbertoSinigaglia ,thank you so much for answer, I changed my Bookmarked.php exact as you wrote,but issue is still the same. :(
what relations have BookmarkedPost with UserTopics?
belongsTo here is method code return $this->belongsTo('App\Models\BookmarkedPost');
i mean, the relation in the database
In Database Bookmarked_posts table has column user_topic_id reference key of user_topic table.
|
0

Try this

 BookmarkedPost::where('leade_id', $user_id)->with('user_topics'); 

5 Comments

Thank You so much for help but,I tried this This is also Not Working @YJRB
@Y JRB Getting no error but condition is not working.
Try to change your user_topics() relation like this : return $this->hasOne('App\UserTopic', 'user_topic_id')->where('isdelete',0)->where('isactive',1);
Already Tried That also But Still Condition is not working. :(
Does your code exists on Github and is open source?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.