Build Laravel multistep form with foreach? - laravel

Good morning,
I want build a multistep form in Laravel and I discover a lot of tutorial on Youtube to make this but In my form, I use a Foreach because I build a exam system and I display Question and Response with foreach.
This is my Code :
<form method="POST" action="{{ route('stagiaire.session.test.store',['id'=>$eval->id]) }}">
#csrf
#foreach($questioncat as $category)
<div class="card mb-3">
<div class="card-header">{{ $category->nom }}</div>
<div class="card-body">
#foreach($category->getQuestion as $question)
<div class="card #if(!$loop->last)mb-3 #endif">
<div class="card-header">{{ $question->question_text }}</div>
<div class="card-body">
<input type="hidden" name="questions[{{ $question->id }}]" value="">
#foreach($question->reponse as $option)
<div class="form-check">
<input class="form-check-input" type="radio" name="questions[{{ $question->id }}]" id="option-{{ $option->id }}" value="{{ $option->id }}"#if(old("questions.$question->id") == $option->id) checked #endif>
<label class="form-check-label" for="option-{{ $option->id }}">
{{ $option->option_text }}
</label>
</div>
#endforeach
#if($errors->has("questions.$question->id"))
<span style="margin-top: .25rem; font-size: 80%; color: #e3342f;" role="alert">
<strong>{{ $errors->first("questions.$question->id") }}</strong>
</span>
#endif
</div>
</div>
#endforeach
</div>
</div>
#endforeach
<div class="form-group row mb-0">
<div class="col-md-6">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</div>
</form>
So my question, is it possible to build a multistep form with foreach?
Thanks
I am at the thinking stage

Related

Can't pass data out of foreach in laravel

How can I export the data when I open a foreach in the application I developed in Laravel. For example, I want to filter after some id sync.
Blade/View
#foreach ($parts as $item)
#foreach ($country->getPerson ?? [] as $person)
#if ($country->id == $person->country_id)
#endif
#endforeach
#if ($loop->iteration == $person->part_id)
#continue
#endif
<div class="col-sm-6 col-lg-4">
<div class="service-item my-3">
<div class="service-wrapper m-0 bg-white">
<div class="service-content mt-3">
<h5 class="mb-3"> {{ $item->name }}</h5>
<a data-bs-toggle="modal" data-bs- target="#service1"
href="#" class="text-primary">
Alert!
<i class="fas fa-angle-right ms-2"> </i>
</a>
<br> <br>
<form class="call-to-action-form m-auto" action="{{ route('add.person') }}"
enctype="multipart/form-data" method="POST">
#csrf
<div class="input-group">
<input type="text" class="form-control" name="name"
placeholder="Adınız Soyadınız" required> <br>
<input type="text" class="form- control" name="email"
placeholder="E-Mail Adresiniz" required>
</div>
<input type="hidden" name="country_id" value="{{ $country->id }}" required>
<input type="hidden" name="part_id" value="{{ $item->part_id }}" required>
<br>
<button class="btn btn-success" type="submit">Cüzü Al
</button>
</form>
</div>
</div>
</div>
#endforeach
I want to return it here.
what I want to do is
{{person->part_id}}
by assigning its value outside the foreach
#if ($loop->iteration == $person->part_id)
#continue
#endif
My associated models run regularly. Now I need to filter the ids corresponding to my person table in my part table. The code sequence I shared above only takes the latest id. I cannot get other corresponding ids, so it only filters once.

Laravel 8 How to restrict user login if email is not verified

