PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 12, 2022

[FIXED] Laravel 8 - Get Data One to Many Relation

 February 12, 2022     laravel, php, relation     No comments   

Issue

I Have Problem with get data

This is my code Controller

$class = DetailMentor::with('Kelas')->where('id', Auth::user()->id)->get(); 

Model:


    protected $primaryKey = 'id_mentor';

    protected $fillable = [
        'id',
        'id_kelas',
    ];

    public $timestamps = true;

    public function Kelas()
    {
        return $this->hasMany(Kelas::class, 'id_kelas','id_kelas');
    }

    public function User()
    {
        return $this->belongsTo(User::class, 'id','id');

View index

 <tbody>
                                     
 @forelse ($class->Kelas as $item)
 <tr role="row" class="odd">
   <th scope="row" class="small" class="sorting_1">{{ $loop->iteration}}.</th>
   <td>{{ $item->nama_kelas }}</td>
   <td>{{ $item->Jeniskelas->jenis_kelas }}</td>
   <td>{{ $item->level->nama_level }}</td>
   <td class="text-center">
       @if ($item->status_video == 'Telah Dibuat')
          <span class="badge badge-success ">Telah Dibuat</span>
       @else
          <span class="badge badge-danger">Belum Dibuat</span>
       @endif
   </td>
   </tr>
 @empty
                                        
 @endforelse
                                        
</tbody>

Error: Property [Kelas] does not exist on this collection instance. (View: C:\laragon\www\Bootcamp\resources\views\mentor\listkelas\index.blade.php)

I want to group data kelas by id. If i use first it only takes one data, If i use get it say error. Can someone Helpme, Please! I'm new to this

[Picture: Array Return $class][1] [1]: https://i.stack.imgur.com/0fjIj.png


Solution

the first ensure if the data get correctly test it on postman or on browser you can use method dd() so if the data come correct

the problem in view index.blade.php
I think it works on first method because you get one object (one record)

and it doesn't work for get method because it gets array of object so the way of display this array it is different from the one object

ensure the way of display it and use foreach in index.blade.php

hopefully that is help you

update:

        @foreach ($class as $item)
         <tr role="row" class="odd">
         <th scope="row" class="small" class="sorting_1">{{ $loop->iteration}}.</th>
         <td>{{ $item->kelas[0]->nama_kelas }}</td>
         <td>{{ $item->kelas[0]->Jeniskelas->jenis_kelas }}</td>
         <td class="text-center">
       @if ($item->kelas[0]->status_video == 'Telah Dibuat')
          <span class="badge badge-success ">Telah Dibuat</span>
       @else
          <span class="badge badge-danger">Belum Dibuat</span>
       @endif
   </td>
   </tr>
                                                               
     @empty

     @endforeach

you can access like this $item->kelas[0]->nama_kelas



Answered By - Ossama Abd
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing