samedi 22 août 2015

Laravel 5 Displaying related model data on blade

I would like to display the data of a related table on my view. Here's what I have

class DrugLog extends Model
{
    public function drug() {
      return $this->belongsTo('App\Drug');
    }

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


class Drug extends Model
{
    public function drugLogs() {
      return $this->hasMany('App\DrugLog');
    }
}

class DispensedDrug extends Model
{
    public function drugLog() {
      return $this->belongsTo('App\DrugLog');
    }
}

Now on my controller I have this

$drug = Drug::where('id', $id)->owned()->first();
$drug_logs = DrugLog::with('dispensed_drugs')->where('drug_id', $drug->id)->get();

return view('drug.details', compact('drug_logs', 'drug'));

Now the displaying of Drug Details is working, but my problem is trying to access the data from DispensedDrug model. Since this model is related to DrugLog. I would like to get the name of the dispensed drugs and that field is residing on "dispensed_drugs" table

I'm tried this but none works. Any idea guys?

@foreach ($drug_logs as $logs)
    {{ $log->dispensedDrug->name }}
    {{ $log->dispensed_drugs->name }}
    {{ $log->dispensedDrug()->name }}
    {{ $log()->dispensedDrug->name }}
    {{ $log->dispensedDrug()->name }}
@endforeach

I'm trying to get the name column located on dispensed_drug table. But with no luck I can't get the value. Any idea would be much appreciated. Thanks



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1JnGpgi
via IFTTT

Aucun commentaire:

Enregistrer un commentaire