Laravel Relationship 5.2 Eloquent

Asked

Viewed 1,153 times

0

Good morning, everyone,

I’m having a problem in a relationship between tables with Elouquent (Laravel)

I have two tables [clients / activities_commercial] (innoDB MYSQL)

In the client table there is a field (FK) called: activity_commercial In the table activity_commercial has the field index: id, along with the activity field_commercial (scan)

At the moment when I try to recover the customer information, I would like you to come to the fields of business activity and your id related to that customer.

I’m doing it this way in Controller:

$return = \App\Cliente::find($id)->with('atividadesComerciais')->get();
return view('clientes.show', ['data'=>$return]);

No Model:

public function atividadesComerciais()
    {
        return $this->belongsTo('App\AtividadesComerciais', 'atividades_comerciais_id', 'id');
    }

Before I could access the information in the view by Lade like this

{{ $data['razao_nome'] }}

but now it’s not possible.

Where can I be wrong ?

2 answers

2

The belongsTo relationships of the Laravel are stored in a property of your Model instance. This property will always be the name of the relationship method.

For example, if your model User relates to Role, through the method User::role, we would have the following:

class User {
     public function role() {
        return $this->belongsTo(Role::class);
     }
}

For you can access the relationship data, it would be as follows:

$user = User::with('role')->find($id);

$user->role->name; // admin
$user->role->id; // 3
$user->name; // Wallace de Souza

That is, Laravel will convert the name of your relationship method into a property, which treats the relationship values, if any.

There are some important remarks to be made:

\App\Cliente::find($id)->with('atividadesComerciais')->get();

You put the with after find. In Laravel it makes all the difference, since find is a method of Eloquent Builder, and with is a static method of Model - which in turn returns a new instance of the model itself. So, to get the desired behavior, you should use the with whenever starting any query, to load the relationships "eagerly".

Behold:

 \App\Cliente::with('atividadesComerciais')->find(1);

If you want to load the relationship after having made the query, you will actually use the method load.

Example:

$user = User::find(1);

$user->load('role');

$user->role->id; // 3
  • I’m having trouble with relationships, I did what you explained but the problem persisted. The Relationship property always comes empty.

  • But what’s the problem?

  • I have an N to N relationship between areas and courses. I look for a specific area and use '$area->courses' and always returns empty

1

You’re finishing with the method get(), this results in a collection of templates. Try changing from:

$return = \App\Cliente::find($id)->with('atividadesComerciais')->get();

for

$return = \App\Cliente::with('atividadesComerciais')->find($id);

Thus with the method find() we returned a single model. If you prefer, you can leave the method get(), but in the view you should use this way:

@foreach ($data as $model)
    {{ $model->razao_nome }}
@endforeach

Browser other questions tagged

You are not signed in. Login or sign up in order to post.