Php 对成员函数Laravel 5.5的多对多插入错误调用

Php 对成员函数Laravel 5.5的多对多插入错误调用,php,laravel,laravel-5.5,Php,Laravel,Laravel 5.5,我在多对多插入中遇到问题。控制器中的代码是正确的。在成功添加两个数据后,第三次尝试插入时产生错误: 在null上调用成员函数bagcollects() 错误就在这个代码上 $collection->bagcollects()->attach($bagcollect->id); 我只是不明白为什么会发生这个错误 我将显示CollectionController存储方法的全部代码: public function addbag(Request $request){ $co

我在多对多插入中遇到问题。控制器中的代码是正确的。在成功添加两个数据后,第三次尝试插入时产生错误:

在null上调用成员函数bagcollects()

错误就在这个代码上

$collection->bagcollects()->attach($bagcollect->id);
我只是不明白为什么会发生这个错误

我将显示CollectionController存储方法的全部代码:

public function addbag(Request $request){
    $collection = Collection::find($request->input('collection_id'));
    $bagcollect = Bagcollect::create([
        'bag_id' => $request->input('bag_id'),
        'weight' => $request->input('weight')
    ]);
    $collection->bagcollects()->attach($bagcollect->id);
    return redirect()->route('collections.show', ['collection'=> $collection->id]);
}
基于集合的迁移

Schema::disableForeignKeyConstraints();
if(!Schema::hasTable('collections')){
    Schema::create('collections', function (Blueprint $table) {
        $table->engine = "InnoDB";
        $table->increments('id');
        $table->integer('assignment_id')->unsigned();
        $table->foreign('assignment_id')->references('id')->on('assignments')->onUpdate('cascade')->onDelete('cascade');
        $table->timestamp('collected_on');
    });
}
巴格拉岛上的迁移

Schema::create('bagcollects', function (Blueprint $table) {
    $table->engine = "InnoDB";
    $table->increments('id');
    $table->integer('bag_id')->unsigned();
    $table->double('weight', 8, 2);
    $table->foreign('bag_id')->references('id')->on('bags');
    $table->timestamps();
});
迁移bagcollect\u集合

Schema::create('bagcollect_collection', function (Blueprint $table) {
    $table->engine = "InnoDB";
    $table->increments('id');
    $table->integer('bagcollect_id')->unsigned();
    $table->integer('collection_id')->unsigned();
    $table->foreign('bagcollect_id')->references('id')->on('bagcollects');
    $table->foreign('collection_id')->references('id')->on('collections');
    $table->timestamps();
});
集合show.blade.php添加模式

<!-- ADD MODAL -->
<div class="modal fade" role="dialog" id="addModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">New Collection</h4>
            </div>

            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12"> 
                        <form class="form-horizontal" method="POST" action="{{ route('collections.addbag') }}">
                            {{ csrf_field() }}


                            <div class="row form-group">  

                                <input class="form-control" name = "collection_id" id="collection_id" value="{{$collection->id}}" type="hidden">
                                 <div class="{{ $errors->has('bag') ? ' has-error' : '' }}">
                                        <div class="col-md-8">
                                            <label for="bag_id">Bag</label>
                                             <select class="form-control" required id="bag" name="bag_id">
                                                 <option value="" data-hidden="true"  
                                                    selected="selected">
                                                </option>
                                                    @foreach($bags as $bag)
                                                        <option value= "{{ $bag->id }}">
                                                              {{ $bag->name }} 
                                                        </option>
                                                    @endforeach
                                              </select>
                                        </div>
                                    </div>

                                  <div class="{{ $errors->has('weight') ? ' has-error' : '' }}">
                                        <div class="col-md-8">
                                            <label for="weight">Weight</label>
                                             <input type="text" class="form-control" id="weight" name= "weight" required>
                                        </div>
                                  </div>

                            </div>   

                            <!-- SUBMIT BUTTON -->
                            <button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
                                Create
                            </button>

                            <button  data-dismiss="modal" aria-hidden="true" class="btn btn-basic pull-right" style="margin-right: 2%">
                                Cancel
                            </button>             
                            <div class="clearfix"></div>

                        </form>                
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
Bagcollect.php

protected $fillable = [
    'bag_id',
    'weight'
];

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

public function bag()
{
    return $this->belongsTo('App\Bag');
}
bagcollection.php

protected $table = "bagcollect_collection";

protected $fillable = [
    'bagcollect_id',
    'collection_id',

];
调用成员函数。。。在
null上

表示您正试图调用
null
对象上的函数。查看您的代码:

$collection = Collection::find($request->input('collection_id'));
根据当前逻辑,
$collection
可能是
null
,因此调用

$collection->bagcollects() ...
将抛出此错误

调试
Collection::find(…)
的结果,并确保
$Collection
不是
null

调用成员函数。。。在
null上

表示您正试图调用
null
对象上的函数。查看您的代码:

$collection = Collection::find($request->input('collection_id'));
根据当前逻辑,
$collection
可能是
null
,因此调用

$collection->bagcollects() ...
将抛出此错误


调试
Collection::find(…)
的结果,并确保
$Collection
不是
null

您可以共享关系模型吗?
Collection
已经是Laravel中的一个类;您的
集合
模型与
集合
助手()可能会遇到问题。考虑改变您的表/型号名称。@ TimLewis可能使用它自己的命名空间,不应该是一个问题。@ HasAtNang.Nr是的,可能是,但是没有看到<代码>使用< /Cord>语句,很难说。更改了我在评论中的措辞,使其更像是一个警告。您可以使用dd()方法转储
$collection
var吗?您可以共享关系模型吗?
collection
已经是Laravel中的一个类;您的
集合
模型与
集合
助手()可能会遇到问题。考虑改变您的表/型号名称。@ TimLewis可能使用它自己的命名空间,不应该是一个问题。@ HasAtNang.Nr是的,可能是,但是没有看到<代码>使用< /Cord>语句,很难说。更改了我在注释中的措辞,使其更像是一个警告。您可以使用dd()方法转储
$collection
var吗?我尝试在控制器代码中使用dd。当我输入数据时,collection_id从show.blade.php传递,但是在第二次尝试时,collection_id变为null。我不知道,因为我认为应该传递它,知道它在节目中,并且集合id存在于该视图中。我尝试在控制器代码中使用dd。当我输入数据时,collection_id从show.blade.php传递,但是在第二次尝试时,collection_id变为null。我不知道,因为我认为应该传递它,知道它在显示中,并且集合id存在于该视图中