我有一个函数需要执行发布并获取操作。
我正在创建一个报价系统。客户可以询问报价。此优惠称为 $pendingOffer
。工作人员可以接受报价,当它接受时,数据库行将传输到 $offer
表中。 pendingOffer 仍然在pendingOffer 表中。我想删除这个。我使用 POST 方法将挂起的报价存储到报价表中。我想使用 GET 方法删除pendingOffer。我该怎么做?
路线:
Route::post('/create_offer/{request}/{id}', 'RequestOfferController@saveCreateOffer');
我的 Controller :
public function saveCreateOffer(RequestOffer $request, RequestOffer $id) {
$data = Request::all();
$offer = new Offer($request->getAttributes());
$offer->save();
for ($i = 0; $i < count($data['products']); $i++) {
$productId = $data['products'][$i];
$productCount = $data['count'][$i];
$start = $data['date'][$i];
$period = $data['per_periode'][$i];
$btw = $data['btw'][$i];
$product = Product::find($productId);
if (!$product) {
$offer->delete();
return back()->withInput()->withErrors(['error' => 'Één van de producten kon niet gevonden worden.']);
}
$offerProduct = new OfferProduct();
$offerProduct->product()->associate($product);
$offerProduct->offer()->associate($offer);
$offerProduct->count = $productCount;
$offerProduct->start_date = Carbon::parse($start);
$offerProduct->btw = $btw;
$offerProduct->period = $period;
$offerProduct->save();
}
$pendingOffer = RequestOffer::find($id);
$pendingOffer->delete();
return redirect('/home');
}
我尝试使用 ::find($id)
方法,但这不起作用。我收到此错误:
MethodNotAllowedHttpException in RouteCollection.php line 251:
最佳答案
如果它们是不同的请求方法并执行不同的操作,为什么要将它们组合在一个函数/方法中?为每个操作创建新的路线和新的 Controller 方法。
您可以使用相同的帖子 URL 并进入您的路线文件。
例如:
Route::post('/create_offer/{request}/{id}', 'RequestOfferController@saveCreateOffer');
Route::get('/create_offer/{request}/{id}', 'RequestOfferController@deleteOffer');
不过,对我来说,在此 URL 上使用 GET 来删除待处理的报价没有任何意义。您可能应该对更合适的 URL 使用 DELETE 请求来删除优惠。
关于php - 一个函数中的 Post 和 Get 方法?拉拉维尔 5.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45013950/