Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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
Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Asetss asetss Joined 3 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.