mercredi 25 septembre 2019

How to display eloquent query into view blade?

I am querying a list of staffs from 'itemregistration' table with the eloquent relationship with 'section' table. But I cannot display the information from section table to the view blade.

My controller get the query as follows:

  $itemregistrations = Itemregistration::with('section')->get();

When I check the array with:

 dd($itemregistrations->toArray());  

I get this result:

 0 => array:133 [▼
"ItemRegistrationID" => 1
"RegistrationDate" => "2005-12-01"
"SectionID" => 12
"name" => "A RAMESH A/L ARTHUNARAYANAN"
"section" => array:2 [ …2]
]

The section array should contain 'SectionID' and 'sectionname' fields.

I want to display the result in the blade but failed to fetch the section array value:

 @foreach($itemregistrations as $index => $value)                                                                
    <td></td>
    <td></td>
 @endforeach 

I also tried:

 @foreach($itemregistrations as $index => $value)                                                                
    <td></td>
    @foreach($value as $section => $val) 
     <td></td>
    @endforeach 
 @endforeach 

But also failed. The error appears is "Trying to get property of non-object".

My section model:

  class Section extends Authenticatable
{
protected $table = 'sections';
protected  $primaryKey = 'SectionID';
 /**
 * Get the user that have agama.
 */
public function itemregistrations()
{
    return $this->hasMany('App\Itemregistration', 'SectionID', 'ItemregistrationID');
}

}

Itemregistration model:

  class Itemregistration extends Model 
{
protected  $primaryKey = 'ItemRegistrationID';

/*
 * Get all of the owning models.
 */

public function section()
{
    return $this->belongsTo('App\Section', 'SectionID');

}

How to get display the query with section array values?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire