Relationship of Laravel Relationship

Asked

Viewed 389 times

2

I have a model called Processo, this model has a relationship hasMany with the model Andamento, the model Andamento has a queryScope what use to return the data already with other relations of this model, what I want to do is: through the relationship of Processo with the Andamento, it is also possible to return the relations of the queryScope of Andamento, this is possible?

Code:

model Process.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Processo extends Model {

    function andamentos () {
        return $this->hasMany('App\Processos\Andamento');
    }

    function scopeInfo($query) {
        return $query->with('andamentos');
    }

}

model Andamento.php

namespace App\Processos;

use Illuminate\Database\Eloquent\Model;

class Andamento extends Model
{
    protected $table = 'processos_andamentos';
    protected $guarded = [];

    function scopeInfo ($query) {
      return $query->with('fase', 'tipo_andamento');
    }

    function processo () {
        return $this->belongsTo('App\Processo');
    }

    function fase () {
    return $this->hasOne('App\Processos\Fase', 'id', 'fase_processo_id');
    }

    function tipo_andamento () {
        return $this->hasOne('App\Processos\TipoAndamento', 'id', 'tipo_andamento_id');
    }
}

The call would be something like:

$Processo = Processo:Info()->whereId(1);

$andamentos_fases = $Processo->andamentos->fase
  • 1

    tries so, if it works I create the answer Return $this->hasMany('App Processes Progress')->with('scopeInfo');

  • 1

    if you do not try Return $this->hasMany('App Processes Progress')->with('phase', 'tempo type');

  • With the "scopeInfo" it returns: "Too few Arguments to Function App Processes Progress::scopeInfo()", worked with the second comment, but is it possible to get the scopeInfo even? so you don’t have to rename each new method

1 answer

2


IS as follows:

Processo::with(['andamentos.fase'])->get()

That is, the name of the separated relations by point and if you have other array.

If you can change your Model to load all relations as follows:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Processo extends Model 
{
    function andamentos () 
    {
        return $this->hasMany('App\Processos\Andamento');
    }

    function scopeInfo($query) 
    {
        return $query->with([
            'andamentos.fase', 
            'andamentos.tipo_andamento'
        ]);
    }
}

and

Processo::info()->where('id', 1)->get(); // ou first();

Reading:

  • 1

    It worked, it is possible to already use the scopeInfo of the Progress model with?

  • @Nbayoungcode yes look at the example in the question.

  • @Nbayoungcode if it is the answer of your doubt please tick.

  • 1

    Is there no way to use the Progress Sync directly? Ex: with('Andamento.scopeInfo');

  • @Nbayoungcode not one thing is to load the relations, another thing is to work with Builder Query, I mean, they’re different things.

Browser other questions tagged

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