dsvjw20866 2018-06-09 12:20
浏览 187
已采纳

Eloquent多对多attach()传递空模型ID

I am developing an application using Laravel and Eloquent ORM, it uses a database filled with event information.

I have successfully implemented attach() in the relevant controllers for both my user and role models.

My Event model can have many Links. A Link can have Many events. My problem is that the attach() is not supplying the ID of the object it is being called on, instead it supplies null and I receive the following error message:

SQLSTATE[23000]: Integrity constraint violation:

1048 Column 'link_id' cannot be null (SQL: insert into 'event_link' ('created_at', 'event_id', 'link_id', 'updated_at') values (2018-06-09 11:27:15, 2, , 2018-06-09 11:27:15))

I've triple checked my models and database structure.

I can't even imagine how this error could occur since the id lacking in the SQL query is the id of the object that the attach() method is actually being called on. If I use sync($eventID, false) instead of attach(), the result is the same.

Event table:

Event table

Link table:

Link table

Event_Link table:

Event_Link table

The following is the problematic method in the controller responsible for storing the record and creating an entry in the event_link weak entity.

The $link object is created successfully if the attach() line is commented out, a JSON representation of a link is returned which confirms this (but it lacks the 'id' field).

    public function store(StoreLink $request) {
      $link = Link::create([
        'title' => $request->title,
        'url' => $request->url,
      ]);

      if ($request['eventId']) {
        // $request->eventId is passed successfully, $link id is not passed.
        $link->events()->attach($request->eventId);
      }
      return response()->json($link, 201);
    }

Link Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Link extends Model
{
  protected $table = 'links';
  public $incrementing = false;
  public $timestamps = true;
  protected $primaryKey = "id";
  protected $fillable = ['title', 'url'];

  public function events()
  {
    return $this->belongsToMany('App\Event')->withTimestamps();
  }
}

Event Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Event extends Model
{
  protected $table = 'events';
  public $incrementing = false;
  public $timestamps = true;
  protected $primaryKey = "id";
  protected $fillable = ['description', 'date', 'image', 'category_id'];

  public function category()
  {
      return $this->belongsTo('App\Event', 'category_id');
  }

  public function links()
  {
    return $this->belongsToMany('App\Link', 'event_link')->withTimestamps();
  }

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

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

}
  • 写回答

1条回答 默认 最新

  • duanji1056 2018-06-09 13:32
    关注

    The problem is public $incrementing = false;: Remove the line from both your models.

    Your id columns are defined as AUTO_INCREMENT, so $incrementing has to be true.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么