mercredi 2 janvier 2019

How to create a relationship in laravel

Am new to laravel, I have issues trying to connect this tables: plans,users and loans even after reading the docs,

I have a plans tables that have all my plans, then I have a users table and loans table, my loans table has a user_id and a plan_id, all I want is to pull the records for plans and the users in the loan model.

Loanplan.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Loanplan extends Model
{
    //
    protected $fillable = [
        'title',
        'amount',
        'interest',
        'repayment_month',
        'status',
    ];


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


}

my loan model:

Loan.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Loan extends Model
{
    protected $table = 'loans';
    //
    protected $fillable = [
        'id',
        'user_id',
        'loanplan_id',
        'payment_made',
        'status',
    ];


    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function loanplan()
    {
        return $this->belongsTo('App\Loanplan');
    }    
}

I want get all the loan plans and users table records with plan_id and user_id as foreign respectively respectively in my LoanController.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire