1

I have a multiselect form to store multiple client_id in contact table:

 {!! Form::select('client_id[]', $clients, null, ['multiple'=>true]) !!}

When I show an individual client, I wish to show the related contacts.

My client Model has as a 1:many relationship defined as:

public function contact()
{
    return $this->hasMany('App\Models\contact');
}

usually, for non array items, I would use:

$contacts = $client->contact()->get();

to fetch related contacts, but as the client_id is stored as an array in my contact table, how to I fetch this data?

1 Answer 1

1

I really think you just want this:

$contacts = $client->contact()->lists('id', 'name')->toArray();

Note that you didn't specify your version of Laravel, but in 5.1+ lists returns a Illuminate\Support\Collection instance, but in previous versions it returns an Array.

Sidenote

Your hasMany relationship should be defined as plural and not singular:

public function contacts()

This is by no means a requisite, but it better defines your relationship type.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not working. It doesn't match the client_id of 2 within an array of "2","3"

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.