EChan42
EChan42

Reputation: 139

Models in laravel

I'm developing an app in laravel, specifically a social network. After wasting some time stuck on an issue I found out I had two files wich responded to the user model.

My files

One is namespaced under "appname" and the other under "appname\Models", Adding the posts() method in the one under "appname" gave me an error where the method couldn't be found, so I assumed the one under "appname\Models" was the correct one. Although deleting the "User.php" under "appname" gives me a

Fatal error: Class 'Instyle\User' not found

error.

I'm sure I've misunderstood something along the lines I just can't point out where. app\Models\User.php

namespace Instyle\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

    protected $table = 'users';

    protected $fillable = [
        'username', 
        'email', 
        'password',
        'first_name', 
        'last_name',
        'location',
    ];


    protected $hidden = [
        'password', 
        'remember_token',
    ];

    public function getName()
    {
        if($this->first_name && $this->last_name)
        {
            return "{$this->first_name} {$this->last_name}";
        }
        if ($this->first_name)
        {
            return $this->first_name;
        }
        return null;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function getFirstNameOrUsername()
    {
        return $this->first_name ?: $this->username;
    }

    public function getAvatarUrl()
    {
    $hash = md5(strtolower(trim($this->attributes['email'])));
    return "http://www.gravatar.com/avatar/$hash?d=https://u.pomf.is/maqope.png";
    }

    public function posts()
    {
        return $this->hasMany('Instyle\Post');
    }

}

app/User.php

<?php

namespace Instyle;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function posts()
    {
        return $this->hasMany('Instyle\Post');
    }
}

app\post.php

<?php

namespace Instyle;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = ['body'];

//    protected $appends = ['humanCreatedAt'];

    public function user()
    {
        return $this->belongsTo('Instyle\User');
    }

 }

Upvotes: 0

Views: 765

Answers (2)

frankfullstack
frankfullstack

Reputation: 550

I would avoid to have both models with the same name because it may collide one with the other, although they are in different paths.

Make sure you are importing your custom models with the use path\to\your\namespace\Model; expression to allow the root path of your models to be located in a correct way.

May be helpful to see your Post and User models sample code to evaluate the error.

UPDATE: Why don't you try to generate only one User model and, the other data related with the User Profile, try to save in one One-to-One Relationship to a Profile table? i.e. firstname, lastname, location, twitter_account, facebook_account, phone_number, and so on could be stored in a User_Profile or Profile separated table, then you maintain your User table with the minimum required fields and delete the other one.

Upvotes: 1

aimme
aimme

Reputation: 6773

if your application name is Instyle, Instyle\User is the User.php inside the app folder not the User.php in app/Models/.

More info: learn more in psr namespacing and autoloading. check your compose.json . here it says that namespace Instyle is app/ folder and from here the namespace at the top of each file goes according to the absolute folder path.

    "psr-4": {
        "Instyle\\": "app/"
    }

One more thing, If you are willing to use User.php anywhere else and thinking it for authentication purpose remember to change authentication configuration declared in config/auth.php accordingly

Upvotes: 2

Related Questions