0

I have two models: User & Tag. They have a many-to-many relation.

Table users   : id, email, password...
Table tags    : id, name...
table tag_user: id, tag_id, user_id

If can use this code to get an array with all Tags a User has:

$tagArray = User::find($user_id)->tags;

However, I can't find a quicker way to get a simple array with all the tags ids. Something like this:

$tagIdArray = array(1, 2, 3);

1 Answer 1

1

Simply use the lists method, on the tags method.

$tagIdArray = User::find($userId)->tags->lists('id');
Sign up to request clarification or add additional context in comments.

1 Comment

tags not tags() $tagIdArray = User::find($userId)->tags->lists('id');

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.