naekobest
4 months ago
697
11
Eloquent

Property [name] does not exist on this collection instance.

Posted 4 months ago by naekobest

Hello there, im stuck for almost 3days now on this error and i cant seem to get my head around on how to fix it.

Method Illuminate\Database\Eloquent\Collection::subjects does not exist. (View:\resources\views\topic\index.blade.php)

Im essentially trying to bring Exam in a relationship to subjects in a relationship to topics. Exams have many subjects, subjects have many topics Topics belong to subjects, subjects belong to exams

Exam Model

class Exam extends Model
{
    protected $guarded = [];

    public function subjects()
    {
        return $this->hasMany(Subject::class);
    }
}

Subject Model

class Subject extends Model
{
    protected $guarded = [];

    public function exams()
    {
        return $this->belongsTo(Exam::class);
    }

    public function topics()
    {
        return $this->hasMany(Topic::class);
    }
}

Topic Model

class Topic extends Model
{
    protected $guarded = [];

    public function subjects()
    {
        return $this->belongsTo(Subject::class);
    }
}

Topic Controller

    public function index(Topic $topic)
    {
        
        $topics = Topic::paginate(10);

        $topic->load('subjects');

        return view('topic.index', compact('topics'));
    }

Topic Index

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-12 mt-4">
            <p class="h1 text-center">Übersicht Fächer</p>
            <table class="table table-striped">
            <thead>
              <tr>
                <th scope="col">Name</th>
                <th scope="col">Fach</th>
                <th scope="col">Aktion</th>
              </tr>
            </thead>
              <tbody>
                @forelse($topics as $topic)
                    <tr>
                        <td><a href="{{ route('topic') }}/{{ $topic->id }}">{{ $topic->name }}</a></td>
                        <td>
                            {{-- @foreach ($topics->subjects()->name as $subject)
                                {{ $subject->name }}
                            @endforeach --}}
                        </td>
                        <td>
                            <li type="button" class="btn btn-primary btn-sm">Show Subjects</li>
                            <li type="button" class="btn btn-info btn-sm">Edit</li>
                            <li type="button" class="btn btn-danger btn-sm">Delete</li>
                        </td>
                    </tr>
                @empty
                    <p>Keine Fächer vorhanden.</p>
                @endforelse
              </tbody>
            </table>
            <div>
                <a class="btn btn-primary" href="{{ route('subject') }}">New subject</a>
                <p class="mx-auto">{{ $topics->links() }}</p>
            </div>    
        </div>
    </div>
 </div>
@endsection

Under "Fach" (subject) i want to display the corresponding subject name to each topic

Please sign in or create an account to participate in this conversation.