您好我正在使用Laravel,我从数据库表中获取登录的用户收藏夹。虽然我正在查看我从CMS API获取的文章,但我也在提取已登录的用户收藏夹,我想检查用户是否已经喜欢这篇文章但是获取收藏夹并检查UID是否存在在数组内,若是,我将输出不同的样式图标。所以我认为我会将数据作为数组传递,然后使用php函数in_array
但是我收到此错误:
Argument 1 passed to Illuminate\Database\Grammar::columnize()
must be of the type array, string given
这就是我检索数据的方式:
//fetch article from cms
$item = $this->client->fetchPage($id);
$favourites = Auth::user()->favourites()->get("article_uid")->toArray();
然后在我看来
@if (in_array($item->uid, $favourites))
Already a favourite
@else
{{Form::open(['action'=>['FavouritesController@store',$item->UID]])}}
<button type="submit" class="btn btn-danger"><span class="icon-remove-user"></span>Select Favourite</button>
{{Form::close()}}
@endif
我的数组输出如下:
array(3) {
[0] = > array(6) {
["id"] = > string(1)"1" ["user_id"] = > string(1)"1" ["article_uid"] = > string(14)"testb" ["deleted_at"] = > NULL["created_at"] = > string(20)"-0001-11-30 00:00:00" ["updated_at"] = > string(20)"-0001-11-30 00:00:00"
}[1] = > array(6) {
["id"] = > string(1)"2" ["user_id"] = > string(1)"1" ["article_uid"] = > string(6)"test" ["deleted_at"] = > NULL["created_at"] = > string(20)"-0001-11-30 00:00:00" ["updated_at"] = > string(20)"-0001-11-30 00:00:00"
}[2] = > array(6) {
["id"] = > string(1)"3" ["user_id"] = > string(1)"1" ["aritlce_uid"] = > string(18)"testb" ["deleted_at"] = > NULL["created_at"] = > string(19)"2015-02-08 11:25:32" ["updated_at"] = > string(19)"2015-02-08 11:25:32"
}
}
理想情况下,我想针对article_uid
检查$favourites
数组中的$item->uid
。但是我收到了这个错误,我不确定我做错了什么,我认为这是因为我的数组比PHP.net上的标准文档更复杂。在将其传递给视图或其他方法之前,是否需要简化它?有什么想法吗?
答案 0 :(得分:2)
如果您只需要将ID作为数组,请使用lists()
:
$favourites = Auth::user()->favourites()->lists("article_uid");
顺便说一下,您收到的错误是由于您将字符串传递给get()
而引起的。你需要传递一个数组(即使它只是一列):
$favourites = Auth::user()->favourites()->get(["article_uid"])->toArray();