0

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 SANTESH"
"section" => array:2 [ …2]
]

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

If I check with dd($itemregistration);

it produce

 Collection {#1948 ▼
 #items: array:1125 [▼
 0 => Itemregistration {#1990 ▼
  #primaryKey: "ItemRegistrationID"
  #hidden: array:1 [ …1]
  +timestamps: false
  #connection: "mysql"
  #table: null
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:133 [ …133]
  #original: array:133 [ …133]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [ …1]
  #touches: []
  #visible: []
  #fillable: []
  #guarded: array:1 [ …1]

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

 @foreach($itemregistrations as $index => $value)                                                                
    <td>{{ $value->name }}</td>
    <td>{{ $value->section->sectionname }}</td>
 @endforeach 

I also tried:

 @foreach($itemregistrations as $index => $value)                                                                
    <td>{{ $value->name }}</td>
    @foreach($value as $section => $val) 
     <td>{{ $val->sectionname }}</td>
    @endforeach 
 @endforeach 

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

I tried to see the values contained in the collection through this code:

    @foreach ($itemregistrations as $item)
     <tr>
       <td>{{ $item }}</td>
       <td>{{ $item->section }}</td>                             
     </tr>
    @endforeach  

And it shows this value in this format:

{"ItemRegistrationID":1,"name":"A Santesh"{"SectionID":12,"sectionname":"E"}}

If I show the result with this: @foreach ($itemregistrations as $item) {{ $item->name }}
@endforeach

I can get the name list but to get the sectionname with this

    @foreach ($itemregistrations as $item)
     <tr>
       <td>{{ $item->section->sectionname }}</td>                           
     </tr>
    @endforeach 

It shows error "Trying to get property of non-object ". I dont understand why I can't get the section values.

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?

joun
  • 656
  • 1
  • 8
  • 25
  • I had edited my question and add some clues to identify the error. Can anyone help me figure out what missing in my code? Thanks – joun Sep 27 '19 at 01:31

3 Answers3

1

You can do with the following code:

@foreach($itemregistrations as $index => $value)
  <td>{{ $value->name }}</td>
  @foreach($value->section as $section) 
    <td>{{ $section->sectionname }}</td>
  @endforeach 
@endforeach 
  • owh..why need if else here? – joun Sep 26 '19 at 01:55
  • also get the same error and it shows the error at --> {{ $section->sectionname }} – joun Sep 26 '19 at 02:00
  • yes..but it shows the same error at section->sectionname – joun Sep 26 '19 at 03:46
  • I think the problem in your relation. How do you get that array? Your relation is one to one. You have to fix the relation otherwise you will get a single item in section relation. – Mostakim Billah Sep 26 '19 at 04:39
  • my relation one to many..itemregistration can have one section but one section can belong to many itemregistration. I am new to eloquent relation so I did that according to samples available in tutorials – joun Sep 26 '19 at 04:49
  • I see.. Your itemregistration contains only one section and the relation also ok. you will get only one section from itemregistration. You can get multiple itemregistration from section object. So what's the point to loop over section since one itemregistration contains only one section? what you exactly want to show in your blade template? – Mostakim Billah Sep 26 '19 at 05:18
  • section values included in other tables grabbed using relation with itemregistration. I want to display the section values but failed. I used relation instead of raw sql queries to improve speed performance – joun Sep 26 '19 at 05:25
  • I put the collection content of itemregistration in the question – joun Sep 26 '19 at 06:02
1

I assume, that @foreach call toArray() on Collection, so then you should handle your data as named array, so try this:

@foreach($itemregistrations as $index => $value)                                                                
  <td>{{ $value['name'] }}</td>
  @foreach($value['section'] as $section => $val) 
    <td>{{ $val['sectionname'] }}</td>
  @endforeach 
@endforeach 
Stano
  • 86
  • 4
  • there is error says "Illegal string offset 'sectionname'" – joun Sep 26 '19 at 09:19
  • Don't you have typo in this sectionname? Please check it – Stano Sep 27 '19 at 08:21
  • the sectionname is the right field name. I found the solution as answered below. maybe you have an explaination to it..I am not really understand it but I just tried possible ways and it worked. – joun Sep 27 '19 at 08:22
0

Thank you for others' help with my question.

I had configured the right syntax to get to the values of sectionname in section array. I don't know why I can't use -> helper to get to the values. I tried the following syntax and it can fetch the values in blade view.

 @foreach ($itemregistrations as $item)
 <tr>
   <td>{{ $item->name }}</td> //also can <td>{{ $item['name'] }}</td>
   <td>{{ $item['section]['sectionname'] }}</td>                             
 </tr>
@endforeach  
joun
  • 656
  • 1
  • 8
  • 25