vendredi 28 juillet 2017

laravel eloquent for hasMany relation does not work

I have these relations in my database

Schema::create('my_users', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('fullName');
        $table->string('userName');
        $table->string('password');
        $table->string('photo');
        $table->string('email');
    });

and

Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('photo');
        $table->text('caption');
        $table->integer('like');
        $table->integer('my_user_id');
    });

my models :

class myUser extends Model
{
    public function post()
    {
        return $this->hasMany('App\post');
    }        

}
 class post extends Model
    {
        public function user()
        {
            return $this->belongsTo('App\myUser');
        }


}

now I'm trying to get a user's data using the following code :

$post = post::find(1);        
$user =  $post->user;

but it doesn't work and $user is NULL. I'm sure that in posts table there is a record with id 1. the weird part is this that I get no error when I change the method's name like this :

$post = post::find(1);        
$user =  $post->somethingBullshit;

I'm using laravel 5.3.

help please.



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

Aucun commentaire:

Enregistrer un commentaire