1

My model Dispatch has a field invoice_id. It is a foreign key so i can get the invoice details using the below code:

protected $fillable = [
    'id',
    'truck_no',      
    'driver_name',      
    'driver_phone',      
    'gps_details',      
    'invoice_id',      
];

public function invoice(){
    return $this->belongsTo('App\Invoice')->select('id','invoice_no','permit_id');
}

Now I want to get the value permit_id from invoice() so i can use it to get the details of the Permit.

permit_id = id of Permit model

So I use the below code to get the permit data.

public function permit(){
    return $this->hasOne('App\Permit','id',$this->invoice()->permit_id);
}

Update:

My Invoice Model has :

class Invoice extends Model
{
    protected $fillable = [
        'id',
        'invoice_no',
        'invoice_date',
        'permit_id',
    ];

    public function permit(){
        return $this->hasOne('App\Permit', 'id', 'permit_id');
    }
}

My Permit Model has:

class Permit extends Model
{
    protected $fillable = [
        'permit_type',
        'permit_no',
        'application_no',  
        'supply_unit', 
        'supply_unit_id' ,  
     ];

    public function supplyunit(){
        return $this->hasOne('App\SupplyUnit','id','supply_unit_id');
    }
}

And as per suggestions i have added below code in my Dispatch Model:

 class Dispatch extends Model
  {
    protected $fillable = [
        'id',
        'truck_no',      
        'driver_name',      
        'driver_phone',      
        'gps_details',      
        'invoice_id',      
    ];

    public function invoice(){
        return $this->hasOne('App\Invoice','id','invoice_id');
    }

    public function permit(){
        return $this->hasOne('App\Permit','id','permit_id');
    }
  }

But it doesn't work. What should i do to achieve the above? Is there any other solutions please suggest.

2 Answers 2

4

Assuming each invoice has one permit, your relationship definition should look like this:

public function permit(){
    return $this->hasOne('App\Permit', 'id', 'permit_id');
}

Edit: If invoice belongs to permit, which is the inverse, your relationship would look like this instead:

public function permit(){
    return $this->belongsTo('App\Permit', 'permit_id');
}

Edit: Based on your updated question, I think you got the relationship definitions a bit wrong. The following should work:

Since you have an invoice_id column in App\Dispatch, it means that each App\Dispatch belongs to an invoice.

In App\Dispatch, your relationship definition should be as follows:

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

// permit does not belong to `App\Dispatch` as a direct relationship
// it should be removed

In App\Invoice, your relationship definition should be as follows:

public function dispatch() {
    return $this->hasOne('App\Dispatch');
}

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

In App\Permit, your relationship definition should be as follows:

public function invoice() {
    return $this->hasOne('App\Invoice');
}

To then retrieve the permit id from an Invoice model, you would do

$invoice->permit->id; 
8
  • Yes each invoice belongs to one permit. public function permit(){ return $this->belongsTo('App\Permit'); } This is what i use. But your code shows null.
    – Lano Angel
    Jan 13, 2019 at 7:37
  • Edited my answer to define the inverse of the relationship.
    – Mozammil
    Jan 13, 2019 at 7:40
  • Can you show us your relationship definitions in the following classes: App\Permit, App\Dispatch and App\Invoice?
    – Mozammil
    Jan 13, 2019 at 8:09
  • Ok should we start a chat?
    – Lano Angel
    Jan 13, 2019 at 8:12
  • Let us continue this discussion in chat.
    – Lano Angel
    Jan 13, 2019 at 8:13
2

Change this line

return $this->belongsTo('App\Invoice')->select('id','invoice_no','permit_id');

To

return $this->belongsTo('App\Invoice');

And add the following code on Invoice

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

And you can access as

Dispatch::find($id)->invoice->permit->id;

Or if you want all the information

Dispatch::find($id)->invoice->permit;
6
  • check this. you can easily get all the information.
    – Yasii
    Jan 13, 2019 at 8:45
  • I want to get it in the model itself. Using the invoice().Is it possible?
    – Lano Angel
    Jan 13, 2019 at 8:46
  • don't know about that since i didn't need to do that. but what are you hoping to gain from that?
    – Yasii
    Jan 13, 2019 at 8:53
  • The reason is because i would have to loop through all the data and add permit_no like this. $dispatches = Dispatch::with('invoice')->get(); foreach ($dispatches as $index => $value) { $permit_no = Dispatch::find($value->id)->invoice->permit->permit_no; $value['sno'] = ++$index; $value['permit_no'] = $permit_no; }
    – Lano Angel
    Jan 13, 2019 at 9:00
  • you can do this way.$dispatches= Dispatch::all(); foreach($dispatches as $dispatch){ $permit_no = $dispatch->invoice->permit->id; }. Likewise you can get others as well. Don't need to call the model again
    – Yasii
    Jan 13, 2019 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.