Laravel 关系多对多

Laravel Relations many to many

可能是一个愚蠢的错误,但我正在学习。 我尝试创建系统消息,但我遇到了问题,因为我与用户有很多消息的关系 模型用户:

class User extends Authenticatable
{
    use Notifiable;


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

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

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

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

模特留言:

class Message extends Model
{
    protected $fillable = [
        'id_user_to_send',
        'id_user_from',
        'text',
        'title'
    ];

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

MessagesController 中的函数

 public function mymessages()
{

    $user= Auth::user()->id;
    $messages = Message::latest()->where('id_user_to_send',$user)->orWhere('id_user_from',$user)->get();
    return view('messages.inbox', compact('messages', 'user'));
}

查看:

    @foreach( $messages as $message)
        <tr>
            <td>
                {{ $message->user->name }}</td><td>{{ $message->title }}
            </td>
            <td>
                <button type="button" class="btn btn-primary">Przeczytaj</button>
            </td>
        </tr>
    @endforeach

错误:/

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'wymiana2.message_user' doesn't exist (SQL: select users.*, message_user.message_id as pivot_message_id, message_user.user_id as pivot_user_id from users inner join message_user on users.id = message_user.user_id where message_user.message_id = 4) (View: C:\wymiana2\resources\views\messages\inbox.blade.php)

Schema::create('messages', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('id_user_to_send')->unsigned();
    $table->integer('id_user_from')->unsigned();
    $table->string('text');
    $table->string('title');
    $table->foreign('id_user_to_send')->references('id')->on('users');
    $table->foreign('id_user_from')->references('id')->on('users');
    $table->timestamps();
});

   Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();


});

我阅读了文档并尝试了很多东西。 我究竟做错了什么?你能帮助我吗?给个提示? 我只想显示用户名, 我可以使用查询,但我仍然需要修复这种关系。

当我有: User.php

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

Message.php

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

我有错误:尝试获取非对象的 属性 'name'(视图:C:\wymiana2\resources\views\messages\inbox.blade.php)

更新

由于消息仅针对特定用户,因此不需要多对多关系。在保持数据库结构的情况下(不需要中间 table),将以下关系添加到 Message 模型。

Message.php

class Message extends Model
{
    public function sender()
        {
            return $this->hasOne('App\User', 'id', 'id_user_from');
        }

    public function reciepient()
        {
            return $this->hasOne('App\User', 'id', 'id_user_to_send');
        }
}

User.php

class User extends Model
{
    public function messages()
    {
        return $this->hasMany('App\Message');
    }
}

在您的控制器或视图中,您可以通过 $user->messages();

访问用户的消息

消息的发送用户 $message->sender; 和收件人用户 $user->reciepient;

为您的 pivot table 进行 new 迁移:

Schema::create('message_user', function (Blueprint $table) {
        $table->increments('id');
        $table->bigInteger('message_id')->unsigned();
        $table->integer('user_id')->unsigned();
        $table->timestamps();
});