gpt4 book ai didi

Laravel - 重定向到路由 POST

转载 作者:行者123 更新时间:2023-12-02 17:57:46 24 4
gpt4 key购买 nike

如果验证器失败,我尝试重定向我的 POSTform,但它不起作用,因为它不是 GET 路由。我有一个用于选择产品的第一页,发布到第二页以向产品添加一个模型。如果验证器失败,我想重定向到第二页。

验证:

if ($validator->fails()) {
return redirect('admin/modele/create')
->withErrors($validator)
->withInput();
}

路线:

Route::get('modele/select/product', 'ModeleController@selectProduct')->name('modele.selectProduct'); //get page for select product
Route::post('modele/selected/product', 'ModeleController@selectedProduct')->name('modele.selectedProduct'); //the product is selected, send it to form page
Route::post('modele/create/product/{produit}', 'ModeleController@storea')->name('modele.storea'); //is a store custom

Controller :

public function selectProduct() 
{
//
$lesProduits = Produit::pluck('nom', 'id');
return view('admin/modele/select', compact('lesProduits'));

}

public function selectedProduct(Request $request)
{
//

$unProduit = $request->get('produit');
$id = ($request->get('produit'));

$unProduit = Produit::where('id', $id)->pluck('nom', 'id');
$uneCategorie = Produit::find($id)->categorie;
$produit = Produit::find($id);
//dd($uneCategorie) ;
//$categorie = $unProduit->categorie->id;

return view('admin/modele/create', compact('unProduit', 'uneCategorie', 'produit'));

}
public function storea(Request $request, $produit_id)
{
//
$validator = Validator::make($request->all(), [
'ref' => 'required|max:255',
'prixHT' => 'required|max:9|numeric',
'prixHTpromo' => 'max:9|numeric',
'quantite' => 'required|numeric|max:255',
't1' => 'max:255',
't2' => 'max:255',
't3' => 'max:255',
't4' => 'max:255',
't5' => 'max:255',
't6' => 'max:255',
't7' => 'max:255',
't8' => 'max:255',
't9' => 'max:255',
't10' => 'max:255',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',

]);

if ($validator->fails()) {
return redirect('admin/modele/create')
->withErrors($validator)
->withInput();
}
else
{
$unModele = New Modele();
$unModele->produit_id = $produit_id;
$unModele->ref = $request->get('ref');
$unModele->prixHT = $request->get('prixHT');
if ($request->get('prixHTpromo') != null)
{
$unModele->prixHPromoHT = $request->get('prixHTpromo');
}
$unModele->quantiteDebut = $request->get('quantite');
for ($i = 1; $i < 11; $i++)
{
$name = 't'.$i;
$nameDB = 'info'.$i;
if ($request->get($name) != null)
{
$unModele->$nameDB = $request->get($name);
}
}
// photo 0,1
if($request->file('image') != null) // && $uneActualite->url != null)
{
//image code here
}
else
{
$unModele->image=null;
}
/////////////////////////
$unModele->save();
$request->session()->flash("success","Modèle ajouté.");
return redirect(route('modele.index'));
}


}

选择页面:

     {!! Form::open(array('route' => 'modele.selectedProduct')) !!}


<div class="form-group">
{!! Form::label('produit', 'Produit') !!}
{!! Form::select('produit',$lesProduits, null, array('class' => 'form-control')) !!}
</div>
@if ($errors->has('produit'))
<div class="alert alert-danger" role="alert">
<ul>
@foreach ($errors->get('produit') as $message)
<li>{{$message}}</li>
@endforeach
</ul>
</div>
@endif




<button type="submit" class="btn btn-primary col-lg-12"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button>
{!! Form::close() !!}

创建模型页面:

                    {!! Form::open(array('route' => ['modele.storea', $produit->id],'enctype' => 'multipart/form-data')) !!}


<div class="form-group">
{!! Form::label('produit', 'Produit') !!}
{!! Form::select('produit', $unProduit, null, array('class' => 'form-control')) !!}
</div>
@if ($errors->has('produit'))
<div class="alert alert-danger" role="alert">
<ul>
@foreach ($errors->get('produit') as $message)
<li>{{$message}}</li>
@endforeach
</ul>
</div>
@endif

<div class="form-group">
{!! Form::label('ref', 'Réference') !!}
{!! Form::text('ref', null,array('maxlength' => 255, 'class'=>'form-control' )) !!}
</div>
@if ($errors->has('ref'))
<div class="alert alert-danger" role="alert">
<ul>
@foreach ($errors->get('ref') as $message)
<li>{{$message}}</li>
@endforeach
</ul>
</div>
@endif
@for ($i = 1; $i < 11; $i++)
<?php $item_name = 't' . $i; ?>

@if ($uneCategorie->$item_name != null)
<div class="col-lg-3">
<div class="form-group">
{!! Form::label($item_name, $uneCategorie->$item_name) !!}
{!! Form::text($item_name, null,array('maxlength' => 255, 'class'=>'form-control' )) !!}
</div>
@if ($errors->has('{{$item_name }}'))
<div class="alert alert-danger" role="alert">
<ul>
@foreach ($errors->get('{{$item_name }}') as $message)
<li>{{$message}}</li>
@endforeach
</ul>
</div>
@endif
</div>
@endif
@endfor

最佳答案

我知道这已经很旧了,但我遇到了类似的问题,这是我的解决方案,重写以匹配主题所有者的帖子:

路线:

Route::match(['get','post'],'modele/selected/product', 'ModeleController@selectedProduct')->name('modele.selectedProduct');

Controller

$unProduit = $request->get('produit')??session('_old_input')['produit_id'];

创建模型页面

{{ Form::hidden('produit_id', $unProduit->id) }}

这至少适用于 Laravel 5.6 和 PHP 7+

关于Laravel - 重定向到路由 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41998580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com