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
Related
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
Fellow coders,
It might be a stupid question but I really am stuck on this part in my application.
I am making an hourregistration system for the company i'm an intern at. What I have done is creating a delete button in my application that deletes a record with the click of a simple button. Yet what I did was to delete all the visible records in the table I created that shows all the registrations.
When I deleted the final record I got an error of "undefined index hourregistration".
public function index()
{
$hoursregistrations = Hoursregistration::latest()->get();
$user_id = Sentinel::getUser();
$project_id = Project::pluck('description', 'id');
$company_name = Company::where('status','client')->pluck('company_name', 'id');
$activity_name = Subproject::pluck('id');
//dd($hoursregistrations);
return view('hoursregistrations.index', compact('hoursregistrations', 'user_id', 'project_id',
'activity_name', 'company_name'));
}
I think the problem lies at
$hoursregistrations = Hoursregistration::latest()->get();
Because I'm trying to get the latest value of the registration but there is none right?
Now i'm wondering how I could still show my view without breaking my inserting and/or viewing portion of the app.
#foreach ($hoursregistrations as $hoursregistration)
<tr>
<td hidden>{{ $user_id->first_name }}</td>
<td >{!! App\Project::getCompanyName($hoursregistration->project_id) !!} - {!! \App\Subproject::getTaskTitle($hoursregistration->subproject_id)!!}</td>
<td>{!! $hoursregistration->note !!}</td>
<td>{!! \App\Helpers::dateFormat($hoursregistration->date) !!}</td>
<td>{!! $hoursregistration->hours !!}</td>
<td>
<button id="btn-edit" name="btn-edit" class="btn btn-warning btn-xs btn-detail open-modal" value="{{$hoursregistration->id}}">Edit</button>
<form action="hoursregistrations/{{ $hoursregistration->id }}/delete" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button id="btn-delete" type="submit" name="btn-delete" class="btn btn-danger btn-xs btn-delete delete-hoursregistration" value="{{ $hoursregistration->id }}">Delete</button>
</form>
</td>
</tr>
#endforeach
This is the foreach loop that shows the data in the index.blade.php view
I would love some help so I can continue finishing this application
public function index()
{
$hoursregistrations = Hoursregistration::latest()->get();
$user_id = Sentinel::getUser();
$project_id = Project::pluck('description', 'id');
$company_name = Company::where('status','client')->pluck('company_name', 'id');
$activity_name = Subproject::pluck('id');
//dd($hoursregistrations);
return view('hoursregistrations.index', compact('hoursregistrations', 'user_id', 'project_id',
'activity_name', 'company_name'));
}
Everything is about s or not s.
The error you are talking about is because you have an occurrence of $hoursregistration (without s) before or after the #foreach of your view. The solution is to check and remove occurrence of $hoursregistration outside your loop:
// You can't use $hoursregistration before
#foreach ($hoursregistrations as $hoursregistration)
{{-- ... do what you want with $hoursregistration --}}
#endforeach
// You can't use $hoursregistration after
I am sure there is a confusion (typo) between $hoursregistration, $hoursregistrations and $hourregistration (the confusion is even in your post VS comment)
you can use if here if $hoursregistrations exists then it populate data else nothing happen
#if($hoursregistrations)
#foreach ($hoursregistrations as $hoursregistration)
<tr>
<td hidden>{{ $user_id->first_name }}</td>
<td >{!! App\Project::getCompanyName($hoursregistration->project_id) !!} - {!! \App\Subproject::getTaskTitle($hoursregistration->subproject_id)!!}</td>
<td>{!! $hoursregistration->note !!}</td>
<td>{!! \App\Helpers::dateFormat($hoursregistration->date) !!}</td>
<td>{!! $hoursregistration->hours !!}</td>
<td>
<button id="btn-edit" name="btn-edit" class="btn btn-warning btn-xs btn-detail open-modal" value="{{$hoursregistration->id}}">Edit</button>
<form action="hoursregistrations/{{ $hoursregistration->id }}/delete" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button id="btn-delete" type="submit" name="btn-delete" class="btn btn-danger btn-xs btn-delete delete-hoursregistration" value="{{ $hoursregistration->id }}">Delete</button>
</form>
</td>
</tr>
#endforeach
#endif
i think this might help
I try to load a view(project_template) in an other view.
My problem ist, how i can give my $project-variable to the 'project_template'-view which i create in my main-view with "View:make".
Actually i use
->with($project)
But when I try to use it in my template_view, i get an non-object-error
I also used "compact" what also not worked for me.
In my main-view i can use the $project-variable without problems
View:
#foreach($projects as $project)
<tr class="gradeA">
<td>
<td>{!! $project->description !!}</td>
<td>
{!! View::make('shared.tables.project_template')->with($project)->render() !!}
</td>
</tr>
#endforeach
Project_Template-View:
{!! $project->project_name !!}
Error-Message
ErrorException in 0f9dcdc70fd0be3b50d7a39ad7bc90d7 line 4:
Trying to get property of non-object
Thanks for the help!
Change to this:
#foreach($projects as $project)
<tr class="gradeA">
<td>
<td>{!! $project->description !!}</td>
<td>
#include('shared.tables.project_template')
</td>
</tr>
#endforeach
Simply use the #include(viewName, [data]).
That means, in your case:
#include('shared.tables.project_template', ['project' => $project])
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
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.