Laravel – Redirect even if no product selected
I have a system where a user can select an item, submit a request and move on. I need to give the user the option to NOT select an item and still be able to move on. In my current situation, I don’t know how to achieve that.
In my view, I display all the selectable items like this, using a foreach:
@foreach($themes as $theme)
<div class="col-md-4 mb-4">
<div class="card theme-card card-hover depth-2 border-0" id="theme-id-">
<a href="" class="theme-link" data-toggle="modal" data-target="#theme">
<div class="card-header p-0">
@if($theme->thumbnail != 0)
<img src="https://s.tmimgcdn.com/scr/67400/magento-thema-over-boxen_67400-original.jpg?width=502&height=502" width="502" height="350" class="theme-card-img" alt=""> @else
<img src="http://jis.gov.jm/media/blank.png" class="theme-card-img" alt=""> @endif
</div>
<div class="card-body">
<div class="row">
<div class="col-md-2 vertical-center">
<i class="fab fa-magento fa-lg"></i>
</div>
<div class="col-md-10">
<p class="m-0">{!! str_limit($theme->name, $limit = 32, $end = '...') !!}</p>
<small></small>
</div>
</div>
</div>
</a>
<div class="card-footer bg-white border-0 text-right pt-0">
<div class="row">
<div class="col-md-6 text-left">
<input type="hidden" class="theme-name" name="theme[]">
<button data-card-id="" class="btn btn-orange btn-sm btn-theme-choice">Kiezen</button>
</div>
<div class="col-md-6 text-right">
<small class="text-muted" style="text-decoration: line-through">145 €</small><span style="font-size: 20px;"> €</span>
</div>
</div>
</div>
</div>
</div>
@endforeach
Here, I supply the ID of each product. Then in my jQuery file, I do the following: When a user presses the button. The ID gets called and the value of the input is being set to ‘selected’.
$('.btn-theme-choice').on('click', function (event) {
event.preventDefault();
newSelectedCardId = $(event.target).data('card-id');
// console.log(newSelectedCardPkg);
if(cardId === null) {
cardId = newSelectedCardId;
} else if (cardId !== newSelectedCardId) {
$('#theme-id-' + cardId).removeClass('theme-chosen').find("input[name='theme["+cardId+"]']").val('');
cardId = $(event.target).data('card-id');
}
var card = $('#theme-id-' + cardId );
card.toggleClass('theme-chosen');
selectedCardInput = card.find("input[name='theme["+cardId+"]']");
selectedCardInput.val('selected');
console.log(selectedCardInput);
});
So when a user clicks on one of the buttons. That product is getting ‘selected’. Now in my controller, I foreach all the inputs and check if the value is ‘selected’. If it is, a variable called $selectedTheme will be filled with the ID. Then I find the selected product and put its values in a session and then the user gets redirected to ‘/plugins’.
The problem is
When I don’t select a theme and submit the form. It gives me the error Trying to get property 'id' of none object
. The user needs to be able to go to that link even if he/she did not select a product. If the user did not select a product. The session shouldn’t even be made. I know how to check if no product is selected in the if-statement that is inside the foreach loop but that’s about it. How can I achieve this?
from Laravel Questions and Answers https://laravelquestions.com/php/laravel-redirect-even-if-no-product-selected/
via Lzo Media
No comments:
Post a Comment