Laravel关系多对多

时间:2018-03-14 10:42:15

标签: laravel laravel-5.5

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

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]:找不到基表或视图:1146表   'wymiana2.message_user'不存在(SQL:select users。*,   message_user.message_id as pivot_message_id,   message_user.user_id为来自用户内部联接的pivot_user_id   users.id = message_user.user_id在哪里   message_user.message_id = 4)(查看:   C:\ wymiana2 \资源\视图\消息\ 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');
}

我有错误:尝试获取非对象的属性“名称”(查看:C:\ wymiana2 \ resources \ views \ messages \ inbox.blade.php)

2 个答案:

答案 0 :(得分:0)

<强>更新

由于消息仅针对特定用户,因此多对多关系不再需要。保留数据库结构(不需要中间表),将以下关系添加到while ($row = mysql_fetch_assoc($result)) { $filelist = $row["file"]; echo "<input type=\"checkbox\" value=\"$filelist\" name=\"files[]\" checked=\"checked\"/>&nbsp;&nbsp;$filelist<br />"; } echo "<br>"; $dirname = "/var/www/vhosts/hub.gkrmaintenance.co.uk/public_html/forms/templates"; $forms = scandir($dirname); sort($forms); foreach ($forms as $file) { if(($file != ".") and ($file != "..") and ($file != "index.php") and ($file != "Site Induction Sheet.docx") and ($file != "Method Statement Complete.docx") and ($file != "Construction Phase Plan Complete.docx") and ($file != "Method Statement.docx") and ($file != "Construction Phase Plan.docx") and ($file != $filelist)) { echo "<input type=\"checkbox\" value=\"$file\" name=\"files[]\" />&nbsp;&nbsp;$file<br />"; } } 模型。

Message.php

Message

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

在您的控制器或视图中,您可以使用class User extends Model { public function messages() { return $this->hasMany('App\Message'); } }

访问用户的消息

使用$user->messages();向收件人用户$message->sender;

发送邮件的用户

答案 1 :(得分:0)

new表进行pivot迁移:

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