0
votes

I'm working on a photography website at the moment and am having trouble with the page that should show all the albums and pictures within the album. I always get an error

Class 'Photo' not found (View: /var/www/photography.website/resources/views/album/index.blade.php)

when trying to access the page.

Following are the view, controllers, and models;

Index View:

@extends('layout.main')
@section('title')
<title>Albums</title>
@endsection
@section('content')
<div class="content">
@if(count($albums)==0)
    <p>Nothing here yet, come back later!</p>
@else
    @foreach($albums as $album)
        <div class="album">
            <h3>{{$album->name}}</h3>
            @foreach($album->photos()->getEager() as $photo)
                <img src="/public/thumb-{{$photo->id->toString()}}.jpeg" class="">
                @endforeach
            </div>
        </div>
    @endforeach
@endif
@endsection

Album Controller (snippet):

<?php

namespace App\Http\Controllers;

use App\Photo;
use App\Album;
use Illuminate\Http\Request;

class AlbumController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $albums = Album::all();
        return view('album.index')->with(compact($albums));
    }
}

Album Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Album extends Model
{
    protected $fillable = ['name','cover'];
    public function photos() {
        return $this->hasMany('Photo');
    }
}

Photo Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{
    public $incrementing = false;
    protected $fillable = ['id','album_id','title'];
}
1

1 Answers

1
votes

You are using the Unqualified Name of the class in your relationship. You would need to use the full name of the class when referring to the class via string like that.

There is no class named Photo in the application but there is a class named App\Photo.

public function photos() {
    return $this->hasMany('App\Photo');
    // but probably almost always better to use the class constant
    return $this->hasMany(Photo::class);
    // 'App\Photo'
}

Photo here refers to a class in the current namespace App, as there is no alias for anything named Photo.