I am using Bootstrap Starter template package in Laravel 8 that replaces Tailwind CSS framework with Bootstrap CSS framework.
https://packagist.org/packages/shahvirag/laravel-ui-bootstrap
The problem I'm having is that user can sign in after registration without needing to verify account through email. How can I restrict his access until the email is not verified?
I have followed the steps and tried using routes middleware, but there is no such file in App\Http\Controllers\PagesController;
Route::get('/',[PagesController::class, 'index'])->name('users')->middleware('verified'); // this does not work
Any help?
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagesController;
Route::get('/', function () {
return view('main');
});
Route::middleware(['auth'])->group(function() {
Route::get('/home', function() {
return view('home');
})->name('home');
Route::get('/user/profile', function() {
return view('profile');
})->name('profile');
});
Route::get('/',[PagesController::class, 'index'])->name('users')->middleware('verified');
resources/views/auth/login.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Login') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('login') }}">
#csrf
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="current-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<div class="col-md-6 offset-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
<label class="form-check-label" for="remember">
{{ __('Remember Me') }}
</label>
</div>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
#if (Route::has('password.request'))
<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
#endif
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
To use verified middleware, your user model should implement MustVerifyEmail
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
// ...
}
Try this instead
Route::get('/',[PagesController::class, 'index'])->name('users')->middleware(['auth', 'verified']);
You can use the method included with the User model:
$user->hasVerifiedEmail()
Or in the routes middleware(['verified'])

The PATCH method is not supported for this route. when second form is submitted

I've been working on a CMS system for a website im building and it all went good untill I added 2 forms on the same page (both updating different things) and yet it still gives me the error in the title while the other form works fine?
The page banner is the one which returns the error and the 'normal' update works fine!
Web.php
Route::patch('/beheer/paginas/{product}', 'PageController#update')->middleware('auth')->name('beheer.pages.update');
Route::patch('/beheer/paginas/update-banner/{product}', 'PageController#update-banner')->middleware('auth')->name('beheer.pages.banner.update');
Also shows in php artisan route:list that it's a PATCH method route.
My view:
#extends('layouts.beheer')
#section('content')
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<div class="d-inline card-title">Pagina bewerken</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
<div class="card-body">
<form action="{{ route('beheer.pages.update', $page->id) }}" method="POST" enctype="multipart/form-data">
#csrf
{{ method_field('PATCH') }}
<div class="form-group">
<label>Zichtbaarheid:</label>
<select class="custom-select" name="page_hidden">
<option value="0" #if (!$page->page_hidden) selected #endif>Zichtbaar</option>
<option value="1" #if ($page->page_hidden) selected #endif>Verborgen</option>
</select>
</div>
<div class="form-group">
<label>Pagina naam:</label>
<input type="text" name="page_name" value="{{ $page->page_name ? $page->page_name : old('page_name') }}" class="form-control" placeholder="Pagina naam">
</div>
<div class="form-group">
<label>Pagina tekst:</label>
<textarea name="page_text" class="form-control">{{ $page->page_text ? $page->page_text : old('page_meta_title') }}</textarea>
</div>
<div class="form-group">
<label>Pagina title SEO:</label>
<input type="text" name="page_meta_title" value="{{ $page->page_meta_title ? $page->page_meta_title : old('page_meta_title') }}" class="form-control" placeholder="Pagina titel SEO">
</div>
<div class="form-group">
<label>Pagina beschijving SEO:</label>
<input type="text" name="page_meta_description" value="{{ $page->page_meta_description ? $page->page_meta_description : old('page_meta_description') }}" class="form-control" placeholder="Pagina beschijving SEO">
</div>
<button type="submit" class="btn btn-primary float-right">Opslaan</button>
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">
<div class="d-inline card-title">Banner</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
<div class="card-body">
<form onsubmit="{{ route('beheer.pages.banner.update', $page->id) }}" method="POST" enctype="multipart/form-data">
#csrf
{{ method_field('PATCH') }}
<input type="hidden" name="image_section_banner" value="1">
<input type="hidden" name="page_id" value="{{ $page->id }}">
<div class="form-group">
<img src="{{ asset($page_banner ? $page_banner->image_large_url : asset('assets/img/banner-home.jpg')) }}" class="img-fluid" style="max-height:250px;">
</div>
<div class="form-group">
<label>Afbeelding:</label>
<input type="file" name="image" value="{{ old('image') }}" class="form-control">
</div>
<button type="submit" class="btn btn-primary float-right">Opslaan</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
If there is any additional information needed please let me know!
There is an attribute missing on your 2nd <form>. Here is what you have:
<form onsubmit="{{ route('beheer.pages.banner.update', $page->id) }}" ...>
But it should look like this:
<form action="{{ route('beheer.pages.banner.update', $page->id) }}" ...>
Since you did not specify an action attribute, it is submitting to the current url which probably doesn't have a patch route defined.

