This question already has answers here:
How to stop a loop PHP
(4 answers)
Short circuit Array.forEach like calling break
(30 answers)
How to break a foreach loop in laravel blade view?
(5 answers)
Closed 3 years ago.
I'm setting up a loop whenever i tried to print table first row print good with category name and then next row print table with ONE tr(row) data and closed the tag and again start new row and print next data with one row...
i want to print all data with respect to its category name without break multiple table
#if( ! empty($packages) )
#php
$serviceId=null;
#endphp
#foreach($packages as $package)
#if(is_null($serviceId))
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $package->service->name }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId=$package->service->id
#endphp
#endif
#if($serviceId != $package->service_id)
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $package->service->name }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId = $package->service_id
#endphp
#endif
<!--SERVICE NAME TABLE-->
<div class="row">
<div class="col-sm-12">
<div class="well">
<div class="table-responsive">
<table class="table table-striped table-sm ">
<thead>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ $package->id }}</td>
<td>{{ $package->name }}</td>
<td>#php $price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_formats(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}</td>
<td>{{ $package->minimum_quantity }}</td>
<td>{{ $package->maximum_quantity }}</td>
<td>{{ $package->description }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
!--SERVICE NAME TABLE-->
#endforeach
#else
No Record Found
#endif
You can create an extra array to map this,
In method
$packagesNew = [];
foreach($packages as $k => $v)
{
$packagesNew[$v->service->name]['name'] = $v->service->name;
$packagesNew[$v->service->name]['id'] = $v->service->id;
$packagesNew[$v->service->name]['service_id'] = $v->service_id;
$packagesNew[$v->service->name]['data'][] = $v;
}
pass packagesNew to view
Then I made changes in view, please check.
#if( ! empty($packagesNew) )
#php
$serviceId=null;
#endphp
#foreach($packagesNew as $k => $package)
#if(is_null($serviceId))
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $k }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId=$package['id'];
#endphp
#endif
#if($serviceId != $package['service_id'])
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $k }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId = $package['service_id'];
#endphp
#endif
<!--SERVICE NAME TABLE-->
<div class="row">
<div class="col-sm-12">
<div class="well">
<div class="table-responsive">
<table class="table table-striped table-sm ">
<thead>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
</thead>
<tbody>
#foreach($package['data'] as $k1 => $v1)
<tr>
<td>{{ $v1->id }}</td>
<td>{{ $v1->name }}</td>
<td>#php $price = isset($userPackagePrices[$v1->id]) ? $userPackagePrices[$v1->id] : $v1->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_formats(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}</td>
<td>{{ $v1->minimum_quantity }}</td>
<td>{{ $v1->maximum_quantity }}</td>
<td>{{ $v1->description }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
!--SERVICE NAME TABLE-->
#endforeach
#else
No Record Found
#endif
Related
just wondering how to format this output for a better view? (second picture). as you can see in the first picture the output from pluck('name') was like {Tidak Masuk Sekolah} {Terlambat}. check the pictures below
and this is my view, I'm using table
<div class="card-body">
<h2 class="display-4 text-center">{{ $title }}</h2>
<div class="table-responsive d-flex justify-content-center">
<table class="table mx-9 my-6 table-borderless">
<tr>
<td>Nama Siswa</td>
<td>{{ $counseling->student->name }}</td>
</tr>
<tr>
<td>Kelas</td>
<td>{{ $counseling->student->student_class->name }}</td>
</tr>
<tr>
<td>Masalah yang dihadapi</td>
#foreach ($counseling->problems->pluck('name') as $problem_name) {
<td>{{ $problem_name }}</td>
}
#endforeach
</tr>
</table>
</div>
</div>
You could use an ol element to achieve that look.
<tr>
<td>
<p>Masalah yang dihadapi</p>
<ol>
#foreach ($counseling->problems->pluck('name') as $problem_name)
<li>{{ $problem_name }}</li>
#endforeach
</ol>
</td>
</tr>
Or
<tr>
<td>Masalah yang dihadapi</td>
<td>
<ol>
#foreach ($counseling->problems->pluck('name') as $problem_name)
<li>{{ $problem_name }}</li>
#endforeach
</ol>
</td>
</tr>
Depending on where you want the list to appear
The Routes
Route::get('/adminContacts/{user_id}', 'AdminContactsController#index')->name('adminContacts.index')->middleware('is_admin');
Route::get('/adminContacts/{user_id}/create', 'AdminContactsController#create')->name('adminContacts.create')->middleware('is_admin');
adminContactController
public function index($user_id)
{
// Confirm User exists
User::findOrFail($user_id);
$filter = request('filter', NULL);
$contacts = NULL;
if ($filter == NULL)
$contacts = Contacts::query()->where('owner_id', $user_id)->sortable()->paginate(5);
else
$contacts = Contacts::query()->where('owner_id', $user_id)->where('name', 'like', '%'.$filter.'%')
->orWhere('number', 'like', '%'.$filter.'%')
->sortable()->paginate(5);
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create($user_id)
{
return view('adminContacts.create')->withUserId($user_id);
}
index.blade.php
<div class="container">
<h1 class="jumbotron">Sample Phone Book</h1>
Add Contact
</div>
<!--Search Field -->
<div class="container">
<form method="GET" action="{{ route('adminContacts.index') }}">
<input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
#if (request('filter'))
<a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index') }}">X</a>
#endif
</form>
</div>
<!-- Table -->
<div class="container">
<div class="row" >
<table class="table table-hover" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>#sortablelink('name', 'Contact Name')</th>
<th>#sortablelink('number', 'Phone Number')</th>
<th>
<button class="btn btn-primary btn-sm">Prepend</button>
</th>
</tr>
</thead>
<!--Loop through all the cutomers and output them on the table-->
#foreach($contacts as $contact)
<tbody>
<tr>
<td>{{ $contact->id }}</td>
<td>{{ $contact->name }}</td>
<td>{{ $contact->number }}</td>
<td>
View
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $contacts->appends(\Request::except('page'))->render() !!}
</div>
</div>
The error is:
Undefined variable: user_id (View: D:\laragon\www\SampleContacts\resources\views\adminContacts\index.blade.php)
Am I missing something? The idea is to pass the $user_id to the adminContact.create form where i can use it to set
$contact->owner_id = $user_id;
so the entered contact apears under that users table.
I don't think you can pass variables into your view like you're doing now.
In your controller, try changing this line:
return view('adminContacts.index')->withContacts($contacts)->withUserId($user_id);
To this:
return view('adminContacts.index')->with('contacts', $contacts)->with('user_id', $user_id);
You could explicitly name the variable when passing it to the view, either this way
return view('adminContacts.index')->with('user_id', $user_id);
Or
return view('adminContract.index', compact('user_id'));
I'm not sure what the name of the variable becomes when passed to the view via withUserId, but I'm guessing this is the issue.
Found the problem.
#extends('layouts.app')
#section('content')
<div class="container">
<h1 class="jumbotron">Sample Phone Book</h1>
Add Contact
</div>
<!--Search Field -->
<div class="container">
<form method="GET" action="{{ route('adminContacts.index', $user_id) }}">
<input type="text" name='filter' class='input' placeholder='Search' value="{{ request('filter') }}">
#if (request('filter'))
<a class="btn btn-primary btn-sm" href="{{ route('adminContacts.index', $user_id) }}">X</a>
#endif
</form>
</div>
<!-- Table -->
<div class="container">
<div class="row" >
<table class="table table-hover" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>#sortablelink('name', 'Contact Name')</th>
<th>#sortablelink('number', 'Phone Number')</th>
<th>
<button class="btn btn-primary btn-sm">Prepend</button>
</th>
</tr>
</thead>
<!--Loop through all the cutomers and output them on the table-->
#foreach($contacts as $contact)
<tbody>
<tr>
<td>{{ $contact->id }}</td>
<td>{{ $contact->name }}</td>
<td>{{ $contact->number }}</td>
<td>
View
</td>
</tr>
</tbody>
#endforeach
</table>
{!! $contacts->appends(\Request::except('page'))->render() !!}
</div>
</div>
#endsection
Anywhere where there is a {{ route('adminContacts.index') }} it also needed the $user_id to be passed along with it because I'm also using it in the routes as '/adminContacts/{user_id}'
Thank you for all the help.
Thank you for all the help in the past.
On my app, when i try to view a certain blade, it returns the error
"Trying to get property of non-object".
It has pointed me to the blade and the section of code where the error comes from:
#extends('layouts.app')
#section('content')
#component('dashboard.frame')
<div class="panel panel-default">
<div class="panel-heading">
Create
Activation Pins - {{ $item->name }}
</div>
{{--<div class="panel-body">--}}
{{--#if($pin_item && !$pin_item->pivot->is_paid )--}}
{{--<form action="/activation-pins/{{ $pin_item->id }}" method="post">--}}
{{--<input type="hidden" name="_method" value="PUT">--}}
{{--<input type="hidden" name="vendor_id" value="{{ $item->vendor->id }}">--}}
{{--{!! csrf_field() !!}--}}
{{--<button type="submit" class="btn btn-success">Mark As Paid</button>--}}
{{--</form>--}}
{{--#endif--}}
{{--</div>--}}
<table class="table">
<thead>
<tr>
<th>S/N</th>
<th>Name</th>
<th>Is Active</th>
<th>User</th>
</tr>
</thead>
<tbody>
#forelse($item->activation_pins as $itm)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $itm->pin }}</td>
<td>
<span class="label label-{{ $itm->is_active ? ( $itm->is_valid ? 'info' : 'danger' ) : 'warning' }}">
{{ $itm->is_active ? ( $itm->is_valid ? 'active' : 'expired' ) : 'inactive' }}
</span>
</td>
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
</tr>
#empty
<tr>
<td colspan="6">
<p class="text-center">No Activation Pins</p>
</td>
</tr>
#endforelse
</tbody>
</table>
</div>
#endcomponent
#endsection
With emphasis on:
<td>
{{ $itm->user_id ? $itm->user->email : '-' }}
</td>
Could I be missing something? Thanks.
This is what it displays now:
My pagination works correctly, but when use search component it display only first page of result.
My page URL without search looks like:
http://localhost:8000/dictionary-management/postcode?page=2
And it's work correctly.
My first page URL with search:
http://localhost:8000/dictionary-management/postcode/search
and it's work correctly.
My second page URL with search:
http://localhost:8000/dictionary-management/postcode/search?page=2
and the is nothing to show, only blank page.
This is my Controller Search method:
public function search(Request $request) {
$constraints = [
'postcode' => $request['postcode'],
'address' => $request['address']
];
$postcodes = $this->doSearchingQuery($constraints);
return view('dictionary-mgmt/postcode/index', ['postcodes' => $postcodes, 'searchingVals' => $constraints]);
}
private function doSearchingQuery($constraints) {
$query = Postcode::query();
$fields = array_keys($constraints);
$index = 0;
foreach ($constraints as $constraint) {
if ($constraint != null) {
$query = $query->where( $fields[$index], 'like', '%'.$constraint.'%');
}
$index++;
}
return $query->Paginate(5);
}
This is my route :
Route::resource('dictionary-management/postcode', 'PostCodeController');
Route::post('dictionary-management/postcode/search', PosstCodeController#search')->name('postcode.search');
This is my index view :
#extends('dictionary-mgmt.postcode.base')
#section('action-content')
<!-- Main content -->
<section class="content">
<div class="box">
<div class="box-header">
<div class="row">
<div class="col-sm-8">
<h3 class="box-title">List kodów pocztowych</h3>
</div>
<div class="col-sm-4">
<a class="btn btn-primary pull-right" href="{{ route('postcode.create') }}">Dodaj nowy kod pocztowy</a>
</div>
</div>
</div>
<!-- /.box-header -->
<div class="box-body">
<div class="row">
<div class="col-sm-6"></div>
<div class="col-sm-6"></div>
</div>
<form method="POST" action="{{ route('postcode.search') }}">
{{ csrf_field() }}
#component('layouts.search', ['title' => 'Szukaj'])
#component('layouts.two-cols-search-row', ['items' => ['postcode', 'address'], 'title' => ['Kod','Adres'],
'oldVals' => [isset($searchingVals) ? $searchingVals['postcode'] : '', isset($searchingVals) ? $searchingVals['address'] : '']])
#endcomponent
#endcomponent
</form>
<div id="example2_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">
<div class="row">
<div class="col-sm-12">
<table id="example2" class="table table-bordered table-hover dataTable" role="grid" aria-describedby="example2_info">
<thead>
<tr role="row">
<th width="5%">Kod pocztowy</th>
<th width="40%">Adres</th>
<th width="10%">Miejscowość</th>
<th width="10%">Województwo</th>
<th width="10%">Powiat</th>
<th>Akcja</th>
</tr>
</thead>
<tbody>
#foreach ($postcodes as $postcode)
<tr role="row" class="odd">
<td>{{ $postcode->postcode }}</td>
<td>{{ $postcode->address }}</td>
<td>{{ $postcode->city }}</td>
<td>{{ $postcode->voivodeship }}</td>
<td>{{ $postcode->county }}</td>
<td>
<form class="row" method="POST" action="{{ route('postcode.destroy', ['id' => $postcode->id]) }}" onsubmit = "return confirm('Czy napewno usunąć?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('postcode.edit', ['id' => $postcode->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin">
Edytuj
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin">
Usuń
</button>
</form>
</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th width="5%">Kod pocztowy</th>
<th width="40%">Adres</th>
<th width="10%">Miejscowość</th>
<th width="10%">Województwo</th>
<th width="10%">Powiat</th>
<th>Akcja</th>
</tr>
</tfoot>
</table>
</div>
</div>
<div class="row">
<div class="col-sm-5">
</div>
<div class="col-sm-7">
<div class="dataTables_paginate paging_simple_numbers" id="example2_paginate">
{{ $postcodes->links() }}
</div>
</div>
</div>
</div>
</div>
<!-- /.box-body -->
</div>
</section>
<!-- /.content -->
</div>
#endsection
And this is my search component:
<div class="row">
#php
$index = 0;
#endphp
#foreach ($items as $item)
<div class="col-md-6">
<div class="form-group">
#php
$stringFormat = strtolower(str_replace(' ', '', $item));
#endphp
<label for="input<?=$stringFormat?>" class="col-sm-3 control-label">{{$title[$index]}}</label>
<div class="col-sm-9">
<input value="{{isset($oldVals) ? $oldVals[$index] : ''}}" type="text" class="form-control" name="<?=$stringFormat?>" id="input<?=$stringFormat?>" placeholder="{{$title[$index]}}">
</div>
</div>
</div>
#php
$index++;
#endphp
#endforeach
</div>
Please help, I don't know where is my mistake...
Try this
{{ $postcodes->appends(request()->input())->links()}}
instead of {{ $postcodes->links() }}
I got a really strange behavior in my views. I have a parent view called "access.blade.php" and child view "survey.blade.php".
I instanciated 2 vars in the parent view which are empty arrays.
I populate them in the child view but when i var_dump them in the parent view after the include of the child view, my var are empty...
here is the parent view :
#extends('template')
#section('title')
Compile result
#stop
#section('content')
<div id="access" class="container-fluid main-content">
<div class="row">
<div class="col-xs-6 col-md-push-3 col-xs-push-3">
<div class="page-title white-text text-center">
<h1>Access Report</h1>
</div>
<div class="widget-container fluid-height clearfix">
<?php
$session = Sessions::find($params['session_id']);
$userNotSeen = [];
$userSeen = [];
?>
<div class="heading">
<button class="btn btn-success btn-xl pull-right" role="button" data-toggle="modal" data-target="#complianceModal">Compliance</button>
Access Report for
{{ isset($result->content->display_name) ? $result->content->display_name : $result->content->title }}
<p>
Journey: {{ $session->location_id != null ? $session->location->city : $session->place }}
</p>
</div>
<div class="row text-center"></div>
<div class="row">
<div class="col-sm-offset-1 col-sm-10">
#if($result != null)
#if($result->content_type == 1)
#include('contents.partials.access.tasklist')
#elseif($result->content_type == 2)
#include('contents.partials.access.resources')
#elseif($result->content_type == 3)
#include('contents.partials.access.survey')
#elseif($result->content_type == 4)
#include('contents.partials.access.feedback')
#elseif($result->content_type == 5)
#include('contents.partials.access.memo')
#elseif($result->content_type == 6)
#include('contents.partials.access.gear')
#elseif($result->content_type == 7)
#include('contents.partials.access.sinsim')
#elseif($result->content_type == 8)
#include('contents.partials.access.changr')
#endif
#else
<p class="text-center">{{ $errors }}</p>
#endif
<pre>
{{ var_dump($userNotSeen) }}
</pre>
<pre>
{{ var_dump($userSeen) }}
</pre>
</div>
</div>
</div>
</div>
</div>
</div>
#include('contents.partials.modal')
#stop
And here is my child view
<table class="table">
<thead>
<tr>
<th>Name</th>
<th class="text-center">Viewed</th>
<th class="text-center">Submitted</th>
<th class="text-center">Personnal Results</th>
</tr>
</thead>
<tbody>
#if(isset($result->data))
#forelse($result->data as $key => $value)
<tr {{ $value->view == 0 ? 'class="notyetseen"' : ''; }}>
<td>{{ $value->fullname }}</td>
<td class="text-center">{{ $value->view == 0 ? 'No' : 'Yes' }}</td>
<td class="text-center">{{ $value->done == 0 ? 'No' : 'Yes' }}</td>
<td class="text-center">
Get results
</td>
</tr>
<?php
if($value->view == 0) {
array_push($userNotSeen, $key);
} else {
array_push($userSeen, $key);
}
?>
#empty
<tr><td colspan="3" class="text-center">{{ $errors }}</td></tr>
#endforelse
#else
<tr><td colspan="3" class="text-center">No participants for this journey</td></tr>
#endif
</tbody>
</table>
I have an idea about the why it doesn't work: maybe the parent view is loaded completly before the child view, so my var are not yet populated.
If it is the case, is there any solution?
First of all, never EVER include logic in your templates. Move this logic to your controller and pass results to a view. Templates are only for displaying your data.
If you have to use <?php in your Blade template, you are doing something wrong.
Second, Blade compiles your templates separately. It is not a simple include(__child_template__). That's why while compiling each of your templates, Blade works with completely different variable scopes and $userNotSeen in access.blade.php template is not the same as in your survey.blade.php.