Мое приложение позволяет пользователям ставить лайки и оставлять комментарии с отношениями morphMany
Любимая модель:
public function favorited()
{
return $this->morphTo();
}
public function user(){
return $this->belongsTo(User::class, 'user_id');
}
Моя черта
trait Favoriteable
{
public function favorites()
{
return $this->morphMany(Favorite::class, 'favorited');
}
}
Мое сообщение
public function favorites()
{
return $this->morphMany(Favorite::class, 'favorited');
}
Мой комментарий
public function favorites()
{
return $this->morphMany(Favorite::class, 'favorited');
}
Мои пользователи
public function favorites(){
return $this->hasMany(Favorite::class);
}
Теперь в блейде я пытаюсь получить любимые действия пользователя .. Я пробовал $ activity->subject-> favour, и больше ничего не работает, но когда я dd ($ activity-> subject-> favourited), я получаю это:
Post {#682 ▼
#guarded: []
#fillable: array:4 [▶]
#appends: array:2 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▼
"id" => 14
"user_id" => 1
"author_id" => 3
"content" => "Awesome Albert.."
"total_comments" => 0
"created_at" => "2018-05-16 13:02:18"
"updated_at" => "2018-05-16 13:02:18"
]
#original: array:7 [▼
"id" => 14
"user_id" => 1
"author_id" => 3
"content" => "Awesome Albert.."
"total_comments" => 0
"created_at" => "2018-05-16 13:02:18"
"updated_at" => "2018-05-16 13:02:18"
]
#casts: []
#dates: []
#dateFormat: null
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
}
, когда я dd ($ activity->результат):
Favorite {#685 ▼
#guarded: []
#with: array:1 [▼
0 => "favorited"
]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▼
"id" => 30
"user_id" => 3
"favorited_id" => 14
"favorited_type" => "App\Post"
"created_at" => "2018-05-18 16:01:56"
"updated_at" => "2018-05-18 16:01:56"
]
#original: array:6 [▼
"id" => 30
"user_id" => 3
"favorited_id" => 14
"favorited_type" => "App\Post"
"created_at" => "2018-05-18 16:01:56"
"updated_at" => "2018-05-18 16:01:56"
]
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: array:1 [▼
"favorited" => Post {#720 ▼
#guarded: []
#fillable: array:4 [▶]
#appends: array:2 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▼
"id" => 14
"user_id" => 1
"author_id" => 3
"content" => "Awesome Albert.."
"total_comments" => 0
"created_at" => "2018-05-16 13:02:18"
"updated_at" => "2018-05-16 13:02:18"
]
#original: array:7 [▼
"id" => 14
"user_id" => 1
"author_id" => 3
"content" => "Awesome Albert.."
"total_comments" => 0
"created_at" => "2018-05-16 13:02:18"
"updated_at" => "2018-05-16 13:02:18"
]
#casts: []
#dates: []
#dateFormat: null
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}