Reputation: 4515
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?
Upvotes: 1
Views: 115
Reputation: 50787
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.
Upvotes: 1