Laravel app displays wrong image source when deployed

My app works perfectly offline but when deployed, I didn't get to display images properly.
this is my store controller function
public function store(Request $request)
{
$contestant = new Contestant();
$contestant->name = $request->name;
$contestant->gender = $request->Gender[0];
$contestant->Occupation = $request->Occupation;
$contestant->Hobbies = $request->Hobbies;
$contestant->DOB = $request->DOB;
$contestant->Nationality = $request->Nationality;
$contestant->location = $request->location;
$contestant->About = $request->About;
$contestant->votes = 0;
$image = $request->photo;
$imagePath = $image->store('contestant', 'public');
$contestant->image = $imagePath;
// Image::make(public_path("storage/{$imagePath}"))->fit(1200, 1200);
// $contestant -> image = $imagePath;
$contestant->save();
return view('admin.home');
}
this is is how I display it in my blade
<a class="cardlink" href="{{ route('Contestant.index',$contestant -> id)}}">
<img class=" card-img-top " src="/storage/{{ $contestant -> image }}" alt="Card image cap">
</a>
EDIT
html code:
#foreach ($contestants as $contestant)
<div class="col-md-4">
<div class="card shadow contestant-card">
<a class="cardlink" href="{{ route('Contestant.index',$contestant -> id)}}">
<img class=" card-img-top " src="/storage/{{ $contestant -> image }}" alt="Card image cap"></a>
<div class="card-body">
<h5 class="card-title border-bottom pb-3"> <span class="text-dark"> Name:
</span><br>{{ $contestant -> name }}
{{-- <a href="#"
class="float-right d-inline-flex share"><i class="fa fa-share-alt text-primary"></i></a> --}}
</h5>
<p>
<span class="card-text">{{ $contestant -> Ocupation ?? $contestant -> gender }}</span> |
<span class="card-text"> {{ $contestant -> location }}.</span>
</p>
<input type="hidden" name="id" value="{{ $contestant -> id }}">
<button data-toggle="modal" data-target="#exampleModalCenter"
class="btn btn-lg btn-info btn-block">Vote</button>
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog"
aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Vote for
{{ $contestant -> name }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
#csrf
#method('POST')
<div class="form-group">
<label for="cc-payment" class="control-label mb-1">contestant Name: </label>
<input id="cc-pament" name="cc-payment" type="text" class="form-control"
disabled value="{{ $contestant -> name }}">
</div>
<div class="form-group">
<label for="vote" class="control-label mb-1">Number of Votes: </label>
<select name="cc-vote" id="cc-vote" class="form-control"> Number of Votes
<option value="none" selected disabled> Select</option>
<option value="50">1 Vote for ₦50</option>
<option value="3000">20 Votes for ₦3000</option>
<option value="5000">40 Votes for ₦5000</option>
<option value="10000">100 Votes for ₦10,000</option>
<option value="20000">250 Votes for ₦20,000</option>
<option value="40000">550 Votes for ₦40,000</option>
<option value="100000">1,200 Votes for ₦100,000</option>
<option value="200000">2,500 Votes for ₦200,000</option>
</select>
</div>
<div class="form-group has-success">
<label for="cc-name" class="control-label mb-1">Name</label>
<input id="cc-name" name="cc-name" type="text" class="form-control cc-name">
<span class="help-block field-validation-valid" data-valmsg-for="cc-name"
data-valmsg-replace="true"></span>
</div>
<div>
<form action="{{ route('pay') }}" method="post" novalidate="novalidate">
<input type="hidden" name="email" value="otemuyiwca#gmail.com">
{{-- required --}}
<input type="hidden" name="orderID" value="345">
<input type="hidden" id="amount" name="amount" value="">
<input type="hidden" name="metadata"
value="{{ json_encode($array = ['id' => $contestant -> id,]) }}">
<input type="hidden" name="reference"
value="{{ Unicodeveloper\Paystack\Facades\Paystack::genTranxRef() }}">
{{-- required --}}
<input type="hidden" name="key" value="{{ config('paystack.secretKey') }}">
{{-- required --}}
{{ csrf_field() }} {{-- works only when using laravel 5.1, 5.2 --}}
<button id="payment-button" type="submit"
class="btn btn-lg btn-info btn-block">
<i class="fa fa-lock fa-lg"></i>
<span id="payment-button-amount">Pay</span>
<span id="payment-button-sending" style="display:none;">Sending…</span>
</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</a>
</div>
</div>
The image is stored as contestant/dgfy563tgfFTjhhyyf6CTiihiDc5tCT.jpeg in my database but gives a broken image when displayed, when I inspect element with chrome, img src is "/storage//tmp/phptYYEv9

Catch ONLY the invalid credentials error in a login.blade Laravel

I am using the custom login controller from Laravel. I have the validation messages in a below the input fields.
Everything works fine.
What I want now is to show a message “incorrect credentials” ONLY when user or password are incorrect and in a different div. I mean, if other validation error triggers, this message should not be visible.
The errors->has(‘email’) array catches this error but also the rest, for instance, ‘the field is required’.
Does anybody know how to write a condition that only catches this ‘invalid credentials’ error message?
Below the template.
Thanks in advance for your help!
#extends ('layouts.default')
#section('content')
#if ($errors->has('email')) {{-- I want the credential error here, but only
for credential error is triggered --}}
<div class="warning">
<div class="input-icon">
<i style="font-size:1.5em; color:Tomato; margin-right:5px;" class="fas
fa-exclamation-triangle"></i>
</div>
<p>Usuario o contraseña incorrecta</p>
</div>
#endif
<main class="login-page">
<div class="contact login">
<div class="titulos">
<p>Ingresar</p>
<p>Soy nuevo</p>
</div>
<form method="post">
#csrf
<div class="input-group input-group-icon">
<input type="email" name="email" placeholder="Correo electrónico"
value="{{ old('email') }}" autofocus/>
<div class="input-icon">
<i class="fas fa-envelope"></i>
</div>
<span class="obligatorio" > {{ $errors->first('email') }}</span>
</div>
<div class="input-group input-group-icon">
<input type="password" name="contraseña"
placeholder="Contraseña"/>
<div class="input-icon">
<i class="fas fa-lock"></i>
</div>
<span class="obligatorio" >{{ $errors->first('contraseña') }}
<div class="input-group">
<input type="submit" value="Ingresar" />
<a href="{{ route('password.request') }}">Olvidé mi
contraseña</a>
</div>
<div>
<label>
<input type="checkbox" name="recordar" id="cbox1"
value="recordar" {{ old('recordar') ? 'checked' : '' }}>
<span>Recordar mi usuario</span>
</label>
</div>
</form>
</div>
</main>
#endsection
In an old beginner Project of mine I did it this way:
<div class="form-group{{ $errors->has('text') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">#lang('views/auth/login.username')</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name">
#if ($errors->has('text'))
<span class="help-block">
<strong>{{ $errors->first('text') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">#lang('views/auth/login.password')</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password">
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
And inside the Controller I just catched the Exceptions:
public function authenticate(Request $request)
{
try {
$this->ldapHelper->checkCredentials($request->name, $request->password);
$ldapUserData = $this->ldapHelper->getFormattedUserData(request('name'));
$this->sessionService->login($ldapUserData);
return redirect()->route('form.formular');
} catch (\Exception $e) {
return back()->withInput()->withErrors([
'message' => $e->getMessage(),
]);
}
}
Maybe it helps!

Resources