Doubt between relationships with Laravel and Eloquent

Asked

Viewed 125 times

1

I have the following model, using Laravel 5.8

inserir a descrição da imagem aqui

I ran all the records and structured the relationships. On my Dashboard I need to list in card form all reports from a specific user. How could I do this?

So far I can get all of that user’s groups doing this:

$obj = User::with('grupos')->get();

However, how can I access the other table and get to the report? I tried to do something like this:

$obj = User::with('grupos', 'relatorios')->get();

However, the relationship is in another table can not list the data

User model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','cliente_id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

   public function cliente()
   {
       return $this->belongsTo('App\models\Cliente');
   }

   public function grupos(){
       return $this->belongsToMany("App\models\Grupo", "acessos");
   }
}
  • How’s your user model class?

  • edited in the main question

1 answer

1


You’ll have to change that line:

$obj = User::with('grupos', 'relatorios')->get();

for that:

$obj = User::with('grupos.relatorios')->get();

Whereas in the model grupos has to have the declaration of a relationship with relatorios.

When you use the first form, the class understands that there is a direct relationship with the other, but in your model you can only make the relationship with the class groups and customers.

The statement form of this line is wrong, the correct is:

$obj = User::with(['grupos', 'relatorios'])->get();
  • Show, it worked. All reports are coming, however I need to return only the reports of a particular customer. Within the Reporting model I have a relationship with customers, following the above logic, shouldn’t this work? $obj = User::with('groups.reporios.client')->get() Yet all reports are returned

  • Not like this in Eloquent, for this you will have to change the way you perform the search, because that way, you are pulling the group reports first and then the customer, owner of that report.

  • I would redesign the bank, because the client has n users, the user has access to n clients not only his and the clients have n reports. Would that be it? If it’s right, it’s $obj = User::with('groups.cliente.reports')->get()

  • Inside the report I have the client, searching from the User I can not filter the cliente_id of the User model, with the cliente_id of the report? What would be the other way to do that?

  • I don’t know if I’m thinking too big for your project, but here’s a link to the table template I’m thinking https://drive.google.com/open?id=10tOuh5iXeyq3U7TEzNYDxIO4p9Cf4ai_

  • I get it, but in this case you just put the relationship between Client and Group, right? In my model the Groups would be for all customers, so I did not make this relationship. However, this way I could get all reports per group from a user filtering by the client?

  • Sorry, I sent you the wrong model. Check this one out: https://drive.google.com/open?id=1U_sdlaqu_-C0HQzrLFRmwPN5CU04gft3, a doubt, a client may be in more than one group?

  • The client does not, only the user. The user belongs to a client. and that user may be in more than one group

  • Man, thanks for the help... but it was giving a lot of headache.. I could solve without messing with the structure. Only I did it by queryBuilder. I still have to better understand these complex relationships using Eloquent

  • Quiet! Success there!!

Show 5 more comments

Browser other questions tagged

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