Laravel Form Looping Not Rendering Completely - php

My View
<tbody>
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
<td>{{ $category->slug }}</td>
<td>{{ ($category->TermTaxonomy ? $category->TermTaxonomy->description : '') }}</td>
<td>
{{ Form::open(['method' => 'DELETE', 'route' => ['admin_posts_categories_destroy', $category->term_id]]) }}
{{ Form::submit('Delete'); }}
{{ Form::close() }}
</td>
</tr>
#endforeach
</tbody>
then result in inspect element
so i can't delete the first row , but i can delete the other , why this things happen ? and how to fix it. already try in the other browser and still same.

Never mind , in have 2 form in my view the first is static form to add term, the second is loop form for restful delete, and missing {{ Form::close() }} in the first form (static) , so just put {{ Form::close() }} in the first form (static) and both form static and looping work like a charm. thanks for all.

Related

Symfony - creation of a "table" form

I am under symfony 5, on an application already in production on which I would like to add a screen allowing to modify several lines of the same table at once, in the same "POST" message, with one and the same "send" button.
The different fields must be contained inside a single form tag:
<form name="modif_todo_liste" method="post">
...
</form>
I tried different things including creating a table with different views based on the same template form:
foreach($todos as $v){
$form_tab[]=$this->createForm(ModifTodoListeType::class,$v)->createView();
}
that I render in twig:
{% for form in form_tab %}
{{ form_start(form) }}
<tr>
<td>{{ form_row(form.Denomination) }}</td>
<td>{{ form_row(form.Nbr) }}</td>
<td>{{ form_row(form.CIS) }}</td>
<td>{{ form_row(form.Label) }}</td>
<td>{{ form_row(form.BN_Label) }}</td>
<td>{{ form_row(form.ATC7) }}</td>
<td>{{ form_row(form.Categorie) }}</td>
</tr>
{{ form_end(form) }}
{% endfor %}
visually this corresponds to what I would like, but impossible to validate everything at once because, it is all the lines are not included in the same form:
Does anyone know of an easy way to make this form that will be "flushable" in the controller afterwards?
Otherwise I will do a more classic solution, I pass an array containing my entire table to twig, in the view I build a form by looping over the array. And in the controller I will take care of the update.
Thanks for your help
You can create the whole form containing different fields using Symfony forms.
You can even map the attributes to the entity directly, so the update is taken care of on the handleRequest method called on the form. (the entity is updated, you still need to save it to the database)
Symfony forms docs
If the fields are dependent on some array in the controller, you can also build the form in the controller like here.
Try this instead:
{{ form_start(form) }}
{% for form in form_tab %}
<tr>
<td>{{ form_row(form.Denomination) }}</td>
<td>{{ form_row(form.Nbr) }}</td>
<td>{{ form_row(form.CIS) }}</td>
<td>{{ form_row(form.Label) }}</td>
<td>{{ form_row(form.BN_Label) }}</td>
<td>{{ form_row(form.ATC7) }}</td>
<td>{{ form_row(form.Categorie) }}</td>
</tr>
{% endfor %}
{{ form_end(form) }}

Laravel Parameter passing error from controller to view

