Laravel - Query with 3 tables (1-N)

Asked

Viewed 99 times

0

I’m trying to relate these tables:

inserir a descrição da imagem aqui

The goal is to show:

Nome,Descrição,Parcela,Valor

Model Cliente:

class Cliente extends Model
{

    public function recebtos()
    {
        return $this->hasMany('App\Recebto','id_cliente');
    }

}

Model Recebto:

class Recebto extends Model
{
    public function cliente()
    {
        return $this->belongsTo('App\Cliente','id_cliente');
    }

    public function parcelas()
    {
        return $this->hasMany('App\RecebtoParcela','id_recebto');
    }
}

Model Parcelas:

class RecebtoParcela extends Model
{
    public function recebto()
    {
        return $this->belongsTo('App\Recebto','id_recebto');
    }
}

No Controller:

class RecebtoController extends Controller
{
    public function index()
    {
        $recebtos = Recebto::paginate(15);
        return view('recebtos.index',compact('recebtos'));
    }

Only this way, if a receipt has more than one portion, only one of them appears in the view. What can I do to have all the plots shown?

  • I need to know the view as you did, because it’s so miss you put too with in both relationships.

1 answer

0

To search the array with all portions of a receipt, just use the with

For example in search of a specific receipt

public function show($id)
{
    $recebtos = Recebto::with('parcelas')
                ->find($id);
}

Browser other questions tagged

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