How pass the data of a foreach to a modal window? - php

This is the code that i have in index.blade.php
#foreach ($posts as $item)
<!-- Someting -->
<form name="f" method="POST" action="route...">
#csrf
#method('DELETE')
<button class="modal-open"><i class="fas fa-edit"></i></button>
<!-- Delete button... -->
</form>
#endforeach
How i can open a modal window with the data from $item where i clicked the edit button and not the others? (When i tried the code always give me the data of the frist or the last $item)
Here the modal window code
<div class="modal opacity-0 pointer-events-none fixed w-full h-full top-0 left-0 flex items-center justify-center">
<div class="modal-overlay absolute w-full h-full bg-gray-900 opacity-50"></div>
<div class="modal-container bg-white w-11/12 md:max-w-md mx-auto rounded shadow-lg z-50 overflow-y-auto">
<div class="modal-close absolute top-0 right-0 cursor-pointer flex flex-col items-center mt-4 mr-4 text-white text-sm z-50">
<svg class="fill-current text-white" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">
<path d="M14.53 4.53l-1.06-1.06L9 7.94 4.53 3.47 3.47 4.53 7.94 9l-4.47 4.47 1.06 1.06L9 10.06l4.47 4.47 1.06-1.06L10.06 9z"></path>
</svg>
<span class="text-sm">(Esc)</span>
</div>
<div class="modal-content py-4 text-left px-6">
<form name="form" action="route..." method="GET" class="mt-3">
#csrf
#method('POST')
<br>
<textarea class="bg-gray-300 rounded" name="mensaje" placeholder="{{Here the old data}}" required></textarea>
<input type="hidden" name="foro_id" value="#">
<input type="hidden" name="tema_id" value="#">
<div class="mt-2">
<button type="submit" class="bg-green-300 p-1 rounded">Actualizar Post</button>
</div>
</form>
<div class="flex justify-end pt-2">
<button class="px-4 bg-transparent p-3 rounded-lg text-indigo-500 hover:bg-gray-100 hover:text-indigo-400 mr-2">Action</button>
<button class="modal-close px-4 bg-indigo-500 p-3 rounded-lg text-white hover:bg-indigo-400">Close</button>
</div>
</div>
</div>
</div>
Thank you all and sorry for the inconvenience.

You must use JavaScript or JQuery to pass the data to the modal. For my convenience, I am using JQuery. For JQuery, we need to do something like the following-
In index.blade.php
<button class="modal-open" data-item-foro-id="{{ $item->foro_id }}" data-item-tema-id="{{ $item->tema_id }}"><i class="fas fa-edit"></i></button>
//In Modal
............
<input type="hidden" name="foro_id" id="foro_id" value="#">
<input type="hidden" name="tema_id" id="tema_id" value="#">
............
JQuery Code-
<script>
$(document).on("click", ".modal-open", function () {
var foroId= $(this).attr('data-item-foro-id');
var temaId= $(this).attr('data-item-tema-id');
$("#foro_id").val(foroId);
$("#tema_id").val(temaId);
});
</script>
Note: Here, I am providing an example to pass values to the two input fields. If you get the idea from it, you can pass any value to the modal.

Related

submit prevent doesn't work laravel livewire

