php - Laravel - Eloquent attach() 不适用于模型

标签 php laravel-5.4 eloquent

长话短说


尝试使用 eloquent for a rest api 让三个模型进行交互。

  • 用户 - belongsToMany(pulls)
  • 拉 - belongsToMany(user) && belongsToMany(boxes)
  • Box - belongsToMany(拉动)

pull_user 表运行良好,我可以在保存拉取后附加一个用户。保存一个框工作正常,但附加不起作用/在数据透视表中输入任何内容(尽管我没有收到任何错误)。

问题


保存后,我无法获得将我的两个模型关联到 attach() 的数据透视表。我有上面列出的三个模型,即使 save for box 工作正常,pivot 对 pull_user 但对 pull_box 不起作用。我能够在没有错误的情况下保存一个框,但是关联永远不会发生(没有错误)。

代码


拉箱.php

class PullBox extends Migration
{

  public function up()
  {
    Schema::create('pull_box', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->integer('pull_id');
        $table->integer('box_id');

    });
  }

  public function down()
  {
    Schema::dropIfExists('pull_box');
  }
}

拉.php

class Pull extends Model
{
    protected $fillable = ['from', 'to', 'runit_id', 'start_time', 'end_time', 'box_count', 'pull_status', 'audit_status', 'status', 'total_quantity', 'accuracy'];

    public function users(){
        return $this->belongsToMany('App\User');
    }
    public function boxes(){
        return $this->belongsToMany('App\Box');
    }
}

盒子.php

class Box extends Model
{
    protected $fillable = ['user_id','from', 'to', 'runit_id', 'start_time', 'end_time', 'pull_id', 'total_quantity', 'status', 'accuracy'];

    public function pulls(){
        return $this->belongsToMany('App\Pull');
    }
}

BoxController.php

public function store(Request $request)
{
    $this->validate($request, [
        'user_id' => 'required|integer',
        ...
    ]);

    $user_id = $request->input('user_id');
    ...

    $box = new Box([
        'user_id' => $user_id,
        ...
    ]);

    $pull = Pull::whereId($pull_id)->first();

    if($box->save()){
        $pull->boxes()->attach($box->id);
        $box->view_box = [
            'href' => 'api/v1/box/' . $box->id,
            'method' => 'GET'
        ];
        $message = [
            'msg' => 'Box created',
            'box' => $box,
            'pull' => $pull_id
        ];

        return response()->json($message, 201);
    }

    $response = [
        'msg' => 'Box creation error, contact supervisor',
    ];
    return response()->json($response, 404);
}

解决方案


我需要知道如何让这个协会发挥作用。我将需要在 Item 的拉动下添加一个新层,但我不想在解决此问题之前移动一个层。我认为我的问题必须源于我的语法/逻辑错误,但我看不到。 SO 上有一堆问题非常接近给我一个解决方案,但在阅读它们之后我无法解决我的问题。

感谢任何帮助。

最佳答案

尝试将您的 pull_box 表重命名为 box_pull,laravel 上的数据透视表必须按字母顺序排列。如果你想在数据透视表上使用自定义名称,你必须扩展你的数据透视表,例如:

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class PullBox extends Pivot
{
    protected $table = 'pull_box';
}

还有你的多对多关系:

class Pull extends Model
{
    protected $fillable = ['from', 'to', 'runit_id', 'start_time', 'end_time', 'box_count', 'pull_status', 'audit_status', 'status', 'total_quantity', 'accuracy'];

    public function users(){
        return $this->belongsToMany('App\User');
    }
    public function boxes(){
        return $this->belongsToMany('App\Box')->using('App\PullBox');
    }
}

class Box extends Model
{
    protected $fillable = ['user_id','from', 'to', 'runit_id', 'start_time', 'end_time', 'pull_id', 'total_quantity', 'status', 'accuracy'];

    public function pulls(){
        return $this->belongsToMany('App\Pull')->using('App\PullBox');
    }
}

关于php - Laravel - Eloquent attach() 不适用于模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45743840/

相关文章:

php - Laravel 连接表不返回任何数据

php - Laravel/Eloquent 建议覆盖特征属性?

php - Laravel Eloquent 的 API 适配器?

php - Laravel - 让用户在创建后插入类别并创建新类别(如果不存在)

php - 将单换行符替换为双换行符

javascript - php/jQuery - 表单验证,获取动态添加的输入值

php - 拒绝应用来自 ... 的样式,因为它的 MIME 类型 ('text/html' ) 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查

php - $this->post codeigniter 不适用于rest api

php - 如何在 laravel 调度程序中访问 Controller 中的方法?

laravel - 如何重构实现相同方法的两个模型类