Reputation: 189
I am using laravel and for storing category I use Modal I want to have two separate buttons for submitting new category one is "Store" and two is "store and continue" and after press this button I want to store new category and after redirect show new modal
How can I do this?
here is my form
<div class="modal fade bd-example-modal-xl" id="AddProductCategoryModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-xl" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">افزودن دسته بندی جدید</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body modal-scroll">
<form action="{{ route('product-category.store') }}" method="post" class="form-horizontal">
@csrf
<div class="form-group mb-0">
<div class="input-group mt-3">
<div class="input-group-prepend"><span class="input-group-text bg-light min-width-140" id="basic-addon7">عنوان دسته بندی :</span></div>
<input type="text" class="form-control inputfield" name="name" placeholder="مثال: ورزشی">
</div>
<div class="input-group mt-3">
<div class="input-group-prepend"><span class="input-group-text bg-light min-width-140" id="basic-addon7">توضیحات دسته بندی :</span></div>
<input type="text" class="form-control inputfield" name="description" placeholder="مثال: توضیحات مختصری درمورد دسته بندی">
</div>
</div>
<!--end form-group-->
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-danger" data-dismiss="modal">انصراف</button>
<div class="group">
<button type="submit" class="btn btn-primary">store</button>
<button type="submit" class="btn btn-primary">store and continu</button>
</div>
</div>
</form>
</div>
</div>
</div>
and here is my controller
public function store(ProductCategoryRequest $request)
{
if(\Auth::user()->shop()->first()->ProductCategories()->where('name',$request->name)->get()->count() == null){
$productCategory = new ProductCategory;
$productCategory->name = $request->name;
$productCategory->description = $request->description;
$productCategory->shop_id = \Auth::user()->shop()->first()->id;
$productCategory->save();
alert()->success('دسته بندی جدید شما باموفقیت اضافه شد.', 'ثبت شد');
return redirect()->route('product-category.index');
}
else{
alert()->error('دسته بندی با این نام قبلا در فروشگاه شما ثبت شده است ', 'خطا');
return redirect()->route('product-category.index');
}
}
btw my route is a resource
PHP Route::resource('product-category', 'ProductCategoryController');
Upvotes: 0
Views: 1088
Reputation: 396
to handle the submit from 2 buttons. give a name
attribute to the buttons with action
and value of the name of the action lets say:
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-danger" data-dismiss="modal">انصراف</button>
<div class="group">
<button type="submit" name="action" value="justSave" class="btn btn-primary">store</button>
<button type="submit" name="action" value="saveAndContinue" class="btn btn-primary">store and continu</button>
</div>
</div>
then in your store method check for a cases :
public function store(Request $request)
{
switch ($request->input('action')) {
case 'justSave':
if(\Auth::user()->shop()->first()->ProductCategories()->where('name',$request->name)->get()->count() == null){
$productCategory = new ProductCategory;
$productCategory->name = $request->name;
$productCategory->description = $request->description;
$productCategory->shop_id = \Auth::user()->shop()->first()->id;
$productCategory->save();
alert()->success('دسته بندی جدید شما باموفقیت اضافه شد.', 'ثبت شد');
return redirect()->route('product-category.index');
}
else{
alert()->error('دسته بندی با این نام قبلا در فروشگاه شما ثبت شده است ', 'خطا');
return redirect()->route('product-category.index');
}
break;
case 'saveAndContinue':
if(\Auth::user()->shop()->first()->ProductCategories()->where('name',$request->name)->get()->count() == null){
$productCategory = new ProductCategory;
$productCategory->name = $request->name;
$productCategory->description = $request->description;
$productCategory->shop_id = \Auth::user()->shop()->first()->id;
$productCategory->save();
session()->flash('flashModal');
alert()->success('دسته بندی جدید شما باموفقیت اضافه شد.', 'ثبت شد');
return redirect()->route('product-category.index');
}
else{
alert()->error('دسته بندی با این نام قبلا در فروشگاه شما ثبت شده است ', 'خطا');
return redirect()->route('product-category.index');
}
break;
}
}
in your view check for the session and trigger the modal if the session exists :
@if(session()->has('flashModal'))
<script>
$('#YourModalId').modal('show');
</script>
@endif
Upvotes: 1