Hi Asetss,
In your controller you should be able to do something like this:
$company = Company::find(1);
$contacts = $company->contacts;
var_dump($contacts);
The var dump should give you an array of all contacts that are associated with the the company with an id of '1'.
Does that make sense? First you need get the Company and then you can ask for its Contacts.
T2theC said:
Hi Asetss,
In your controller you should be able to do something like this:
$company = Company::find(1); $contacts = $company->contacts; var_dump($contacts);
The var dump should give you an array of all contacts that are associated with the the company with an id of '1'.
Does that make sense? First you need get the Company and then you can ask for its Contacts.
object(Illuminate\Database\Eloquent\Collection)[370]
protected 'items' =>
array (size=1)
0 =>
object(Contact)[366]
protected 'guarded' =>
array (size=0)
...
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=5)
...
protected 'original' =>
array (size=5)
...
protected 'relations' =>
array (size=0)
...
protected 'hidden' =>
array (size=0)
...
protected 'visible' =>
array (size=0)
...
protected 'appends' =>
array (size=0)
...
protected 'fillable' =>
array (size=0)
...
protected 'dates' =>
array (size=0)
...
protected 'touches' =>
array (size=0)
...
protected 'observables' =>
array (size=0)
...
protected 'with' =>
array (size=0)
...
protected 'morphClass' => null
public 'exists' => boolean true
I have contacts through
$company = Contact::find(1);
$contacts = $company->company;
var_dump($contacts);
dumps
object(Company)[366]
protected 'guarded' =>
array (size=0)
empty
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=5)
'id' => int 1
'title' => string 'Citicom' (length=7)
'body' => string 'company body' (length=12)
'created_at' => string '2014-10-03 09:15:51' (length=19)
'updated_at' => string '2014-10-03 09:15:51' (length=19)
protected 'original' =>
array (size=5)
'id' => int 1
'title' => string 'Citicom' (length=7)
'body' => string 'company body' (length=12)
'created_at' => string '2014-10-03 09:15:51' (length=19)
'updated_at' => string '2014-10-03 09:15:51' (length=19)
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'fillable' =>
array (size=0)
empty
protected 'dates' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
protected 'morphClass' => null
public 'exists' => boolean true
Only through the loop does not work. or communication problems?
$contact = Contact::all();
foreach($contact as $contacts) {
$contacts->company->title;
}
I should like this.
Try this:
$contacts = Contact::all();
foreach($contacts->company as $contact)
{
dd($contact['title'];
}
Generally, you would pass $contacts through to your view. You can then run your foreach() in there and echo out what you need.
T2theC said:
Try this:
$contacts = Contact::all(); foreach($contacts->company as $contact) { dd($contact['title']; }
Generally, you would pass $contacts through to your view. You can then run your foreach() in there and echo out what you need.
I do this. Your code error.
this contact view
@foreach ($contacts as $contact)
{{{ $contact->title }}}</td>
{{{ $contact->company->title }}}
@endforeach
Error trying to get property of non-object
Ahh, OK I see what you are trying to do now. You are using a hasMany() - This assumes that you are going to have more than one Company for each Contact.
So $contact->company will return an array. Try this:
@foreach ($contacts as $contact)
{{{ $contact->title }}}
@foreach($contact->company as $company)
{{{ $company['title'] }}}
@endforeach
@endforeach
T2theC said:
Ahh, OK I see what you are trying to do now. You are using a hasMany() - This assumes that you are going to have more than one Company for each Contact.
So $contact->company will return an array. Try this:
@foreach ($contacts as $contact) {{{ $contact->title }}} @foreach($contact->company as $company) {{{ $company['title'] }}} @endforeach @endforeach
Error (Invalid argument supplied for foreach() )
here so I have to http://s52.radikal.ru/i135/1410/3b/b6cac375348e.png
I'm doing this right?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community