模型关系在控制器中工作正常,API测试很高兴,但是当我尝试广播它时,我得到一个RelationNotFoundException。
我这里有两个模型,Message和ExtraMessage,一个控制器,MessageController和一个事件MessageSent。他们之间存在一对一的关系。
除了我用Event :: fake()模拟事件的事实以外,Postman可以很好地返回结果,并且自动化测试也可以正常工作。
一旦它实际被使用它的人击中了测试服务器,MessageSent专门的事件根本不会触发并检查日志,我得到了令人反感的消息:
local.ERROR: Call to undefined relationship [extra_message] on model [App\Message]. {
"exception":
"[object] (Illuminate\\Database\\Eloquent\\RelationNotFoundException(code: 0):
Call to undefined relationship [extra_message] on model [App\\Message].
at /path/to/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/RelationNotFoundException.php:34)
下面的原始代码。我尝试在MessageSent事件中添加use App\ExtraMessage;
,并尝试在事件的构造函数中键入提示消息,但结果相同。
消息
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Message extends Model
{
public $table='messages'
public function extra_message()
{
return $this->hasOne('App\ExtraMessage');
}
public function table1()
{
return $this->belongsTo(Table1::class);
}
public function table2()
{
return $this->belongsTo('App\Table2','foreign_key');
}
}
ExtraMessage
use Illuminate\Database\Eloquent\Model;
class ExtraMessage extends Model
{
public $table='extra_message';
protected $fillable = ['column1','column2','message_id'];
public function extra_message(){
return $this->belongsTo('App\Message');
}
}
控制器
use App\Message;
use App\ExtraMessage;
use App\Events\MessageSent;
use Illuminate\Http\Request;
use App\Notifications\ReceivedMessage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Arr;
use App\Jobs\PushNotificationQueue;
class MessageController extends Controller
{
public function sendMessage{
//snip
$message = Message::create(['my_params'=> $values]);
$message->extra_message()->create(['my_params1'=> $values1]);
$message_details = $message->load('table1')->load(['table2' => 'closure'])->load('extra_message');
//snip
broadcast(new MyEvent($message_details))->toOthers();
//snip
return response()->json(['param1' => $data, 'message' => $message_details])
}
}
事件
use App\Message;
use App\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class MessageSent implements ShouldBroadcast{
use Dispatchable, InteractsWithSockets, SerializeModels;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn()
{
return new Channel('channel');
}
}
堆栈跟踪
[2019-11-12 17:01:41] local.ERROR: Call to undefined relationship [extra_message] on model [App\message]. {"exception":"[object] (Illuminate\\Database\\Eloquent\\RelationNotFoundException(code: 0): Call to undefined relationship [extra_message] on model [App\\message]. at path/to/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/RelationNotFoundException.php:34)
[stacktrace]
#0 path/to/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(588): Illuminate\\Database\\Eloquent\\RelationNotFoundException::make(Object(App\\message), 'extra_message')
#1 path/to/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(90): Illuminate\\Database\\Eloquent\\Builder->Illuminate\\Database\\Eloquent\\{closure}()
#2 path/to/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(590): Illuminate\\Database\\Eloquent\\Relations\\Relation::noConstraints(Object(Closure))
#3 path/to/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(558): Illuminate\\Database\\Eloquent\\Builder->getRelation('extra_message')
#4 path/to/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(538): Illuminate\\Database\\Eloquent\\Builder->eagerLoadRelation(Array, 'extra_message', Object(Closure))
#5 path/to/project/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(491): Illuminate\\Database\\Eloquent\\Builder->eagerLoadRelations(Array)
#6 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php(102): Illuminate\\Database\\Eloquent\\Model->load(Array)
#7 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php(57): App\\Events\\MessageSent->restoreModel(Object(Illuminate\\Contracts\\Database\\ModelIdentifier))
#8 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/SerializesModels.php(45): App\\Events\\MessageSent->getRestoredPropertyValue(Object(Illuminate\\Contracts\\Database\\ModelIdentifier))
#9 [internal function]: App\\Events\\MessageSent->__wakeup()
#10 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(171): unserialize('O:38:\"Illuminat...')
#11 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(203): Illuminate\\Queue\\CallQueuedHandler->failed(Array, Object(Illuminate\\Database\\Eloquent\\RelationNotFoundException))
#12 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(182): Illuminate\\Queue\\Jobs\\Job->failed(Object(Illuminate\\Database\\Eloquent\\RelationNotFoundException))
#13 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(462): Illuminate\\Queue\\Jobs\\Job->fail(Object(Illuminate\\Database\\Eloquent\\RelationNotFoundException))
#14 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(449): Illuminate\\Queue\\Worker->failJob(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Object(Illuminate\\Database\\Eloquent\\RelationNotFoundException))
#15 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(379): Illuminate\\Queue\\Worker->markJobAsFailedIfWillExceedMaxAttempts('database', Object(Illuminate\\Queue\\Jobs\\DatabaseJob), 1, Object(Illuminate\\Database\\Eloquent\\RelationNotFoundException))
#16 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(352): Illuminate\\Queue\\Worker->handleJobException('database', Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Object(Illuminate\\Queue\\WorkerOptions), Object(Illuminate\\Database\\Eloquent\\RelationNotFoundException))
#17 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(294): Illuminate\\Queue\\Worker->process('database', Object(Illuminate\\Queue\\Jobs\\DatabaseJob), Object(Illuminate\\Queue\\WorkerOptions))
#18 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(129): Illuminate\\Queue\\Worker->runJob(Object(Illuminate\\Queue\\Jobs\\DatabaseJob), 'database', Object(Illuminate\\Queue\\WorkerOptions))
#19 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(112): Illuminate\\Queue\\Worker->daemon('database', 'default', Object(Illuminate\\Queue\\WorkerOptions))
#20 path/to/project/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(96): Illuminate\\Queue\\Console\\WorkCommand->runWorker('database', 'default')
#21 [internal function]: Illuminate\\Queue\\Console\\WorkCommand->handle()
#22 path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(32): call_user_func_array(Array, Array)
#23 path/to/project/vendor/laravel/framework/src/Illuminate/Support/helpers.php(520): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#24 path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(90): value(Object(Closure))
#25 path/to/project/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(34): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#26 path/to/project/vendor/laravel/framework/src/Illuminate/Container/Container.php(591): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#27 path/to/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(202): Illuminate\\Container\\Container->call(Array)
#28 path/to/project/vendor/symfony/console/Command/Command.php(255): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#29 path/to/project/vendor/laravel/framework/src/Illuminate/Console/Command.php(189): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#30 path/to/project/vendor/symfony/console/Application.php(934): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#31 path/to/project/vendor/symfony/console/Application.php(273): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Queue\\Console\\WorkCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#32 path/to/project/vendor/symfony/console/Application.php(149): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#33 path/to/project/vendor/laravel/framework/src/Illuminate/Console/Application.php(90): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#34 path/to/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(131): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#35 path/to/project/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#36 {main}
"}
我注意到的唯一真正的不同是其他关系正在使用belongsTo,这对于它们所使用的场景来说是很好的。我不确定一旦击中broadcast()后如何破坏该关系,因为与此相关的所有其他事物都在起作用如预期的那样。
答案 0 :(得分:0)
Laravel和PHP的标准是camelCase,因此,如果您这样重命名关系,它应该可以工作。
public function extraMessage()
{
return $this->hasOne('App\ExtraMessage');
}