Can't pass data out of foreach in laravel - 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.

Related

Build Laravel multistep form with foreach?

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

Form checkbox input array gets flooded with textarea input

)
I have been struggling with a form for my Laravel App....
I have 3 fields which are checkboxes and therefore is getting POSTed as arrays and then I have a textarea field and somehow SOMETIMES one of the 3 checkbox inputs get flooded with data from the textarea input field... It is very weird and only happens sometime and therefore I get a data to long truncated error in the database...
But I DD the input at the $request level and can see already there the array from the checkbox is flooded with the data from the textarea field... I can not wrap my head around why this is, as it's only sometimes...
And I can even get the error and go back a page in the browser and submit again with all the same data and then it can be working.. WHAT can cause this weird behavioer
This is my Laravel blade where the form is
#extends('layouts.app')
#section('head')
#endsection
#section('content')
#php
// dd($errors);
#endphp
<h2 class="page-title">Opret Artist Side</h2>
<div class="artist-box">
<form class="formgrid" method="POST" action="{{ route('artist.store') }}">
#csrf
<div class="formleft">
<label for="artist_name" class="label">Artist navn</label>
<input type="text" id="artist_name" name="artist_name" class="formitem #error('artist_name') is-invalid #enderror" value="{{ old('artist_name', $post->artist_name ?? null) }}" />
#error('artist_name')
<div class="invalid-feedback">
#foreach($errors->get('artist_name') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="artist_name_help">Jeres band/artist navn. <span class="text-danger">OBS! Kan IKKE ændres</span></small>
#enderror
</div>
<div class="formright">
<label for="contact_person" class="label">Kontakt person</label>
<input type="text" name="contact_person" id="contact_person" class="formitem #error('contact_person') is-invalid #enderror" value="{{ old('contact_person', $post->content ?? null) }}" />
#error('contact_person')
<div class="invalid-feedback">
#foreach($errors->get('contact_person') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="contact_person_help">Kontakt persons navn</small>
#enderror
</div>
<div class="formleft">
<label for="email" class="label">Email</label>
<input type="text" name="email" id="email" class="formitem #error('email') is-invalid #enderror" value="{{ old('email', $post->email ?? null) }}" />
#error('email')
<div class="invalid-feedback">
#foreach($errors->get('email') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="email_help">Kontakt email</small>
#enderror
</div>
<div class="formright">
<label for="phone" class="label">Tlf nummer</label>
<input type="text" name="phone" id="phone" class="formitem #error('phone') is-invalid #enderror" value="{{ old('phone', $post->phone ?? null) }}" />
#error('phone')
<div class="invalid-feedback">
#foreach($errors->get('phone') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="phone_help">Kontakt tlf nummer</small>
#enderror
</div>
<div class="formleft">
<label for="hometown" class="label">Hjemby</label>
<input type="text" name="hometown" id="hometown" class="formitem #error('hometown') is-invalid #enderror" value="{{ old('hometown', $post->hometown ?? null) }}" />
#error('hometown')
<div class="invalid-feedback">
#foreach($errors->get('hometown') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="hometown_help">Jeres Hjemby</small>
#enderror
</div>
<div class="formright">
<label for="category" class="label">Kategori</label>
<select name="category" id="category" class="formitem #error('category') is-invalid #enderror">
{{-- Show a non selectable if with info of none selected --}}
#if(old('category') === null)
<option disabled="disabled" selected value="null">Vælg en kategori</option>
#endif
#foreach($categories as $category)
<option #if(old('category') !== null && old('category') == $category->id) selected #endif value="{{ $category->id }}"
>{{ $category->artist_category }}</option>
#endforeach
</select>
#error('category')
<div class="invalid-feedback">
#foreach($errors->get('category') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="">Hvilken kategori hører i under</small>
#enderror
</div>
<div class="formfull">
<label for="budget" class="label">Budget i spiller for</label>
<ul class="ks-cboxtags">
#foreach($budgets as $budget)
{{-- <div class="form-check form-check-inline"> --}}
<li><input #if(old('budget') !== null && in_array($budget->id,old('budget'))) checked #endif
type="checkbox" name="budget[]" id="budget{{ $budget->id }}" class="form-check-input #error('budget') is-invalid #enderror" value="{{ $budget->id }}" />
<label class="form-check-label" for="budget{{ $budget->id }}">{{ $budget->artist_budget }}</label></li>
{{-- </div> --}}
#endforeach
</ul>
#error('budget')
<div class="invalid-feedback d-block">
#foreach($errors->get('budget') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="budget_help">Vælg hvilket budget i optræder for, gerne flere</small>
#enderror
</div>
{{-- --}}
<div class="formfull">
<label for="genres" class="label">Genre</label>
<ul class="ks-cboxtags">
#foreach($genres as $genre)
{{-- <div class="form-check form-check-inline"> --}}
<li><input #if(old('genres') !== null && in_array($genre->id,old('genres'))) checked #endif
type="checkbox" name="genres[]" id="genres{{ $genre->id }}" value="{{ $genre->id }}" class="form-check-input #error('genres') is-invalid #enderror" />
<label class="form-check-label" for="genres{{ $genre->id }}">{{ $genre->genre }}</label></li>
{{-- </div> --}}
#endforeach
</ul>
#error('genres')
<div class="invalid-feedback d-block">
#foreach($errors->get('genres') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="">Hvike genrer hører i under</small>
#enderror
</div>
{{-- --}}
<div class="formfull">
<label for="area" class="label">Områder hvor i optræder</label>
<ul class="ks-cboxtags">
#foreach($areas as $area)
{{-- <div class="form-check form-check-inline"> --}}
<li><input #if(old('area') !== null && in_array($area->id,old('area'))) checked #endif
type="checkbox" name="area[]" id="area{{ $area->id }}" value="{{ $area->id }}" class="form-check-input #error('area') is-invalid #enderror" />
<label class="form-check-label" for="area{{ $area->id }}">{{ $area->artist_area }}</label></li>
{{-- </div> --}}
#endforeach
</ul>
#error('area')
<div class="invalid-feedback d-block">
#foreach($errors->get('area') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="">Vælg hvilke områder i optræder i</small>
#enderror
</div>
<div class="formleft">
<label for="desc" class="label">Beskriv jer selv</label>
<textarea name="description" id="description" class="formitem #error('description') is-invalid #enderror">{{ old('description', $post->description ?? null) }}</textarea>
{{-- <input id="description" value="{{ old('description', $post->description ?? null) }}" class="form-control #error('description') is-invalid #enderror" name="description" type="hidden">
#trix(\App\ArtistPage::class, 'trixinput',
[
'id' => 'description',
// 'class' => 'form-control',
'hideTools' => ['file-tools'],
'hideButtonIcons' => ['attach', 'link', 'code', 'strike', 'heading-1']
]) --}}
{{-- <input id="desc" value="{{ old('description', $post->description ?? null) }}" type="hidden" name="description">
<trix-editor input="desc" class="#error('description') is-invalid #enderror"></trix-editor> --}}
#error('description')
<div class="invalid-feedback">
#foreach($errors->get('description') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="description_help">Giv en god beskrivelse af jer selv og hvad i tilbyder<br>
Tilladte tags er <strong>{{ '<br><b><i><li><ul><ol><hr><strong><p>' }}</strong>
</small>
#enderror
</div>
<button type="submit" class="btn btn-primary">Opret</button>
</form>
</div>
#endsection
See also this image of the DD dump on the $request
Hmmmm Think I figured it out :-)
But it has been such a sporadic bug so I'm not sure yet... But seems like it has gone away :-)
Seems like it was this value line on each field I have gotten messed up
The value line structure on the input fields I had was
value="{{ old('artist_name', $post->artist_name ?? null) }}"
And it should be
value="{{ old('artist_name') ?? $post->artist_name ?? null }}"
Dont know why I ended up with such an obscure Value part on every input field :-)
But sometimes the eyes just gets blind on code :-)

