我正在用 laravel 项目训练我的作业,这是我的问题:我有一个数据库,其中包含Event名称和与该数据库的一系列关系,其中两个是events_news,event_gallery哪个新闻更新工作正常,购买画廊就行不通,我用同样的方式处理新闻,我试过邮递员,我只得到更新的响应,但没有任何改变。这是我的事件模型:protected static function boot(){ parent::boot(); static::creating(function ($event){ $event->slug = str_slug($event->name); });}public function getRouteKeyName(){ return 'slug';}protected $with = ['galleries','news'];public function news(){ return $this->hasMany(EventNews::class);}public function galleries(){ return $this->hasMany(EventGallery::class);}public function getPathAttribute(){ return "/admin/events/$this->slug";}这是我的画廊和新闻模型:我之前都使用了guarded,但我已经在画廊中添加了可填充的,看看它是否有效。class EventGallery extends Model{ protected $fillable = ['event_id', 'url', 'body', 'flag']; public function event() { return $this->belongsTo(Event::class); }} class EventNews extends Model { protected $guarded = []; public function event() { return $this->belongsTo(Event::class); }}这是我的控制器:public function update(Request $request, Event $event, EventGallery $eventGallery){ /I've tried this and got same result $eventGallery->update($request->all()); /I've tried this and still got the same result $eventGallery->update( [ 'url' => $request->url, 'body' => $request->body, 'flag' => $request->flag, ] ); return response('Updated', Response::HTTP_ACCEPTED);}应用程序接口:Route::apiResource('/admin/events/{event}/gallery', 'Events\EventsPictureController');我将使用数据库中的真实数据进行测试 http://127.0.0.1:8000/api/admin/events/international-2019/gallery/2它说已更新,但没有任何改变。请帮我找到我的问题。我检查了事件并正确导入而不是 Facade/Event。这是带有 vue.js 的 SPA 应用程序。和讲师在控制器中注入了 Event 所以这就是为什么我像你看到的那样做它。
1 回答
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
在挖掘我的问题之后,我刚刚改变了控制器 public function update(Request $request, Event $event, EventGallery $eventGallery)
到 public function update(Request $request, Event $event, EventGallery $gallery)
仍在寻找发生这种情况的原因。也许是因为我在我的事件模型中调用了画廊
正如你在顶部看到的那样,我输入了画廊,但由于我正在尝试任何东西,我将函数模型更改为画廊,但它没有帮助,但是当我更改 $eventGallery 时,它起作用了。
- 1 回答
- 0 关注
- 162 浏览
添加回答
举报
0/150
提交
取消