https://github.com/laravel/framework/issues/2619
https://laracasts.com/discuss/channels/eloquent/append-pivot-data-to-model?page=0
Eloquent $cast, $guarded
pivot
attach/detach
Eloquent accessor trong relationship.
ví dụ find()/ first()->username
Ok I think I see now what is going on. Your Eloquent model is called subcategory, but so is the foreign key. So when you call
$style->subcategory
That is returning the foreign key instead of the model. To fix this, I'd recommend changing the name of the foreign key id to subcategory_id. If you can't change the database, you could force it to use the model by chaining the method with something like this
$style->subcategory()->first()->name
Edit: Another idea, you could change the name of the relationship to something like
public function subcategory_item()
{
return $this->belongsTo('Paragon\Products\Subcategory', 'subcategory');
}
Then you ought to be able to properly reference it with
$style->subcategory_item->name
$append cột ảo:
http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/
Nhắc lại check empty():
if (empty($result)) { }
if (!$result) { }
if ($result) { }
Because if you dd($result); you'll notice an instance of Illuminate\Support\Collection is always returned, even when there are no results. Essentially what you're checking is $a = new stdClass; if ($a) { ... } which will always return true.
To determine if there are any results you can do any of the following:
if ($result->first()) { }
if (!$result->isEmpty()) { }
if ($result->count()) { }
if (count($result)) { }
https://stackoverflow.com/questions/20563166/eloquent-collection-counting-and-detect-empty
Scope:
https://stackoverflow.com/questions/41319146/laravel-orm-database-query-inside-model-function
https://hashnode.com/post/a-little-trick-with-eloquent-query-scopes-that-makes-them-super-reusable-ciylr4k0r001os453wuvd4t8w
https://laracasts.com/discuss/channels/eloquent/append-pivot-data-to-model?page=0
Eloquent $cast, $guarded
pivot
attach/detach
Eloquent accessor trong relationship.
ví dụ find()/ first()->username
Ok I think I see now what is going on. Your Eloquent model is called subcategory, but so is the foreign key. So when you call
$style->subcategory
That is returning the foreign key instead of the model. To fix this, I'd recommend changing the name of the foreign key id to subcategory_id. If you can't change the database, you could force it to use the model by chaining the method with something like this
$style->subcategory()->first()->name
Edit: Another idea, you could change the name of the relationship to something like
public function subcategory_item()
{
return $this->belongsTo('Paragon\Products\Subcategory', 'subcategory');
}
Then you ought to be able to properly reference it with
$style->subcategory_item->name
$append cột ảo:
http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/
Nhắc lại check empty():
if (empty($result)) { }
if (!$result) { }
if ($result) { }
Because if you dd($result); you'll notice an instance of Illuminate\Support\Collection is always returned, even when there are no results. Essentially what you're checking is $a = new stdClass; if ($a) { ... } which will always return true.
To determine if there are any results you can do any of the following:
if ($result->first()) { }
if (!$result->isEmpty()) { }
if ($result->count()) { }
if (count($result)) { }
https://stackoverflow.com/questions/20563166/eloquent-collection-counting-and-detect-empty
Scope:
https://stackoverflow.com/questions/41319146/laravel-orm-database-query-inside-model-function
https://hashnode.com/post/a-little-trick-with-eloquent-query-scopes-that-makes-them-super-reusable-ciylr4k0r001os453wuvd4t8w
Comments
Post a Comment