Error "The PATCH method is not supported for this route. Supported methods: GET, HEAD, POST. " update method

Well hello there, I am trying to update a register of my table assistants, but when I push the button submit of the form, this error appear, I am using a table pivot between assistants and events, but i am only try to edit a assistant.This is the error
This is my code AssistantController.php file and the methods edit and update
edit and update methods
public function edit(Assistant $assistant)
{
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return view('assistants.edit',compact('assistant','event'));
}
public function update(Request $request, Assistant $assistant)
{
#updating
$assistant->update($request->all());
//$assistants->user()->associate(Auth::user());
//
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return redirect('assistants.index',compact('assistant','event'));
}
This is my form to assistants
input form assistants
#csrf
<div class="form-group">
<label for="id">Document:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('id') ?? $assistant->id }}" name="id" autofocus>
<div>{{ $errors->first('id') }}</div>
</div>
<div class="form-group">
<label for="name">Name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('name') ?? $assistant->name }}" name="name" placeholder="First Name assistant">
<div>{{ $errors->first('name') }}</div>
</div>
<div class="form-group">
<label for="last_name">Last name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('last_name') ?? $assistant->last_name }}" name="last_name" placeholder="Last name assistant">
<div>{{ $errors->first('last_name') }}</div>
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('phone') ?? $assistant->phone }}" name="phone">
<div>{{ $errors->first('phone') }}</div>
</div>
<div class="form-group">
<label for="email">Email:</label>
<small class="text-muted">Required(*)</small>
<input type="mail" class="form-control form-control-sm " value="{{ old('email') ?? $assistant->email }}" name="email">
<div>{{ $errors->first('email') }}</div>
</div>
<div class="form-group">
<label for="observations">Observations:</label>
<input type="text" class="form-control form-control-sm " value="{{ old('observations') ?? $assistant->observations }}"name="observations">
<div>{{ $errors->first('observations') }}</div>
</div>
This is my edit page
#extends('layouts.back')
#section('title','Edit assistants')
#section('content')
<div class="container p-4">
<div class="row">
<div class="card">
<div class="card-header">
<div class="card-title">
<h3>Edit details to assistant:</h3><br>
<h4><strong> {{$assistant->name}} {{$assistant->last_name}}</strong> </h4>
</div>
</div>
<div class="card-body">
<form action="/assistants" method="POST">
#method('PATCH')
#include('assistants.form')
<button type="submit" class="btn btn-primary">Update</button>
Cancel
</form>
</div>
<div class="card-footer">
</div>
</div>
</div>
</div>
#endsection
There are my routes
routes
Auth::routes();
Route::get('/', 'HomeController#index')->name('home');
Route::resource('events', 'EventController');
Route::resource('assistants', 'AssistantController');
Route::resource('certificates', 'CertificateController');
Route::resource('signers', 'SignerController');
I am beginner.
thank you all.
In your edit page, change your form action from
<form action="/assistants" method="POST">
to
<form action="/assistants/{{ $assistant->id }}" method="POST">
Without the assistant id in the action field, Laravel thinks you are trying to update the base assistants route, which is an invalid action for that route.

