Tables:
Posts
id | console_id | game_id | etc
Games
id | name
Console
id | name
Now when i am querying this relationship I am constantly getting the "Trying to get property of non-object" error. Now if i limit my results to say the top 3 (which is all i have in the games table) then it will run but anything more then that it will throw the exception... is the relationship wrong?
Relationships:
Game
public function Post()
{
return $this->belongsTo('App\Post', 'game_id');
}
Post
public function console()
{
return $this->hasOne('App\Console', 'id');
}
public function games()
{
return $this->hasOne('App\Game', 'id');
}
Console
public function Post()
{
return $this->belongsTo('App\Post', 'console_id');
}
Update
#joel #rashmi So actually dumping the $post I am seeing this on my 4th entry... it is returning NULL
["relations":protected]=>
array(2) {
["games"]=>
NULL
The first 3 return values. But then the 4th on are all returning NULL's. Again i have only 3 values in the Games table
Games Table:
1 | game 1
2 | game 2
3 | game 3
And actually on the third entry it it has a value of 2 but showing game 3 name
posts table:
id | game id
1 | 2
2 | 3
3 | 2 (but showing "game 1" text)
Looks like your posts each belong to a console and a game - not the other way around. And hasOne means there can only ever be one, but each console and game can have many posts. So it should be like this:
// Game
public function posts()
{
return $this->hasMany('App\Post');
}
// Post
public function console()
{
return $this->belongsTo('App\Console');
}
public function game()
{
return $this->belongsTo('App\Game');
}
// Console
public function posts()
{
return $this->hasMany('App\Post');
}
If your tables are named consoles, games, and posts, respectively, then you don't need to supply the ids, so I removed them. You can just re-add them if you need to.
It seems that your relationships are not proper. It should be like this:
// Game Model
public function posts()
{
return $this->hasMany('App\Post');
}
// Post Model
public function console()
{
return $this->belongsTo('App\Console');
}
public function game()
{
return $this->belongsTo('App\Game');
}
// Console Model
public function posts()
{
return $this->hasMany('App\Post');
}
And your Eloquent Query for post will be:
$this->model->select(SELECTED_FIELDS)
->with(array('game','console'))
->get();
where SELECTED_FIELDS means table fields you want to fetch.
Related
In my project I want show name in category and province table and this table i relation with it in user table.
This is my code.
In controller:
$user=user::with('province','category')->where([['id',$id]])->first();
In model:
//relation with province model (invert relation)
public function province()
{
return $this->belongsTo('App\Model\province');
}
//relation with province model (invert relation)
public function category()
{
return $this->belongsTo('App\Model\category');
}
But in return just id is returned not name and other property.
In output show this:
category_id: 3
province_id: 6
Wrong $user=user::with('province','category')->where([['id',$id]])->first();
//Use
$user=user::with(['province','category'])->where('id', $id)->first();
Put the foreign key and local key to relatioships.
public function province()
{
return $this->belongsTo('App\Model\province','foreign_key','local_key'); // put your FK and LK here
}
public function category()
{
return $this->belongsTo('App\Model\category','foreign_key','local_key'); // put your FK and LK here
}
You can get the result by
$user=user::where('id','=',$id)->first();
if(isset($user))
{
if(isset($user->province))
{
//do whatever you want
}
if(isset($user->category))
{
//do whatever you want
}
}
I've 2 tables
Users table
id
name
...
Chats table
id
from_id // fk - id on users, the user sent the message
to_id // fk - id on users, intended user
message_body
Now, I'm trying to set up One to Many relation where user has many chat messages but a chat message has two user. from and to user
How can I define this relationship?. I have to user eager loading.
I tried to use Compoships but it didn't work.
My code
User Model
public function chats() {
return $this->hasMany(Chat::class, ['from_id', 'to_id'], 'id');
}
Chat Model
public function user() {
return $this->belongsTo(User::class, ['from_id', 'to_id'], 'id');
}
public function chats() {
return $this->hasMany(Chat::class, 'from_id', 'id') + $this->hasMany(Chat::class,'to_id','id');
}
public function userFrom() {
return $this->belongsTo(User::class, 'from_id', 'id');
}
public function userTo() {
return $this->belongsTo(User::class, 'to_id', 'id');
}
Why don't you do something like that?
On your User model, set up two relationships like this:
public function chatsFrom() {
return $this->hasMany('App\Chat', 'from_id');
}
public function chatsTo() {
return $this->hasMany('App\Chat', 'to_id');
}
Then, on your Chat model, also set up two relationships, one to from and another to to, both referencing a User model. Like this:
public function fromUser() {
return $this->belongsTo('App\User', 'from_id');
}
public function toUser() {
return $this->belongsTo('App\User', 'to_id');
}
This way, you can access the relationships using something like this:
$user->chatsFrom();
$user->chatsTo();
$chat->fromUser();
$chat->toUser();
it is possible to get all details (user, posts, postimages) within one collection / json?
user->hasmany(posts)
post->hasmany(postimage)
user:
id | name
post:
id | user_id | text
postimage:
id | post_id | imgpath
user model:
public function posts() {
return $this->hasMany('App\posts')->orderBy('id', 'ASC');
}
posts model:
public function images(){
return $this->hasMany('App\postsimages');
}
get all posts from user works fine:
$myposts = users::find(Auth::user()->id)->posts;
i'm able to get all images from a post within a loop
foreach($myposts as $mypost) {
$postimages = $mypost->images;
}
what i want is to get all posts, images without the loop e.g
$myposts = users::find(Auth::user()->id)->posts->images
thanks
Yes, use hasManyThrough relationship.
posts model:
public function images()
{
return $this->hasMany('App\postsimages');
}
user model
public function posts()
{
return $this->hasMany('App\posts')->orderBy('id', 'ASC');
}
public function images()
{
return $this->hasManyThrough('App\posts', 'App\postsimages');
}
then
$postimages = Auth::user()->images;
I've a User model and a likes table. I applied polymorphic relation to it.
likes table has the following structure:
user_id --- likeable_id ---- likeable_type
eg: 1 ---- 5 ---- App\User //This indicates user 1 follows 5
eg: 3 ---- 1 ---- App\User //This indicates user 3 follows 1
Now I'm trying to get the followers and following list.
In the User model I added
public function likes()
{
return $this->morphMany(Like::class, 'likeable');
}
public function followers()
{
return $this->belongsTo('App\Like', 'likeable_id', 'user_id');
}
and from the controller, I've tried to do this:
$user1 = User::with('followers')->where('username', $username)->first();
But it's returning null for the followers relation. Am I doing it right?
Got the answer by myself: In User model -
public function followers()
{
return $this->morphMany('App\Like', 'likeable')->with('user');
}
public function following()
{
return $this->morphMany('App\Like', 'user', 'likeable_type')->with('user');
}
I have 3 tables, with:
User: id | name
Products: id | name | user_id
Tracks: user_id | product_id
Relationships between these models (tables) are:
//User.php*************************************************
public function products() {
return $this->hasMany('App\Product');
}
public function track() {
return $this->hasMany('App\Track');
}
//Product.php*************************************************
public function user() {
return $this->belongsTo('App\User');
}
public function track() {
return $this->belongsTo('App\Track');
}
//Track.php*************************************************
public function products() {
return $this->hasMany('App\Product');
}
public function user() {
return $this->belongsTo('App\User');
}
Track table is used if user want to track the product. Some kind of bookmark.
So, when I'am on product page, $product = App\Product::find(1), if I want to echo the user name, I do $product->user()->name, but if I want to know if the user already tracking the current product, when I do $product->user()->track()->get() I receive this error:
Call to undefined method Illuminate\Database\Query\Builder::track()
If I do $product->user()->with('track')->get(), I don't get an error, but I get User Object which it do not contain info if the user already tracking the product.
How can I know if user is already tracking the product?
First, some of your relationship should be like these
Product.php
public function track() {
return $this->hasMany('App\Track');
}
Track.php
public function product() {
return $this->belongsTo('App\Product');
}
now to check if a user is tracking a product
$product = App\Product::find($id);
$product->user->track()->where('product_id', $product->id)->first();
another way
$user = App\User::find($id);
$user->track()->where('product_id', $product->id)->first();
And to check if logged in user is tracking the product, you can get the logged in user by Auth::user() , so
$user = Auth::user();
$user->track()->where('product_id', $product->id)->first();