Formatting pluck('') output in Laravel blade - php

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

Related

Give role acess in php?

Is it right approach to write Laravel PHP code to give a role to hr to view certain column? Because after hosting the website stops automatically and start automatically. Code is working fine..
I am new to PHP, I have no idea if i am going wrong in this code can anyone help me out yrr...
<table class="table table-striped mb-0 dataTable">
<thead>
<tr>
#role('company')
<th>{{__('Employee Name')}}</th>
#endrole
#role('hr')
<th>{{__('Employee Name')}}</th>
#endrole
<th>{{__('Designation')}}</th>
<th>{{__('Promotion Title')}}</th>
<th>{{__('Promotion Date')}}</th>
<th>{{__('Description')}}</th>
#if(Gate::check('Edit Promotion') || Gate::check('Delete Promotion'))
<th width="200px">{{__('Action')}}</th>
#endif
</tr>
</thead>
<tbody class="font-style">
#foreach ($promotions as $promotion)
<tr>
#role('company')
<td>{{ !empty($promotion->employee())?$promotion->employee()->name:'' }}</td>
#endrole
#role('hr')
<td>{{ !empty($promotion->employee())?$promotion->employee()->name:'' }}</td>
#endrole
<td>{{ !empty($promotion->designation())?$promotion->designation()->name:'' }}</td>
<td>{{ $promotion->promotion_title }}</td>
<td>{{ \Auth::user()->dateFormat($promotion->promotion_date) }}</td>
<td>{{ $promotion->description }}</td>
#if(Gate::check('Edit Promotion') || Gate::check('Delete Promotion'))
<td>
#can('Edit Promotion')
<i class="fas fa-pencil-alt"></i>
#endcan
#can('Delete Promotion')
<i class="fas fa-trash"></i>
{!! Form::open(['method' => 'DELETE', 'route' => ['promotion.destroy', $promotion->id],'id'=>'delete-form-'.$promotion->id]) !!}
{!! Form::close() !!}
#endif
</td>
#endif
</tr>
#endforeach
</tbody>
</table>
You can find what you are asking in the documentation here (version select on the left):
https://spatie.be/docs/laravel-permission/v5/basic-usage/blade-directives
#role('hr')
I am from hr!
#else
I am not from hr...
#endrole
It further reads #hasrole('hr') is an alias of #role('hr'), so no worries there.
Similarly, the same goes for permissions:
#can('edit articles')
//
#endcan
For more information, see link mentioned above.

Is there any function to stop foreach loop? [duplicate]

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

Undefined variable: user_id in Laravel view

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.

Trying to get property of non-object on Laravel 5

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:

Content broken bootstrap v3.3.7 with laravel v5.4

When refresh the page then my content broken, I couldn't understand why this happening. Some time's my whole page broken. Please help me to solve this problem.
Bootstrap v: 3.3.7
laravel v: 5.4
Here is my code:
#extends('layouts.admin-user.main')
#section('main_content')
<div class="ptb-80">
<div class="users-mang">
<div class="container">
<div class="row">
<div class="col-md-12">
#include('layouts.success')
#include('layouts.errors')
<h1>User Management</h1>
<div class="portlet-title">
<div class="caption font-dark">
#if(isset($searchData))
<a href="#if (isset($companyData->company_name)){{ url($companyData->company_name.'/user-list') }}#endif"
class="btn btn-info">Back to User List</a>
#else
<a href="#if (isset($companyData->company_name)){{ url($companyData->company_name.'/user') }}#endif"
class="btn btn-success">Add User</a>
Import User
#endif
</div>
<div class="tools"> </div>
</div>
<table class="table table-striped table-bordered table-hover" id="sample_1">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Group</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#if(isset($allData) && !$allData->isEmpty())
<?php $i = 0; ?>
#foreach($allData as $data)
<?php $i++; ?>
<tr>
<td>{{ $i }}</td>
<td>{{ $data->name }}</td>
<td>{{ $data->email }}</td>
<td>{{ $data->phone }}</td>
<td>{{ $data->group_id }}</td>
<td>
<a href="#if (isset($companyData->company_name)){{ url($companyData->company_name.'/user/contact/'.$data->id) }}#endif"
class="btn btn-info">Contact</a>
<a href="#if (isset($companyData->company_name)){{ url($companyData->company_name.'/user/edit/'.$data->id) }}#endif"
class="btn btn-primary">Edit</a>
<a href="#if (isset($companyData->company_name)){{ url($companyData->company_name.'/user/delete/'.$data->id) }}#endif"
onclick="return confirm('Are you sure?')"
class="btn btn-danger">Delete</a>
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="6" class="text-center"> No available data.</td>
</tr>
#endif
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
#endsection
Here is a broken content screenshot:
Thanks so much.

Categories