button disappeared after configuration laravel

hello my radiobutton disappeared after a laravel configuration they are used to modify user roles they worked for a moment and then disappeared
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Modifier <strong> {{ $user->name }}</strong></div>
<div class="card-body">
<form action="{{ route('admin.user.update', $user) }}" method="POST">
#csrf
#method('PATCH')
#foreach ($roles as $role)
<div class="form-group form-check">
#foreach ($user->roles as $userRole)
#if ($userRole->id == $role->id)
<input type="radio" class="form-check-input" name="roles[]" value="{{ $userRole }}" checked>
#else
<input type="radio" class="form-check-input" name="roles[]" value="{{ $userRole }}">
#endif
#endforeach
<label for="{{ $role->id }}" class="form-check-label">{{ $role->name }}</label>
</div>
#endforeach
<button type="submit" class="btn btn-primary">Modifier les roles</button>
</form>
You can use contains() with key and value defined:
The contains() method determines whether the collection contains a given item:
#foreach ($roles as $role)
<div class="form-group form-check">
<input type="radio" class="form-check-input" name="roles[]" value="{{ $role->id }}" {{ $user->roles->contains('id', $role->id) ? 'checked' : '' }} >
<label for="{{ $role->id }}" class="form-check-label">{{ $role->name }}</label>
</div>
#endforeach

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

Resources