Laravel为什么数据透视表是空的?

时间:2019-11-05 15:36:34

标签: php mysql sql database laravel

我在laravel eroquent中有一个简单的关系

这是创建投标人表格

public function up()
{
    Schema::create('bidders', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('params_name');
        $table->string('params_value');
        $table->string('bidder_name');
        $table->timestamps();
    });
}

这是bidder_parameter表创建

public function up()
{
    Schema::create('bidder_parameters', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('bidder_id');
        $table->foreign('bidder_id')->references('id')->on('bidders')->onDelete('cascade');
        $table->timestamps();
    });
}

这是投标人模型

class Bidder extends Model
{
    protected $table = 'bidders';

    protected $fillable = [
        "params_name",
        "params_value",
        "bidder_name"
    ];

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

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

这是BidderParameter模型

class BidderParameter extends Model
{
    public function parameters()
    {
        return $this->belongsTo('App\Bidder');
    }
}

这里是用于将数据插入数据库的参数控制器

public function store(Request $request){
        // dd($request);
        if($request->ajax())
        {
            $rules = array(
                'params_name.*'  => 'required',
                'params_value.*'  => 'required',
                'bidders_name.*' => 'required'
            );
            $error = Validator::make($request->all(), $rules);
            if($error->fails())
            {
                return response()->json([
                    'error'  => $error->errors()->all()
                ]);
            }
            $params_name = $request->params_name;
            $params_value =$request->params_value;
            $bidders_name =$request->bidders_name;
            for($count = 0; $count < count($params_name); $count++)
            {
                $data = array(
                    'params_name'   => $params_name[$count],
                    'params_value'  => $params_value[$count],
                    'bidders_name'  => $bidders_name[$count],
                );

                $insert_data[] = $data; 
                // dd($insert_data);

            }
            Bidders:insert($insert_data);
            return response()->json([
                'success'  => 'Data Added successfully.'
            ]);
        }
    }

现在,当我向数据库“ bidders table have data saved into it but bidder_parameter`提交数据时,是空的

我的代码有什么问题?

1 个答案:

答案 0 :(得分:1)

多对多关系包含3个表。主表有2个表,第3个表是这2个表的主键的组合。

例如

用户可以具有多个角色

角色可以属于许多用户

所以这需要多对多关系

所以我们需要这样的数据库模式:

  1. Users table -> (id, name ,...) e.g. Jonn Doe with id 1
  2. Roles table -> (id, name, ...) e.g. SUPER_ADMIN with 1
  3. role_user table (id, user_id, role_id) role_idroles table的外键,而user_idusers table的外键

现在在模型类中:

In  User Model

public function roles(){
   return $this->belongsToMany(Role::class,'role_user','user_id','role_id');
}

现在在Roles类中

public function users(){
   return $this->belongsToMany(User::class,'role_user','role_id','user_id');
   //note keys are in opposite order as in roles() method
}

现在,您可以从User实例和您需要的Role实例中调用函数

$user->roles // as collection
$user->roles() // as eloquent instance

$role->users // as collection
$role->users() // as eloquent instance

您可以阅读更多here