如何通过多对多关系定义来解决问题

时间:2017-12-06 11:46:11

标签: php laravel laravel-5

我有这样的问题我今天问了另外一个与此代码相关的问题。我解决了这个问题,但现在它给出了另一个错误。

public function addSubject(Request $request){
        $student=User::find($request->invisible);
        $classsubjects = $request['classsubjects'];
        $student->classSubjects()->attach($classsubjects);
        return redirect()->route('users.index')
            ->with('flash_message',
             'User successfully added.');
}

这里我给出了ClassSubject模型和用户模型定义的定义。

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use Notifiable;
    use HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','language'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function setPasswordAttribute($password)
    {   
        $this->attributes['password'] = bcrypt($password);
    }

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

    public function classSubjects()
    {
        return $this->belongsToMany('App\ClassSubject');

    }


}

这是我的classSubject模型。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ClassSubject extends Model
{
    protected $fillable = [
         'class_room_id','subject_id','teacher_id'
    ];

     public function classRoom()
    {
        return $this->belongsTo('App\ClassRoom','class_room_id','id');
    }

     public function subject()
    {
        return $this->belongsTo('App\Subject','subject_id','id');
    }

     public function teacher()
    {
        return $this->belongsTo('App\User','teacher_id','id');
    }


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

    }






}

这是我得到的错误。

Illuminate \ Database \ QueryException (42S02)
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'lcurve.class_subject_user' doesn't exist (SQL: insert into `class_subject_user` (`class_subject_id`, `user_id`) values (3, 4), (1, 4))

我是否需要创建一个类似错误消息的表。

1 个答案:

答案 0 :(得分:0)

您需要创建一个数据透视表来处理此问题。例如:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ClassSubjectUser extends Model
{
    protected $fillable = [
         'class_subject_id','user_id'
    ];

    protected $table = 'class_subject_user';
}

然后,在定义关系时,您应该设置表的名称和键。

在用户模型中:

public function classSubjects()
{
    return $this->belongsToMany('App\ClassSubject', 'class_subject_user', 'class_subject_id', 'user_id');
}

在ClassSubject模型中:

public function students()
{
    return $this->belongsToMany('App\User', 'class_subject_user', 'user_id', 'class_subject_id');
}

这将正确设置belongsToMany关系。

参考:https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-relationships