0

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?

0

1 Answer 1

2

If you just need the ids as an array, use lists():

$favourites = Auth::user()->favourites()->lists("article_uid");

By the way the error you received is caused by the fact that you passed a string to get(). You need to pass an array (even if it's just one column):

$favourites = Auth::user()->favourites()->get(["article_uid"])->toArray();
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, this solves the error and outputs the array array as so: array(3) { [0]=> string(14) "testb" [1]=> string(6) "testb" [2]=> string(18) "testc" } i've left the in_array method untouched but this still outputs the add favourite button event though this is in the array
Can you echo $item->uid to make sure it holds the correct value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.