我正在用 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 所以这就是为什么我像你看到的那样做它。
慕盖茨4494581