BadMethodCallException:在 Laravel 8 中调用数据库播种器中的未定义方法
BadMethodCallException: Call to undefined method in database seeder in Laravel 8
我在执行命令时出错::
>php artisan migrate:refresh --seed
错误信息是:
BadMethodCallException
Call to undefined method App\Models\Diary::diarySubjob()
at C:\MAMP\htdocs\dominios\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:50
46▕ * @throws \BadMethodCallException
47▕ */
48▕ protected static function throwBadMethodCallException($method)
49▕ {
➜ 50▕ throw new BadMethodCallException(sprintf(
51▕ 'Call to undefined method %s::%s()', static::class, $method
52▕ ));
53▕ }
54▕ }
• Bad Method Call: Did you mean App\Models\Diary::subjobs() ?
1 C:\MAMP\htdocs\dominios\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:36
Illuminate\Database\Eloquent\Model::throwBadMethodCallException()
2 C:\MAMP\htdocs\dominios\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1993
Illuminate\Database\Eloquent\Model::forwardCallTo()
Class 日记子作业
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\Pivot;
class DiarySubjob extends Pivot
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'diary_id',
'work_id',
'subjob_id',
'company',
'customer',
'repair',
'duration',
'observations',
];
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;
/**
* Partidas de un diario.
*/
public function subjobs()
{
$this->belongsTo(Subjob::class);
}
/**
* Diarios de una partida.
*/
public function diaries()
{
$this->belongsTo(Diary::class);
}
/**
* Obras o clientes de una partida.
*/
public function works()
{
$this->belongsTo(Work::class);
}
}
Class日记本
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Diary extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'observations',
'duration',
'overtime'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'observations' => 'string',
];
/**
* Obtener el usuario/empleado de un diario.
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Obtener las partidas de un parte diario.
*/
public function subjobs()
{
return $this->belongsToMany(Subjob::class)->using(DiarySubjob::class);
}
}
Class 子工作
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Subjob extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'job_id',
'name'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'name' => 'string'
];
/**
* Obtener el trabajo/partida al que pertenece este subtrabajo/subpartida.
*/
public function job()
{
$this->hasMany(Job::class);
}
/**
* Partes de trabajo diarios de la partida.
*/
public function diaries()
{
$this->belogsToMany(Diary::class)->using(DiarySubjob::class);
}
}
Class 日记子作业工厂
<?php
namespace Database\Factories;
use App\Models\DiarySubjob;
use App\Models\Diary;
use App\Models\Subjob;
use App\Models\Work;
use Illuminate\Database\Eloquent\Factories\Factory;
class DiarySubjobFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = DiarySubjob::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'diary_id' => DiarySubjob::diaries()->all()->random()->id,
'work_id' => Work::all()->random()->id,
'subjob_id' => Subjob::all()->random()->id,
'company' => $this->faker->company(),
'repair' => $this->faker->boolean(),
'duration' => '02:00:00',
'overtime' => '00:00:00',
'observations' => $this->faker->realText(255, 2)
];
}
}
Class DatabaseSeeder
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
\App\Models\Work::factory(5)->create();
$this->call([
JobSeeder::class,
SubJobSeeder::class,
ConfigurationSeeder::class
]);
\App\Models\User::factory(20)
->has(\App\Models\Salary::factory(1))
->has(\App\Models\Diary::factory(10))
->create();
\App\Models\Diary::factory(1)
->has(\App\Models\DiarySubjob::factory(15))
->create();
}
}
这行执行时的错误:
\App\Models\Diary::factory(1)
->has(\App\Models\DiarySubjob::factory(15))
->create();
为了更简洁,我得到错误 运行 this:
->has(\App\Models\DiarySubjob::factory(15))
它告诉我我正在调用一个未定义的方法 App\Models\Diary::diarySubjob() 它建议我调用方法 App\Models\Diary::subjobs() 但我没有在任何地方调用方法 App\Models\Diary::diarySubjob()
出现此错误的原因是什么?我很迷茫。有什么建议吗?
你的问题是你没有阅读文档,因为它清楚地表明它与 Laravel 使用的关系的命名约定有关。
阅读文档的 this 部分。
因此,您的解决方案是将您知道出错的部分替换为:
\App\Models\Diary::factory(1)
->has(\App\Models\DiarySubjob::factory(15), 'subjobs')
->create();
这应该可以解决您的问题。
我在执行命令时出错::
>php artisan migrate:refresh --seed
错误信息是:
BadMethodCallException
Call to undefined method App\Models\Diary::diarySubjob()
at C:\MAMP\htdocs\dominios\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:50
46▕ * @throws \BadMethodCallException
47▕ */
48▕ protected static function throwBadMethodCallException($method)
49▕ {
➜ 50▕ throw new BadMethodCallException(sprintf(
51▕ 'Call to undefined method %s::%s()', static::class, $method
52▕ ));
53▕ }
54▕ }
• Bad Method Call: Did you mean App\Models\Diary::subjobs() ?
1 C:\MAMP\htdocs\dominios\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:36
Illuminate\Database\Eloquent\Model::throwBadMethodCallException()
2 C:\MAMP\htdocs\dominios\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1993
Illuminate\Database\Eloquent\Model::forwardCallTo()
Class 日记子作业
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\Pivot;
class DiarySubjob extends Pivot
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'diary_id',
'work_id',
'subjob_id',
'company',
'customer',
'repair',
'duration',
'observations',
];
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;
/**
* Partidas de un diario.
*/
public function subjobs()
{
$this->belongsTo(Subjob::class);
}
/**
* Diarios de una partida.
*/
public function diaries()
{
$this->belongsTo(Diary::class);
}
/**
* Obras o clientes de una partida.
*/
public function works()
{
$this->belongsTo(Work::class);
}
}
Class日记本
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Diary extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'observations',
'duration',
'overtime'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'observations' => 'string',
];
/**
* Obtener el usuario/empleado de un diario.
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Obtener las partidas de un parte diario.
*/
public function subjobs()
{
return $this->belongsToMany(Subjob::class)->using(DiarySubjob::class);
}
}
Class 子工作
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Subjob extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'job_id',
'name'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'name' => 'string'
];
/**
* Obtener el trabajo/partida al que pertenece este subtrabajo/subpartida.
*/
public function job()
{
$this->hasMany(Job::class);
}
/**
* Partes de trabajo diarios de la partida.
*/
public function diaries()
{
$this->belogsToMany(Diary::class)->using(DiarySubjob::class);
}
}
Class 日记子作业工厂
<?php
namespace Database\Factories;
use App\Models\DiarySubjob;
use App\Models\Diary;
use App\Models\Subjob;
use App\Models\Work;
use Illuminate\Database\Eloquent\Factories\Factory;
class DiarySubjobFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = DiarySubjob::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'diary_id' => DiarySubjob::diaries()->all()->random()->id,
'work_id' => Work::all()->random()->id,
'subjob_id' => Subjob::all()->random()->id,
'company' => $this->faker->company(),
'repair' => $this->faker->boolean(),
'duration' => '02:00:00',
'overtime' => '00:00:00',
'observations' => $this->faker->realText(255, 2)
];
}
}
Class DatabaseSeeder
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
\App\Models\Work::factory(5)->create();
$this->call([
JobSeeder::class,
SubJobSeeder::class,
ConfigurationSeeder::class
]);
\App\Models\User::factory(20)
->has(\App\Models\Salary::factory(1))
->has(\App\Models\Diary::factory(10))
->create();
\App\Models\Diary::factory(1)
->has(\App\Models\DiarySubjob::factory(15))
->create();
}
}
这行执行时的错误:
\App\Models\Diary::factory(1)
->has(\App\Models\DiarySubjob::factory(15))
->create();
为了更简洁,我得到错误 运行 this:
->has(\App\Models\DiarySubjob::factory(15))
它告诉我我正在调用一个未定义的方法 App\Models\Diary::diarySubjob() 它建议我调用方法 App\Models\Diary::subjobs() 但我没有在任何地方调用方法 App\Models\Diary::diarySubjob()
出现此错误的原因是什么?我很迷茫。有什么建议吗?
你的问题是你没有阅读文档,因为它清楚地表明它与 Laravel 使用的关系的命名约定有关。
阅读文档的 this 部分。
因此,您的解决方案是将您知道出错的部分替换为:
\App\Models\Diary::factory(1)
->has(\App\Models\DiarySubjob::factory(15), 'subjobs')
->create();
这应该可以解决您的问题。