【发布时间】:2021-10-11 05:28:02
【问题描述】:
执行命令时出错::
>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()
课堂日记子作业
<?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);
}
}
课堂日记
<?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);
}
}
类子作业
<?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);
}
}
类 DiarySubjobFactory
<?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)
];
}
}
类 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();
为了更简洁,我得到了运行这个错误:
->has(\App\Models\DiarySubjob::factory(15))
它告诉我我正在调用一个未定义的方法App\Models\Diary::diarySubjob(),它建议我调用该方法App\Models\Diary: :subjobs() 但是我没有在任何地方调用方法 App\Models\Diary::diarySubjob()
这个错误可能是什么原因造成的?我很失落。有什么建议吗?
【问题讨论】:
标签: php laravel eloquent laravel-8 factory