Hi I am using Laravel and I fetching the logged in users favourites from a database table. Whilst I am in the view of an article that I am fetching from a CMS API, I am also fetching the logged in users favourites, I want to check if the user has already favourited this article but fetching the favourites and checking if the UID exists within the array and if it does i will output a different styled icon. So I thought I would pass my data as an array and then use the php function in_array however I get this error:
Argument 1 passed to Illuminate\Database\Grammar::columnize()
must be of the type array, string given
This is how I am retrieving my data:
//fetch article from cms
$item = $this->client->fetchPage($id);
$favourites = Auth::user()->favourites()->get("article_uid")->toArray();
and then in my view
@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
My array is output as so:
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"
}
}
Ideally I want to check the article_uid in the $favourites array against the $item->uid. However I am getting this error, I'm not sure what I'm doing wrong, I think its because my array is more complex than the standard documentation on PHP.net. Do I need to simplify it before passing it to the view or another method? Any ideas?