BadMethodCallException:方法不存在

时间:2019-10-08 08:12:04

标签: php laravel api

我试图在此配置文件和可用性表之间创建多对多关系,但在测试中,我不断调用测试中关于可用性的未定义方法。

这是控制器功能

var nunjucks  = require('nunjucks');
var env = nunjucks.configure();
env.addFilter('ws', val => !val || val == 0 ? val : ' ' + val);

var html = env.renderString(`
    Hi {{- firstname | ws -}}, your user ID ...
`, { firstname: 'Dave' });
console.log(html);

这是播客配置文件模型中的方法

 /**
 * Creates association between availability and podcast profile
 *
 * @param array $availabilities
 */
private function associateAvailability(array $availabilities)
{
    $this->podcastProfile->availability()->sync(
        array_map(function ($availability) {
            $availabilityModel = Availability::where('availability', '=', $availability)->first();
            return $availabilityModel->id;
        }, $availabilities)
    );
}

这是对该方法的测试

  /**
 * Defines many-to-many relationship between podcasts and availabilities
 */
public function availability(): BelongsToMany
{
    return $this->belongsToMany(
        'App\Models\Availability',
        'podcast_availability',
        'podcast_profile_id',
        'availability_id'
    );
}

这是插入测试的支票可用性方法

/**
 * @test
 */
public function it_should_create_availability_relationship()
{
    $this->handlePostRequestToController();
    $this->assertTrue($this->user->podcastProfile->availability()->exists());
    $this->checkAvailability($this->requestData['availability']);
}

这是错误

/**
 * Check database
 *
 * @param $availabilities
 */
private function checkAvailability($availabilities): void
{
    foreach ($availabilities as $availability) {
        $availabilityModel = Availability::where('availability', '=', $availability)
            ->first();

        $this->assertDatabaseHas('podcast_availability', [
            'podcast_profile_id' => $this->user->podcastProfile->id,
            'availability_id' => $availabilityModel->id
        ]);
    }
}

BadMethodCallException:方法Illuminate \ Database \ Eloquent \ Collection :: availability不存在。

1 个答案:

答案 0 :(得分:0)

如果您尝试基于Laravel Many to Many Relationship.建立Many to Many关系

这是您的操作方式。您需要2 models和3 migrations

第一

您的模型应如下所示:

个人资料模型

protected $guarded = [];

public function availabilities() {
  return $this->belongsToMany(Availability::class);
} 
  

注意:我使用availabilities是因为它在many to many relationship中,所以它是一个更好的命名约定。

可用性模型

protected $guarded = [];

public function profiles() {
  return $this->belongsToMany(Profile::class);
}

第二

您的迁移应该是这样的:

个人资料迁移

Schema::create('profiles', function (Blueprint $table) {
  $table->bigIncrements('id');
  ...
  $table->timestamps();
});

可用性迁移

Schema::create('availabilities', function (Blueprint $table) {
  $table->bigIncrements('id');
  ...
  $table->timestamps();
});

可用性和配置文件迁移

Schema::create('availability_profile', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->unsignedBigInteger('availability_id');
  $table->unsignedBigInteger('profile_id');
  $table->timestamps();
});
  

注意:我按字母顺序使用availability_profile命名约定

信息

您可以使用artisan command这样的php artisan make:migration create_availability_profile_table --create=availability_profile来生成此迁移

最后

在您的控制器中,您可以将profile分配给availability

控制器

假设您已在数据库中进行记录。

public function generateAvailability() {
  $profile = Profile::firstOrFail(1);
  $role = Role::firstOrFail(1);

  $profile->availabilities()->attach($role->id);

  dd(profile->availabilities);
}
  

注意:我使用dd(转储并死亡)来检查记录

您还可以看到此referencethis