Laravel eloquent 关系多态多数据不起作用
Laravel eloquent relationship polymorph multidata notworking
作为标题,
我正在尝试使用多态关系获取数据的名称。
当我得到一个 object 它的工作完美。但是当结果是多个 object 时,它不是。
这是我的尝试。
>>> $page = App\PageAtribut::first()
=> App\PageAtribut {#2863
id: 1,
page_id: 1,
watchable_id: 1,
watchable_type: "App\Category",
created_at: "2018-09-11 11:03:20",
updated_at: "2018-09-11 11:03:20",
}
>>> $page->watchable
=> App\Category {#2857
id: 1,
name: "Series",
created_at: "2018-09-11 11:01:46",
updated_at: "2018-09-11 11:01:46",
}
关于上面的代码。因为我使用 'id' 来获取数据。
接下来,我尝试使用条件 page_id
等于 page id
.
获取所有数据
>>> $page = App\PageAtribut::where('page_id', 1)->get()
=> Illuminate\Database\Eloquent\Collection {#2859
all: [
App\PageAtribut {#2860
id: 1,
page_id: 1,
watchable_id: 1,
watchable_type: "App\Category",
created_at: "2018-09-11 11:03:20",
updated_at: "2018-09-11 11:03:20",
},
App\PageAtribut {#2861
id: 2,
page_id: 1,
watchable_id: 2,
watchable_type: "App\User",
created_at: "2018-09-11 11:03:40",
updated_at: "2018-09-11 11:03:40",
},
],
}
>>> $page->watchable
Exception with message 'Property [watchable] does not exist on this collection instance.'
这是结果。
Exception with message 'Property [watchable] does not exist on this collection instance.'
如果有多个object,如何获取名称?...
请使用
$page = App\PageAtribut::where('page_id', 1)->first();
dd($page->watchable);
因为 get()
会得到一个 array 集合。
在@Jigar 评论和@Adlan Answer 中提到。
因为得到 return 一个 collection
并且首先 return 一个 single
模型实例。
由于 array collections
它需要使用 forloop 来获取每个模型的属性。
所以代码应该是。
$page = App\PageAtribut::where('page_id', 1)->get()
然后
foreach($page->pageatribut as $p)
{
echo $p->watchable->name;
}
其return全名各集
CMIIW.
作为标题, 我正在尝试使用多态关系获取数据的名称。
当我得到一个 object 它的工作完美。但是当结果是多个 object 时,它不是。
这是我的尝试。
>>> $page = App\PageAtribut::first()
=> App\PageAtribut {#2863
id: 1,
page_id: 1,
watchable_id: 1,
watchable_type: "App\Category",
created_at: "2018-09-11 11:03:20",
updated_at: "2018-09-11 11:03:20",
}
>>> $page->watchable
=> App\Category {#2857
id: 1,
name: "Series",
created_at: "2018-09-11 11:01:46",
updated_at: "2018-09-11 11:01:46",
}
关于上面的代码。因为我使用 'id' 来获取数据。
接下来,我尝试使用条件 page_id
等于 page id
.
>>> $page = App\PageAtribut::where('page_id', 1)->get()
=> Illuminate\Database\Eloquent\Collection {#2859
all: [
App\PageAtribut {#2860
id: 1,
page_id: 1,
watchable_id: 1,
watchable_type: "App\Category",
created_at: "2018-09-11 11:03:20",
updated_at: "2018-09-11 11:03:20",
},
App\PageAtribut {#2861
id: 2,
page_id: 1,
watchable_id: 2,
watchable_type: "App\User",
created_at: "2018-09-11 11:03:40",
updated_at: "2018-09-11 11:03:40",
},
],
}
>>> $page->watchable
Exception with message 'Property [watchable] does not exist on this collection instance.'
这是结果。
Exception with message 'Property [watchable] does not exist on this collection instance.'
如果有多个object,如何获取名称?...
请使用
$page = App\PageAtribut::where('page_id', 1)->first();
dd($page->watchable);
因为 get()
会得到一个 array 集合。
在@Jigar 评论和@Adlan Answer 中提到。
因为得到 return 一个 collection
并且首先 return 一个 single
模型实例。
由于 array collections
它需要使用 forloop 来获取每个模型的属性。
所以代码应该是。
$page = App\PageAtribut::where('page_id', 1)->get()
然后
foreach($page->pageatribut as $p)
{
echo $p->watchable->name;
}
其return全名各集
CMIIW.