如果验证器失败,我试图重定向我的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
控制器:
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
答案 0 :(得分:0)
你必须提出请求..在这种情况下无法发出请求
答案 1 :(得分:0)
我知道这是旧的,但我有一个类似的问题,这是我的解决方案,重写以匹配主题所有者帖子:
路线:
Route::match(['get','post'],'modele/selected/product', 'ModeleController@selectedProduct')->name('modele.selectedProduct');
控制器
$unProduit = $request->get('produit')??session('_old_input')['produit_id'];
创建模型页面
{{ Form::hidden('produit_id', $unProduit->id) }}
这至少适用于Laravel 5.6和PHP 7 +