Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在null上调用成员函数attach()时出错_Php_Mysql_Laravel_Relationship - Fatal编程技术网

Php 在null上调用成员函数attach()时出错

Php 在null上调用成员函数attach()时出错,php,mysql,laravel,relationship,Php,Mysql,Laravel,Relationship,我想创建标记系统,但我有一个错误 调用成员函数attach on null。看看我的代码 关系: Job.php Jobservices.php 我创建了透视表 Schema::create('job_jobservices', function (Blueprint $table) { $table->id(); $table->integer('job_id'); $table->integer('jobservices_id'); $tab

我想创建标记系统,但我有一个错误 调用成员函数attach on null。看看我的代码

关系:

Job.php

Jobservices.php

我创建了透视表

Schema::create('job_jobservices', function (Blueprint $table) {
    $table->id();
    $table->integer('job_id');
    $table->integer('jobservices_id');
    $table->timestamps();
});
控制器和视图

我试着将我的服务连接到控制器。看看这个

$job = Job::create([
           'title'          => $request->title,
           'description'     => $request->description,
           //...
        ]);
$job->services()->attach($request->services);
我认为,$request->services很好,因为如果我尝试dd$request->services,它会显示我 但以防万一,我会告诉你我的观点

<select class="js-example-responsive col-12" multiple="multiple" name="services[]">
    @foreach($services as $service)
        <option value={{ $service->id }}>{{ $service->name }}</option>
    @endforeach
</select>
    @error('services')
        <div class="alert alert-danger" role="alert">
        {{ $message}}
        </div>
    @enderror
我不知道为什么,但它显示了一个错误

调用成员函数attach on null时出错


你知道怎么回事吗?

你应该带着关系返回

公共职能服务 { 返回$this->belongsToMany'App\jobservices'; } 公共职能职位 { 返回$this->belongtomany'App\Job'; }
你应该带着关系回来

公共职能服务 { 返回$this->belongsToMany'App\jobservices'; } 公共职能职位 { 返回$this->belongtomany'App\Job'; }
$job = Job::create([
           'title'          => $request->title,
           'description'     => $request->description,
           //...
        ]);
$job->services()->attach($request->services);
<select class="js-example-responsive col-12" multiple="multiple" name="services[]">
    @foreach($services as $service)
        <option value={{ $service->id }}>{{ $service->name }}</option>
    @endforeach
</select>
    @error('services')
        <div class="alert alert-danger" role="alert">
        {{ $message}}
        </div>
    @enderror