i am doing tutorials doing comments website, i have app layout which yield content and register page the view is working fine but the form in register submit prevent doesnt work it just refresh when submitted.
here are the codes
layout app blade php
`--``
<!DOCTYPE html>
<html lang="en">
<head>
...
#livewireStyles
#livewireScripts
<script src="{{asset('js/app.js')}}"></script>
</head>
<body class="flex flex-wrap justify-center bg-blue-100">
<div class="flex w-full justify-between px-4 bg-purple-900 text-white">
<a class="mx-3 py-4" href="/">Home</a>
<div class="py-4">
<a class="mx-3" href="/login">Login</a>
<a class="mx-3" href="/register">Register</a>
</div>
</div>
<div class="my-10 w-full flex justify-center">
#yield('content')
</div>
<script src="https://cdn.jsdelivr.net/gh/livewire/turbolinks#v0.1.x/dist/livewire-turbolinks.js" data-turbolinks-eval="false"></script>
<script type="module">
import hotwiredTurbo from 'https://cdn.skypack.dev/#hotwired/turbo';
</script>
</body>
</html>
register blade php
#section('content')
<div class="my-10 flex justify-center w-full">
<section class="border rounded shadow-lg p-4 w-6/12 bg-gray-200">
<h1 class="text-center text-3xl my-5">SignUp to Get Started</h1>
<hr>
<form class="my-4" wire:submit.prevent="submit">
<div class="flex justify-around my-8">
<div class="flex flex-wrap w-10/12">
<input type="name" class="p-2 rounded border shadow-sm w-full" wire:model="form.name"
placeholder="Name" />
#error('form.name') <span class="text-red-500 text-xs">{{ $message }}</span> #enderror
</div>
</div>
<div class="flex justify-around my-8">
<div class="flex flex-wrap w-10/12">
<input type="email" class="p-2 rounded border shadow-sm w-full" placeholder="Email"
wire:model="form.email" />
#error('form.email') <span class="text-red-500 text-xs">{{ $message }}</span> #enderror
</div>
</div>
<div class="flex justify-around my-8">
<div class="flex flex-wrap w-10/12">
<input type="password" class="p-2 rounded border shadow-sm w-full" placeholder="Password"
wire:model="form.password" />
#error('form.password') <span class="text-red-500 text-xs">{{ $message }}</span> #enderror
</div>
</div>
<div class="flex justify-around my-8">
<div class="flex flex-wrap w-10/12">
<input type="text" class="p-2 rounded border shadow-sm w-full"
placeholder="Confirm Password" wire:model="form.password_confirmation" />
</div>
</div>
<div class="flex justify-around my-8">
<div class="flex flex-wrap w-10/12">
<input type="submit" value="Register" class="p-2 bg-gray-800 text-white w-full rounded tracking-wider cursor-pointer" />
</div>
</div>
</form>
</section>
</div>
#endsection
this is my first time posting, i searched in a lot of places but didnt find the solution.
it worked when i used #livewire('register') in app layout but if i use that the register page is visible in all other pages when app layout is called
I found the solution,
The problem was i am using the livewire 1.0 code which is '#yield('content')' in app layout, i changed that to '{{ $slot }}' and removed '#section('content')' blade code from my views it worked fine
Check the documentation
https://laravel-livewire.com/docs/2.x/upgrading#route-livewire

livewire action not happening when link is clicked

So I have popup which contains a form that shows a table with editable cells using livewire and each row has a delete button, above this table is a button to add a new empty row to the table. Editing in the cells is working (click the cell and it changes to the inline edit input) but clicking the delete link does nothing and neither does the add link.
So for this process I have the following in the client index file which loads the popup modal when the button to edit the client is clicked.
For this question I am looking at why the add isn't working but as for the delete that would be a bonus!
<script>
$(document).ready(function() {
$("#clients tbody").on("click", ".actions", function () {
var data = table.row( $(this).parents('tr') ).data();
// now call ajax on this url
call_ajax( "/client/" + data[0] + "/edit" );
loader.style.display = "block";
modal.style.display = "block";
});
});
function call_ajax( url ) {
// ToDo make an actual ajax call to get the form data
$.get( url )
.done(
function( data ) {
$("#content-region").html( data );
}
)
.fail(
function() {
$("#content-region").html( "<p>error</p>" );
}
);
}
</script>
So using this when a client has its action section clicked then a call to client/{x}/edit is called which is calls my edit.blade.php file:
#extends('layouts.popup')
#section('header')
<i class="fa fa-fw fa-pencil-square-o fa-lg text-success"></i> Edit Client
#stop
#section('content')
<div class="client-editor">
<form class="admin-link" method="post" action="client/save">
#csrf
<input type="hidden" name="id" value="{{ old( 'id', $client->id ) }}">
<div class="max-w-10xl mx-auto py-10 sm:px-6 lg:px-8">
<div class="md:grid md:grid-cols-3 md:gap-6">
<div class="md:col-span-1 flex justify-between">
<div class="px-4 sm:px-0">
<h3 class="text-lg font-medium text-gray-900">Client Edit</h3>
<p class="mt-1 text-sm text-gray-600">
Update the Client title, you can add and remove contacts below.
</p>
</div>
<div class="px-4 sm:px-0">
</div>
</div>
<div class="mt-5 md:mt-0 md:col-span-2">
<div class="px-4 py-5 bg-white sm:p-6 shadow sm:rounded-tl-md sm:rounded-tr-md">
<!-- Eclipse Account Handler -->
<div class="col-span-6 sm:col-span-4">
<label class="block font-medium text-sm text-gray-700" for="client_id">
Client Title
</label>
<input
class="border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 rounded-md shadow-sm mt-1 block w-full"
id="title"
name="title"
type="text"
value="{{ $client->title }}"
>
</div>
<br>
<div class="col-span-6 sm:col-span-4">
<x-toggle
left="No"
right="Yes"
name="is_enabled"
label="Is this client active?"
colourleft="#805050"
colourright="#508050"
default="{{ $client->is_enabled }}"
/>
</div>
</div>
<div class="hidden sm:block">
<div class="py-8">
<div class="border-t border-gray-200"></div>
</div>
</div>
</div>
</div>
</div>
<!-- client contacts -->
<div class="max-w-10xl mx-auto py-10 sm:px-6 lg:px-8">
<div class="md:grid md:grid-cols-3 md:gap-6">
<div class="md:col-span-1 flex justify-between">
<div class="px-4 sm:px-0">
<h3 class="text-lg font-medium text-gray-900">Client Contacts</h3>
<p class="mt-1 text-sm text-gray-600">
Enter details for client contacts assigned to this client.
</p>
</div>
</div>
<div class="mt-5 md:mt-0 md:col-span-2">
<div class="px-4 py-5 bg-white sm:p-6 shadow sm:rounded-tl-md sm:rounded-tr-md">
#livewire("contacts-table", [ 'client' => $client ])
</div>
<div
class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6 shadow sm:rounded-bl-md sm:rounded-br-md">
<button
type="submit"
class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring focus:ring-gray-300 disabled:opacity-25 transition"
>
Save
</button>
</div>
<div class="hidden sm:block">
<div class="py-8">
<div class="border-t border-gray-200"></div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
#stop
and from in this a call of #livewire("contacts-table", [ 'client' => $client ]) and this are it's parts.
ContactsTable.php
<?php
namespace App\Http\Livewire;
use App\Models\Client;
use App\Models\ClientContact;
use Livewire\Component;
class ContactsTable extends Component
{
protected $listeners =['refresh' => '$refresh'];
public Client $client;
public $clientContacts;
public function mount( Client $client )
{
$this->clientContacts = ClientContact::where( 'client_id', $client->id )->get();
}
public function render()
{
return view('livewire.contacts-table');
}
public function addNewContact()
{
die('Test that we got here!'); // I actually have a breakpoint here.
}
}
and contacts-table.blade.php:
<div class="col-span-6 sm:col-span-4">
<a
class="inline-flex items-center px-4 py-2 bg-gray-800 dark:bg-gray-100 dark:text-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring focus:ring-gray-300 disabled:opacity-25 transition"
wire:click="addNewContact"
>
{{ __('Add') }}
</a>
<table id="contacts" class="table table-striped table-bordered table-hover">
<thead>
<td>Name</td>
<td>Email</td>
<td>Phone</td>
<td>Action</td>
</thead>
<tbody>
#foreach( $clientContacts as $contact )
<tr id="{{ $contact->id }}">
<td>
#livewire( 'inline-edit', ['item' => $contact, 'type' => 'title', 'number' => $client->id] )
</td>
<td>
#livewire( 'inline-edit', ['item' => $contact, 'type' => 'email', 'number' => $client->id] )
</td>
<td>
#livewire( 'inline-edit', ['item' => $contact, 'type' => 'phone_number', 'number' => $client->id] )
</td>
<td class="width-8">
<a
class="cursor-pointer"
wire:click="$emit( 'openModal', 'delete-contact',{{ json_encode(['contact_id' => $contact->id]) }} )"
>
<i class="fas fa-trash"></i>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
<p class="mt-1 text-sm text-gray-600">
Edit the cells above, click the bin to delete or the + to add a new row to populate with a new contact.
</p>
</div>
for completeness here is the popup layout blade file:
<!-- Page Heading-->
<header class="bg-white shadow">
<div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
#yield('header')
</h2>
</div>
</header>
<!-- Page Content -->
<main>
#if (Session::has('message'))
<!-- will be used to show any messages -->
<div class="alert alert-info">{{ Session::get('message') }}</div>
#endif
<div class="py-12">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
#yield('content')
</div>
</div>
</main>
<!-- End Popup -->
Just to reiterate, the popup form is loading and the content in it is rendered correctly. The top part of the form works so the client is updated.
The inline edit livewire is working (well kind of as I've yet to implement the actual save of it's data but that isn't my issue.
The add button does nothing and nothing is shown in my dev toolbar (no network activity and nothing in the console tab) this is also the case with the delete buttons (which i have working elsewhere in the app - it should show a confirm box and if yes it then removes the contact.
thanks
*** EDIT ***
As an afterthought I have tried this with the client table commented ( the entire table in contacts-table.blade.php) so it just has the add link but it still doesn't work.
*** MORE INFO ***
I have tried removing from the popup and it works this way. Not what I wanted but It does work this way. Why would it not work in a popup?

Livewire file upload validation always fails the first time but works the second time

Weird issue with my Livewire: the validation, when I send a file, always fails on the first time. If I have the criteria that it must be a pdf, I will put the PDF, submit the form, get a message telling me I have the wrong format, I won't change anything, resubmit and it will go through with no issue.
Does anyone knows why it might be happening?
<?php
namespace App\Http\Livewire\Branch\Documents;
use App\Models\BranchDocument;
use Livewire\Component;
use Livewire\WithFileUploads;
class Upload extends Component
{
use WithFileUploads;
public BranchDocument $document;
public $file;
public $saved = false;
public function render()
{
return view('livewire.branch.documents.upload');
}
public function save() {
$this->validate([
'file' => 'mimes:jpg,bmp,png,pdf|max:10240', // 1MB Max
]);
$this->document->file = $this->file;
$this->saved = true;
}
}
<div x-data="{ open: false }">
<li class="#if(!$saved) hover:bg-gray-100 py-2 px-2 rounded cursor-pointer #endif" #click="open = true">
<div class="relative">
<div class="relative flex space-x-3">
<div class="min-w-0 flex-1 flex justify-between space-x-4 items-center">
<div>
<p class="text-sm text-porange">{{ $document->document_type->title }}#if($saved)<span class="text-sm text-gray-600"> - {{ __('Document sent!') }}</span>#endif</p>
<p class="text-xs text-gray-600">{{ $document->created_at->diffForHumans() }}</p>
</div>
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-gray-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" >
#if(!$saved)
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12" />
#else
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
#endif
</svg>
</div>
</div>
</div>
</li>
#if(!$saved)
<div x-show="open" class="fixed z-10 inset-0 overflow-y-auto" aria-labelledby="modal-title" role="dialog" aria-modal="true">
<div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
<!--
Background overlay, show/hide based on modal state.
Entering: "ease-out duration-300"
From: "opacity-0"
To: "opacity-100"
Leaving: "ease-in duration-200"
From: "opacity-100"
To: "opacity-0"
-->
<div class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" aria-hidden="true"></div>
<!-- This element is to trick the browser into centering the modal contents. -->
<span class="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">​</span>
<!--
Modal panel, show/hide based on modal state.
Entering: "ease-out duration-300"
From: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
To: "opacity-100 translate-y-0 sm:scale-100"
Leaving: "ease-in duration-200"
From: "opacity-100 translate-y-0 sm:scale-100"
To: "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
-->
<div #click.away="open = false" class="inline-block align-bottom bg-white rounded-lg px-4 py-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full sm:p-6">
<div>
<div class="text-center">
<h3 class="text-lg leading-6 font-medium text-gray-900" id="modal-title">
{{ __('Upload ":title"', ['title' => $document->document_type->title])}}
</h3>
</div>
</div>
<form wire:submit.prevent="save">
<input type="file" wire:model="file" class="file-input-business block mx-auto mt-4"/>
#error('file') <span class="error">{{ $message }}</span> #enderror
<div class="mt-5 sm:mt-6 sm:grid sm:grid-cols-2 sm:gap-3 sm:grid-flow-row-dense">
<button type="submit" type="button" class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-porange text-base font-medium text-white hover:bg-porange-darker focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-porange sm:col-start-2 sm:text-sm">
{{ __('Send')}}
</button>
<button #click="open = false" type="button" class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-porange sm:mt-0 sm:col-start-1 sm:text-sm">
{{ __('Cancel')}}
</button>
</div>
</form>
</div>
</div>
</div>
#endif
</div>
Thank you!
Is the file uploaded before you hit submit? If not then the file is null hence the wrong format error. Check its value in the save method before validation. You can also use wire.loading on a div to determine if the file is still uploading.
You may also run into issues with built in artisan development server (the one you start with php artisan serve command). It has different limitations to a 'real' server - upload file size, formats, etc.

Not able to produce action upon clicking the button

I am using the below code from Tailblocks Link under CTA section , upon clicking the button I am not able to get to new page. In the below code form tags are added by me. please guide how can I resolve it?
<form ction="index.php" method="post">
<section class="text-gray-600 body-font">
<div class="container px-5 py-24 mx-auto flex flex-wrap items-center">
<div class="lg:w-3/5 md:w-1/2 md:pr-16 lg:pr-0 pr-0">
<h1 class="title-font font-medium text-3xl text-gray-900">Slow-carb next level shoindcgoitch ethical authentic, poko scenester</h1>
<p class="leading-relaxed mt-4">Poke slow-carb mixtape knausgaard, typewriter street art gentrify hammock starladder roathse. Craies vegan tousled etsy austin.</p>
</div>
<div class="lg:w-2/6 md:w-1/2 bg-gray-100 rounded-lg p-8 flex flex-col md:ml-auto w-full mt-10 md:mt-0">
<h2 class="text-gray-900 text-lg font-medium title-font mb-5">Sign Up</h2>
<div class="relative mb-4">
<label for="full-name" class="leading-7 text-sm text-gray-600">Full Name</label>
<input type="text" id="full-name" name="full-name" class="w-full bg-white rounded border border-gray-300 focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200 text-base outline-none text-gray-700 py-1 px-3 leading-8 transition-colors duration-200 ease-in-out">
</div>
<div class="relative mb-4">
<label for="email" class="leading-7 text-sm text-gray-600">Email</label>
<input type="email" id="email" name="email" class="w-full bg-white rounded border border-gray-300 focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200 text-base outline-none text-gray-700 py-1 px-3 leading-8 transition-colors duration-200 ease-in-out">
</div>
<button class="text-white bg-indigo-500 border-0 py-2 px-8 focus:outline-none hover:bg-indigo-600 rounded text-lg">Button</button>
<p class="text-xs text-gray-500 mt-3">Literally you probably haven't heard of them jean shorts.</p>
</div>
</div>
</section>
</form>
You form action attribute is having a typo.
<form action="index.php" method="post">
Also, instead of button you need to select the input type as submit.
<input type='submit' class="text-white bg-indigo-500 border-0 py-2 px-8 focus:outline-none hover:bg-indigo-600 rounded text-lg" value="Button">
else add some JS to submit the form
<script>
var forms = document.getElementsByTagName('form');
for(var i = 0; i < forms.length; i += 1) {
forms[i].addEventListener('submit', function(e) {
e.preventDefault();
}, true);
}
</script>
You have a typo error in action attribute in your form tag.
<form action="index.php" method="post">
Also you don't add the type attribute to your button tag.
<button type="submit" class="text-white bg-indigo-500 border-0 py-2 px-8 focus:outline-none hover:bg-indigo-600 rounded text-lg">Button</button>
I hope this will fix your problem.

I want the modal to open with the correct ID i.e. in the foreach

I have a problem: I used a foreach to list a database collection with a button to open a modal in each one. The button works correctly, but the ID that passes into the modal does not. If the modal code is inside the foreach, in all options the first id always appears, if it is outside the foreach the last ID always appears.
<div class="row">
#if ($trainingphases == null)
<p>null</p>
#else
<div class="col-md-6">
<div class="card mt-1">
<div class="card-body">
<!-- right control icon -->
#foreach ($trainingphases as $trainingphase)
<div class="accordion" id="accordionRightIcon">
<div class="card ">
<div class="card-header header-elements-inline">
<h6 class="card-title ul-collapse__icon--size ul-collapse__right-icon mb-0">
<a data-toggle="collapse" class="text-default collapsed" href="#accordion-item-icon-right-{!!$trainingphase->id_trainingphases!!}"
aria-expanded="false">{!! $trainingphase->title !!}</a>
</h6>
</div>
<div id="accordion-item-icon-right-{!!$trainingphase->id_trainingphases!!}" class="collapse" data-parent="#accordionRightIcon" style="">
<div class="card-body">
<textarea class="ckeditor form-control" placeholder="teste" name="description">{{ $trainingphase->description }}</textarea>
</div>
</div>
<div class="col-md-2">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">editar {!! $trainingphase->title !!}</button>
</div>
</div>
</div>
#endforeach
<!-- /right control icon -->
</div>
</div>
</div>
#endif
</div>
<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<form method="POST" action="{{ route('create.trainingphase', array('id_trainings'=>$training->id_trainings)) }}">
#csrf
<h5 class="modal-title" id="exampleModalCenterTitle"><input type="text" class="form-control form-control-rounded" id="title" placeholder="{!! $trainingphase->title !!}" name="title"></h5>
</div>
<div class="modal-body">
<label for="descriptiom">Descrição</label>
<textarea class="ckeditor form-control" placeholder="teste" name="description">{{ $trainingphase->description }}</textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
Explaination
This is the expected behaviour of a loop within PHP.
So your code will evaluate from top to bottom and within your #foreach loop you are essentially re-assigning the value of $trainingphase within the scope of the script.
Once the loop is finished, the value of $trainingphase is not unset so it will be whatever the last value was in the object you had just looped through.
Solution
Keep the modal code outside of the loop and try using data properties on the buttons you are using to open the modal to pass values into the modal itself.
Documentaion on this can be found here:
https://getbootstrap.com/docs/4.0/components/modal/#varying-modal-content

Categories