New Class Name "Song" Extends Laravel 5 Model Class not found
I have created new Class Called "song".
Location of the class app/ClassName , so its app/songs.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Song extends Eloquent {
//put your code here
}
My Route:
Route::get('/','[email protected]');
My SongsController.php location: app/Http/controller/SongsController.php
Wanting to correspond my database table called "songs".
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Song;
class SongsController extends Controller
{
public function index()
{
$songs = Song::get();
return view('pages.songsList',compact('songs'));
}
Now with all these code, I get a simple error says as below,
FatalErrorException in SongsController.php line 19:
Class 'App\Song' not found
If I use without adding class "Song" model works fine. The code is as below
DB::table('songs)->get();
Thanks in Advance, Sorry I am new to Lavaral 5.
Answer
Your class is called Song
and the file is called songs.php
. You need to name the file Song.php
in order for it to be found.
Composer generates a PSR-4 compatible autoloader.
You'll notice in your composer.json file you have a section like this
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
This tells the Autoloader that anything in the App
namespace should be found within the app
directory.
When classes are called, any registered autoloaders attempt to load the class file. This means that App\Song
looks for app/Song.php
unless you specify otherwise.
From the specification
When loading a file that corresponds to a fully qualified class name ...
A contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace separator, in the fully qualified class name (a "namespace prefix") corresponds to at least one "base directory".
The contiguous sub-namespace names after the "namespace prefix" correspond to a subdirectory within a "base directory", in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.
The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.
Related Questions
- → How to update data attribute on Ajax complete
- → how to convert stdclass into string
- → How to dynamically add class to parent div of focused input field?
- → Throttling dispatch in redux producing strange behaviour
- → Mysterious syntax onClick={::this.submit}
- → Browser support for class syntax in Javascript
- → Get the calling element with vue.js
- → Using css modules how do I define a global class
- → Confirming Navigation with React Router without Mixin for es6 Classes
- → redux how to redirect on componentWillMount using react-router
- → Function Undefined in Axios promise
- → Maximum call stack exceeded when instantiating class inside of a module
- → List class element(index).fadetoggle(some speed) not working