lundi 2 mai 2016

Eloquent ORM relationship on laravel 5

I create messages system on laravel 5, but I cant get sender information on recuperation page (such as name email...)

I have messages table:

public function up()
    {
        Schema::create('messages', function(Blueprint $table){
            $table->increments('id');
            $table->integer('destination_id')->unsigned();
            $table->integer('source_id')->unsigned();
            $table->string('sujet');
            $table->text('contenu');
            $table->boolean('vu')->default(0);
            $table->boolean('repondu')->default(0);
            $table->timestamps();
            $table->foreign('source_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('destination_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

On the models I create add this:

Message Model:

class Message extends Model {

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

and on User model I add this function:

public function message()
{
    return $this->hasMany('App\Message');
}

But when i Try to fetch user messages and want to get for example I get a error message that I try to get property of non-object

this is the controller to this page



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire