I'm having the strangest problem today, where something that always works has somehow stopped working. Odds are, it's one of those stupid simple issues that I'm too blind to see.
So here's what's going on:
I have the following file structure (I'm only showing relevant files)
~/project
v app
v Models
User.php
v Support
v Traits
Sluggable.php
composer.json
composer.lock
User.php
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use App\Support\Traits\Sluggable;
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
//////////////
//* Traits *//
//////////////
use Authenticatable, Authorizable, CanResetPassword, Sluggable;
////////////////////////
//* Model Attributes *//
////////////////////////
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
/////////////////////
//* Boot Override *//
/////////////////////
/**
* Override the Boot Method to schedule tasks that are Event-Triggered.
*
* @return void
*/
public static function boot()
{
// Call the Parent Method
parent::boot();
// Intercept the 'Created' Event
static::creating(function($user)
{
// Assign a Validation Token
$user->validation_token = str_slug(str_random(64));
// Initialize the Slug for the User
$user->slug = $user->makeSlug();
});
}
////////////////////////////
//* Attributes Overrides *//
////////////////////////////
/**
* Overrides the $user->name Attibute to update the Sluggable Name.
*
* @return void
*/
public function setNameAttribute($name)
{
$this->attributes['name'] = $name;
$this->attributes['slug'] = $this->makeSlug();
}
}
Traits.php
<?php
namespace App\Support\Traits;
trait Sluggable
{
/**
* The Name of the Attribute that determines the Slug.
*
* Use $this->$sluggableAttribute to determine the Sluggable Value.
*
* @var string
*/
protected $sluggableAttribute = 'name';
/**
* The Name of the Attribute that contains the Slug.
*
* Use $this->$slugAttribute to determine the Slug Value.
*
* @var string
*/
protected $slugAttribute = 'slug';
/**
* Generates a Slug based on the Sluggable Attribute.
*
* @return string
*/
public function makeSlug()
{
// Intialize the Slug
$this->$slugAttribute = str_slug($this->$sluggableAttribute);
// Check for an existing Slug
$slug = $this->whereRaw("{$this->slugAttribute} RLIKE '^{$this->$sluggableAttribute}(-[0-9]+)?$'")
->latest($timestamps ? UPDATED_AT : $this->primaryKey) // Find the Latest Slug
->pluck($this->slugAttribute); // Only grab the Slug Attribute
// See if a Slug was Found
if($slug)
{
// Determine the new Slug Identifier
$slugID = intval(end(explode('-', $slug))) + 1;
// Reassign a new Slug
$this->$slugAttribute .= '-' . $slugID;
}
return $this->$slugAttribute;
}
}
And I end up getting the error:
FatalErrorException in User.php line 19:
Trait 'App\Support\Traits\Sluggable' not found
And I have no idea why.
If it matters, I'm using Laravel 5.1 as my Framework.
Edit:
Here's the composer.json file, in case that matters:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"illuminate/html": "^5.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~4.0",
"phpspec/phpspec": "~2.1"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Helpers/route.php"
]
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"pre-update-cmd": [
"php artisan clear-compiled"
],
"post-update-cmd": [
"php artisan optimize"
],
"post-root-package-install": [
"php -r \"copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
}
}