I want to pass the parameter $questions to view, but it gives the following error:
ErrorException (E_ERROR)
Undefined variable: questions (View: C:\Users\Krishan\Documents\GitHub\GroupProject\lcurve\resources\views\quizz\questions\index.blade.php)
This is my controller index function part:
public function index()
{
$questions = Question::all();
return view('quizz/questions.index', compact('questions'));
}
This is a part Of my view:
<tbody>
#if (count($questions_options) > 0)
#foreach ($questions_options as $questions_option)
<tr data-entry-id="{{ $questions_option->id }}">
<td></td>
<td>{{ $questions_option->question->question_text or '' }}</td>
<td>{{ $questions_option->option }}</td>
<td>{{ $questions_option->correct == 1 ? 'Yes' : 'No' }}</td>
<td>
View-->
<!--Edit-->
{!! Form::open(array(
'style' => 'display: inline-block;',
'method' => 'DELETE',
'onsubmit' => "return confirm('".trans("quickadmin.are_you_sure")."');",
'route' => ['questions_options.destroy', $questions_option->id])) !!}
{!! Form::submit(trans('quickadmin.delete'), array('class' => 'btn btn-xs btn-danger')) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endif
</tbody>
enter image description here
where is questions_options coming from? You are passing questions. So your for loop should be
#if (count($questions) > 0)
#foreach ($questions as $question)
//rest of your code
#endforeach
#endif
and your return view part can be return view(quizz.questions.index, compact('questions'))
Firstly, the error message you have mention should be shown. The error message should be:
Undefined variable: questions_options (View:C:\Users\Krishan\Do........
Because you are passing questions to view but you are accessing question_options in view. So, it should say question_options in undefined in view.
Besides, do you know you can avoid this count check? You can use laravel's forelse tag here a below:
#forelse($questions as $question)
//Your table goes here
#empty
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endforelse

Laravel check if a specific item is present in a table

I have the following relationships
a movie has many episodes
an user has a watchlist
a watchlist has many movies
I get my episodes with eloquent
$latestshows = episode::with('movies') ->where('category', 'tvshow')->take(10) ->get();
I then display it with
#foreach($latestshows as $show)
#if (Auth::guest())
<tr>
#else
<tr class="{{ Auth::user()-> watchlist **...** ? 'alert-danger' : '' }}">
#endif
<td>{{ $show->movie->title }}</td>
<td>{{ $show->number }}</td>
<td>{{ $show->created_at }}</td>
</tr>
#endforeach
How do I check if the logged in used has the show in watchlist? I want to display it with a different color in that case.
Sorry if this is a silly question, I'm just a beginner and experimenting.
You can use in_array or array_diff (Both native PHP functions) to see which movies are the same, or you can use the ->diff() or ->has() methods from the Collection objects that you are dealing with.
<td>
<a ... {{ Auth::user()->watchlist->movies->find($show->movies->id) ? 'alert-danger' : '' }}>
{{ $show->movie->title }}
</a>
</td>
This code gave me the expected result
{{ Auth::user()->watchlist->movies->find($show->movies->id) ? 'alert-danger' : '' }}
Thank you Oscar, you were my inspiration :P

Blade template and radio buttons - select first in foreach loop

I have the following Blade template entry, which creates (as part of a table row) a column of radio buttons.
I want to have only the first radio genereated selected, and I want to do this via the PHP, no js post page load.
How do I check if this is the "first" entry in my collection and thus place the string checked as an attribute in the HTML? My latest code permutation is below, but it does not work.
#foreach ($organisationsOwned as $organisationOwned)
<tr class="organisations-table">
<td><input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" #if ($organisationsOwned{0}) {!! "checked" !!} #endif></td>
<td>{!! $organisationOwned->org_title !!}</td>
<td>{!! $organisationOwned->plan_title !!}</td>
<td style="text-align: center;">{!! currency_format($organisationOwned->gst_amount) !!}</td>
<td style="text-align: right;">{!! $organisationOwned->subscription_ends_at !!}</td>
</tr>
#endforeach
More precisely, here's the line for the input:
<input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" #if ($organisationsOwned{0}) {!! "checked" !!} #endif>
I have tried variations such as:
{{ head($organisationsOwned) ? checked : '' }}>
and
{{ $organisationsOwned{0} ? checked : '' }}>
and even this suggestion shown as a working model of my question:
#if ($organisationOwned == reset($organisationsOwned)) checked #endif
Thanks and happy new year!
If you did not specify keys for the elements of $organisationsOwned you can use the element's index to determine if it's the first one:
#foreach($organisationsOwned as $index => $organisationOwned)
...
<td><input type="radio" name="organisations" id="{!! $organisationOwned->id !!}" #if (!$index) {!! "checked" !!} #endif></td>
...
#endforeach

Symfony2 DATEDIFF function

I need to fill table in TWIG with data from database. Everything is Fine with the exception of this:
I need to have column with DATEDIFF property to get number of days.
TODAY-dateFromDateBase
Question is:
How to get number of days in loop in twig?
here is my twig:
<table>
<thead>
<tr>
<form action="" method="post" {{ form_enctype(searchform) }} class="form-index-permits">
<td>L.p </td>
<td>ID PRZEPUSTKI {{ form_widget(searchform.PermitId) }}</td>
<td>Name{{ form_widget(searchform.Permitname) }}</td>
<td>Surname {{ form_widget(searchform.Permitsurname) }}</td>
<td>Company {{ form_widget(searchform.Company) }}</td>
<td>GW {{ form_widget(searchform.Contractor) }}</td>
<td>Dayleft {{ form_widget(searchform.Dayleft) }}</td>
<td>End date {{ form_widget(searchform.date, { 'attr': {'class': 'datepicker'} }) }}</td>
</form>
</tr>
</thead>
{% for permit in permitcollection %}
<tbody>
<td>{{ loop.index }}</td>
<td>{{ permit.getPermitid()|number_format(0, '.', ' ') }}</td>
<td>{{ permit.getPermitname() }}</td>
<td>{{ permit.getPermitsurname() }}</td>
<td>{{ permit.getPermitsCompany().getName() }}</td>
<td>{{ permit.getPermitsContractor().getName() }}</td>
<td> HERE I WANT TO DISPLAY DAYS LEFT</td>
<td>{{ permit.getExpirationdate()|date('Y-m-d') }}</td>
</tbody>
{% endfor %}
</table>
Is something like this possible?
{{ permit.getExpirationdate()|date('Y-m-d') - "now"|date('Y-m-d') }}
First Solution (recommended) "Use an existing library":
You can use the KnpTimeBundle
In the Twig:
This compare with the current date:
{# Returns something like "3 minutes ago" #}
{{ time_diff(permit.expirationDate) }}
This compare with the another date:
{# Returns something like "3 minutes ago" #}
{{ time_diff(permit.expirationDate, anotherDate) }}
Second Solution "Do it yourself":
Make diff via php function:
$calcFrom = permit.getExpirationdate()
$now = new \DateTime('now');
$now->diff($calcFrom)->format("%a")
And make it available via a Twig extension or directly in an helper method in the entity.
Another possible solution is to write register a custom DQL Function to do the work in the repository
Hope this help

Categories