dimanche 22 mai 2016

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://ift.tt/20mRsAC";
    }

    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');
    }

 }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/27NNZkf
via IFTTT

Aucun commentaire:

Enregistrer un commentaire