Is that the entire store method? Do you have any validation?
form redirects back and doesnt send data in laravel
i have a form whereby am filing data and saving it to a table in the database.i have added all the necessary details but upon submitting the page just reloads and redirects back to the page without showing any error.i here is the form in the blade file
<form role="form" action="{{ route('coupons.store') }}" method="post" >
@csrf
<div class="row form-group">
<div class="col-md-12 mb-4">
<h6 class="mb-2 pb-1 form-label" style="text-align: center">Coupon Option: </h6>
<div class="form-check form-check-inline">
<label class="form-check-label" for="coupon_option">Automatic</label>
<input class="form-check-input" type="radio" name="coupon_option" id="automaticcoupon" value="Automatic Coupon"/>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label" for="coupon_option">Manual</label>
<input class="form-check-input" type="radio" name="coupon_option" id="manualcoupon" value="Manual Coupon" />
</div>
</div>
<div class="form-outline" style="display: none;" id="coupon_field">
<label class="form-label" for="coupon_code">Enter The Coupon Code</label>
<input placeholder="Enter Coupon Code Here" type="text" id="coupon_code" class="form-control form-control-lg" name="coupon_code" />
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-4">
<h6 class="mb-2 pb-1 form-label" style="text-align: center">Coupon Type: </h6>
<div class="form-check form-check-inline">
<label class="form-check-label" for="coupon_type">Single</label>
<input class="form-check-input" type="radio" name="coupon_type" value="Single Time"/>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label" for="coupon_type">Multiple Times</label>
<input class="form-check-input" type="radio" name="coupon_type" value="Multiple Times" />
</div>
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-4">
<h6 class="mb-2 pb-1 form-label" style="text-align: center">Amount Type: </h6>
<div class="form-check form-check-inline">
<label class="form-check-label" for="amount_type">Percentage(%)</label>
<input class="form-check-input" type="radio" name="amount_type" value="Percentage"/>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label" for="amount_type">Fixed(Ksh)</label>
<input class="form-check-input" type="radio" name="amount_type" value="Fixed" />
</div>
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-4">
<div class="form-outline">
<label class="form-label" for="amount">Amount</label>
<input type="number" id="amount" name="amount" class="form-control form-control-lg" style="background: rgb(196, 191, 191);"/>
</div>
</div>
</div>
<div class="row form-group">
<div class="col-md-12 mb-4">
<div class="form-outline">
<label class="form-label" for="expiry_date">Expiry Date</label>
<input type="text" id="expiry_date" name="expiry_date" class="form-control form-control-lg" style="background: rgb(196, 191, 191);"/>
</div>
</div>
</div>
<div class="form-group">
<label for="FormControlSelect">Product Categories</label>
<select name="prodcategories[]" class="couponselect2 form-control" multiple="multiple">
<option>Select Product Categories</option>
@foreach($prodctcategories as $prodCategory)
<option value="{{ $prodCategory->id }}">{{ $prodCategory->merchadisecat_title }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="FormControlSelect">Users</label>
<select name="couponusers[]" class="usersselect2 form-control" multiple="multiple" >
<option>Select Users</option>
@foreach($couponusers as $couponuser)
<option value="{{ $couponuser->email }}">{{ $couponuser->email }}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn-primary btn-lg"> Submit </button>
</form>
here is my store function
public function store(Request $request)
{
if($request->isMethod('post')){
$data=$request->all();
$rules=[
'coupon_option'=>'required',
'coupon_type'=>'required',
'amount_type'=>'required',
'amount'=>'required',
'expiry_date'=>'required',
'categories'=>'required',
'users'=>'required',
];
$custommessages=[
'users.required'=>'Select Users',
'categories.required'=>'Select Categories',
'coupon_option.required'=>'Select a Coupon Option',
'coupon_type.required'=>'Select a Coupon Type',
'amount_type.required'=>'Select Amount Type',
'amount.required'=>'Enter Amount',
'amount.numeric'=>'Enter a Valid Amount',
'expiry_date.required'=>'Select Expiry Date',
];
$this->validate($request,$rules,$custommessages);
if(isset($data['users'])){
$users=implode(',',$data['users']);
}else{
$users="";
}
if(isset($data['categories'])){
$categories=implode(',',$data['categories']);
}else{
$categories="";
}
if($data['coupon_option']=='Automatic'){
$coupon_code=str_random(7);
}else{
$coupon_code=$data['coupon_code'];
}
$coupon=new coupon();
$coupon->coupon_option=$data['coupon_option'];
$coupon->coupon_type=$data['coupon_type'];
$coupon->amount_type=$data['amount_type'];
$coupon->amount=$data['amount'];
$coupon->expiry_date=$data['expiry_date'];
$coupon->categories=$categories;
$coupon->users=$users;
$coupon->save();
}
on dd($data);die(); i get n array of all the data that i have filled in the form.where might i be going wrong in the code for it to redirect bck instead of saving the data
Your categories (validation) are called prodcategories (form)
1st Step is to always dump at the top of the controller method and check that the form contains the expected data,
Please or to participate in this conversation.