duanhuancong1969 2017-03-27 10:56
浏览 61
已采纳

Laravel 5.4关系不起作用不能附加第二个参数

I've got some problems making relationship between my user who is logged in and events that he wants to join work. I am really taking many steps to do something similar to what I do with role users. But here I have some problems, because my user uses model User, events uses model HomeModel and 3 other models to make a relation and connecte to table SaveEvent. There is my code. I hope someone can tell me how to figure it out because I wasted 2 days to resolve my problem:

Model HomeModel

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class HomeModel extends Model 
{
    protected $table = 'events'; // you may change this to your name table
    public $timestamps = true; // set true if you are using created_at and updated_at
    protected $primaryKey = 'id'; // the default is id
     
      /**
     * Is it an all day event?
     *
     * @return bool
     */
    public function isAllDay()
    {
        return (bool)$this->day;
    } 
    
    
}

Model User

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{ 
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'lastname', 'name', 'phonenumber', 'email', 'password', 'user_id' ,
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    
         public function roles()
    {
        return $this->belongsToMany('App\Role', 'user_role', 'user_id', 'role_id');
    }

    public function hasAnyRole($roles)
    {
        if (is_array($roles)) {
            foreach ($roles as $role) {
                if ($this->hasRole($role)) {
                    return true;
                }
            }
        } else {
            if ($this->hasRole($roles)) {
                return true;
            }
        }
        return false;
    }
    
    public function hasRole($role)
    {
        if ($this->roles()->where('name', $role)->first()) {
            return true;
        }
        return false;
    }
    public function events()
    {
        return $this->belongsToMany('App\SaveEvent')->withTimestamps();
    }
}    

Model SaveEvent

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class SaveEvent extends Model
{
    
    public function users()
    {
       
        return $this->belongsToMany('App\User');
    }  
}

Table to relationship

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
| users_id   | int(10) unsigned | NO   | MUL | NULL    |                |
| events_id  | int(10) unsigned | NO   | MUL | NULL    |                |
+------------+------------------+------+-----+---------+----------------+

Controller ZapisController

    public function acceptEvent($id)
      {
 
        $events_id = HomeModel::find($id);
        $users_id = new SaveEvent;
        $users_id->users_id=Auth::id();
        $users_id->save();
        $users_id->events()->attach($events_id);

        return redirect()->back();
      }  

</div>
  • 写回答

1条回答 默认 最新

  • duanjing9739 2017-03-27 11:11
    关注

    You don't need a model to save events. This is a many to many relationship with so use a pivot table.

    Change event method in User model like this :

     public function events()
        {
            return $this->belongsToMany(HomeModel::class,'PIVOT_TABLE_NAME','user_id','events_id');->withTimestamps();
        }
    

    and in HomeModel :

     public function users()
        {
            return $this->belongsToMany(User::class,'PIVOT_TABLE_NAME','events_id','user_id');
        }
    

    now you can use $user->events->attach($events_id);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料