I'm a newbie, studying programming.
Doing Laravel.
My table looks like this
ID FormID Type
1 1 Regional
2 1 Remote
3 1 Metro
4 2 Regional
5 2 Remote
6 3 Regional
7 3 Remote
My controller
$FormID = WorkArea::select('FormID', 'Type')->get();
My View is this
#foreach( $WorkArea as $WorkAreas)
<tr></tr>
<td> {{ $WorkAreas->FormID}} </td
<td> {{ $WorkAreas->Type}} </td>
#endforeach
Question:
How to foreach loop to show something like this in my VIEW
Form ID Type
1 Regional Remote Metro
2 Regional Remote
3 Regional Remote
Thank You!
You could use the Collection to help transform the structure for you:
$workAreas = WorkArea::get(['FormID', 'Type'])
->groupBy('FormID')
->map->pluck('Type');
#foreach ($workAreas as $FormID => $types)
<tr>
<td>{{ $FormID }}</td>
<td>{{ $types->join(' ') }}</td>
</tr>
#endforeach
Laravel 8.x Docs - Collections - Method groupBy
Laravel 8.x Docs - Collections - Method map
Laravel 8.x Docs - Collections - Method pluck
Laravel 8.x Docs - Collections - Method join
Laravel 8.x Docs - Collections - Higher Order Messages
try this
in your controller
$workAreas = WorkArea::select('FormID', 'Type')->get();
in your View
#if(! empty($workAreas))
#foreach($workAreas as $workArea)
<tr>
<th>{{ $work->FormID }}</th>
<th>
#foreach($workAreas as $work)
#if($workArea->FormID === $work->FormID)
{{ $work->Type }}
#endif
#endforeach
</th>
</tr>
#endforeach
#endif
Related
I have categories table (id, category_name, parent_id) with following data.
id-category_name-parent_id
1-Electronics-NULL
2-Mobile Phones-1
3-Smart Watches-1
4-Laptops-1
5-Batteries-4
In the Controller retrieves all categories that parent_id is NULL
public function index()
{
$categories = Category::whereNull('parent_id')->get();
return view('catalogs.categories.v2.index',compact('categories'));
}
In the index.blade.php I tried something using treeview heirarchy
<tbody>
#foreach($categories as $category)
<tr>
<td></td>
<td>{{ $category->category_name }}</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
{{ $category->category_name }}
#if(count($category->childs))
#include('catalogs.categories.v2.manageChild',['childs' => $category->childs])
#endif
</td>
<td>{{$category->childs_count() }}</td>
<th></th>
</tr>
#endforeach
</tbody>
#foreach($childs as $child)
>> {{ $child->category_name }}</span>
#if(count($child->childs))
#include('catalogs.categories.v2.manageChild',['childs' => $child->childs])
#endif
#endforeach
Now, my output in my table in the view is like as
Category Name
Electronics
Electronics >> Mobile Phones >> Smart Watches >> Battery >> Laptops
I want to create something like this
Category Name
Electronics
Electronics >> Mobile Phones
Electronics >> Smart Watches
Electronics >> Laptops >> Battery
Can anyone how to fix this?
In your case you can imagine your categories as nodes and the relations between theme as edges
so to search in depth for each category's sons you need to use graph search algorithms such like DFS,
in this link you could know more better about DFS:
DFS IN GENERAL
show case for similar problem solved IN DFS
whats wrong why in blade don't want to show the LEVEL name
controller:
$streams=Stream::with('level')->get();
return view('streams.index',compact('streams'));
Stream Model:
public function level()
{
return $this->belongsTo('App\Models\Level', 'level_id');
}
Level Model:
public function stream()
{
return $this->hasMany('App\Models\Stream', 'level_id');
}
Blade Index:
#foreach($streams as $stream)
<tr>
<th scope="row">#</th>
<td>{{$stream->name}}</td>
<td>{{$stream->code}}</td>
<td>
{{$stream->level->name}}
</td>
<td>
<tr/>
#endforeach
Also tried:
#foreach($streams as $stream)
<tr>
<th scope="row">#</th>
<td>{{$stream->name}}</td>
<td>{{$stream->code}}</td>
#foreach($stream as $level)
<td>
{{$level->name}}
</td>
#endforeach
<td>
<tr/>
#endforeach
Error:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\project\sms\resources\views\streams\index.blade.php)
DD result:
[{"id":3,"name":"Class A","code":"GRC0001A","uuid":"X9HG9zc7ceTlhdfF1fN1wAer1cHP1MhfuM7GHBSqNogYSo3bsGmpTl06iJQyyKp3QMrkHe1VyiTxeKFa49wC7W5BY3E3kFZkpF1D","level_id":1,"created_at":"2020-06-14T10:58:28.000000Z","updated_at":"2020-06-14T11:39:54.000000Z","level":{"id":1,"name":"YEAR 7","code":"CODE1","uuid":"X9HG9zc7ceTlhdfF1fN1wAer1cHP1MhfuM7GHBSqNogYSo3bsGmpTl06iJQyyKp3QMrkHe1VyiTxeKFa49wC7W5BY3E3kFZkpF1D","created_at":"-000001-11-30T00:00:00.000000Z","updated_at":"-000001-11-30T00:00:00.000000Z"}},{"id":4,"name":"Class B","code":"GRC0001A","uuid":"gq2kZZikN76XEa4pQWsyAZBMxjKeHBJt0a840ZMSiuHGztuhYT0G6q5WcGgp8z6BD6nx0WSrrOTvEb4iQ0ewyB9Fa1M54CAv8HS2","level_id":null,"created_at":"2020-06-14T10:58:36.000000Z","updated_at":"2020-06-14T11:39:59.000000Z","level":null}]
Your first Blade template is almost correct.
The issue is that one or more of your records has no Level assigned (level_id=null), but you're still trying to pull the name property from $stream->level, which is null.
Simply add a check for $stream->level before trying to access/print its properties. For example:
#if($stream->level)
{{ $stream->level->name }}
#endif
or
{{ $stream->level ? $stream->level->name : 'No Level Attached' }}
or
{{ optional($stream->level)->name }}
etc.
I have this code to view all invoices in my page:
#foreach ($user->invoices() as $invoice)
<tr>
<td>{{ $invoice->date()->toFormattedDateString() }}</td>
<td>{{ $invoice->total() }}</td>
<td>
Download
</td>
</tr>
#endforeach
However, the line $invoice->date()->toFormattedDateString() gives an error:
DateTime::__construct(): Failed to parse time string (#) at position 0 (#): Unexpected character
I just followed all instructions in the Laravel Cashier (Billing) website and I am getting this error. I have tried two versions of Laravel (5.8 and 5.5) and two Laravel Cashier versions (8.0 and 7.0). All tests have the same date issue.
I have added the correct migrations fields. This is how I insert/add subscription to my database:
$user = Auth::user();
$user->newSubscription('eat', 'eat_monthly')->create($request->stripeToken);
instead of {{ $invoice->date()->toFormattedDateString() }}
Can you try {{ $invoice->created_at->toFormattedDateString() }} ?
Stripe has changed date parament to created works.
Use this way:{{ $invoice->created }}
I am working with laravel. I have two table. one is users and another is permission. I have two foreach loop. One is to show user data. User data coming from users table. And another foreach loop is is for checking is that user available or not in permission table.
I am fetching all user from users table like this:
$allTeachers = User::where('role',2)->paginate(4);
And am also fetching all data from permission table like this.
$allpermiteds = Specialpermission::where('add_teacher','!=','NULL')->get();
I have sent those two query to my view. And it is working fine.
Now I am printing those users data in my view like this
#foreach($allteachers as $teacher)
<tr>
<td>{{ $teacher->id}}</td>
<td>{{ $teacher->name }}</td>
<td>{{ $teacher->email }}</td>
<td><button>give</button></td>
<td><button>Remove</button></td>
#endforeach
I have printed two button there give and remove. Now I want to do that, If $teacher->id is available in the permission table add_teacher column then it should show remove button. Otherwise it should Show add button.
I have done like this, But it is not working properly.
#foreach($allteachers as $teacher)
<tr>
<td>{{ $teacher->id}}</td>
<td>{{ $teacher->name }}</td>
<td>{{ $teacher->email }}</td>
<td>#foreach($allpermitted as $p)
#if($p->add_teacher == $teacher->id)
<button>Remove</button>
#else
<button>give</button>
#endif
#endforeach</td>
#endforeach
It shows a lot of button. If there are 4 users in the permission table then it will show 4 button for each user. That is the main problem
Thanks in advance.
Simple, select add_teacher column from db, then convert your Specialpermission objects into array like below:
$permissionData = Specialpermission::where('add_teacher','!=','NULL')->select('add_teacher')->get();
$allpermiteds = collect($permissionData )->map(function($x){ return (array) $x; })->toArray();
Then use in_array to check for permission check
#foreach($allteachers as $teacher)
<tr>
<td>{{ $teacher->id}}</td>
<td>{{ $teacher->name }}</td>
<td>{{ $teacher->email }}</td>
<td>
#if(in_array($teacher->id,$allpermitted))
<button>Remove</button>
#else
<button>give</button>
#endif
</td>
#endforeach
I prefer using the eloquent function with that defines the relationship between the two tables:
$allTeachers = User::where('role',2)->with('SpecialPermisson')->paginate(4);
It will add a field of SpecialPermission (if it has). If not, it will give you a blank field, so check if its empty.
I have a report page that loops through a collection and displays the data in a generic bootstrap template table - it's showing data about users, and one user can have multiple rows in the table. I group the rows by the user ID and I want to basically stripe the table rows based on the user ID (not just alternating rows). So, for example, I might display 4 rows for user ID = 5 with a white background and then 2 rows for user ID = 6 with a gray background. I'd just use a local variable in straight php, but is there an elegant/clean way to do this in the blade templates?
#foreach($payments->getDetails() AS $k=>$detail)
<tr>
<td>{{ $detail->payment->userid }}</td>
<td>${{ $detail->payment->amount }}</td>
<td>{{ $detail->payment->status }}</td>
</tr>
#endforeach
In this example, $payments is a collection of $paymentsDetails objects. So based on the $detail->payment->userid value I'd like to determine the class or background color.
Thank you for any help in this.
Just a thought... This assumes the rows are ordered by user id naturally (it would not work otherwise in your example anyway...)... So here's the thought...
If the user id is odd, then give it a color, if it is even, give it the alternate color... that would work... Sort of ...
#foreach($payments->getDetails() AS $k=>$detail)
<tr>
<td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->userid }}</td>
<td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->amount }}</td>
<td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->status }}</td>
</tr>
#endforeach
Something like this would also work... not sure I like it though
#foreach($payments->getDetails() AS $k=>$detail)
#if($detail->payment->userid % 2 == 0)
<tr>
<td class="evencolor">{{ $detail->payment->userid }}</td>
<td class="evencolor">>${{ $detail->payment->amount }}</td>
<td class="evencolor">{{ $detail->payment->status }}</td>
</tr>
#else
<tr>
<td class="oddcolor">{{ $detail->payment->userid }}</td>
<td class="oddcolor">${{ $detail->payment->amount }}</td>
<td class="oddcolor">{{ $detail->payment->status }}</td>
</tr>
#endif
#endforeach
Anyway... just a possible direction... someone else might have a more elegant idea...
I got the solution to this via www.laracasts.com from user #Cronix:
I'd do it the way you were thinking originally and just use
#php/#endphp blade directives to determine if the userid has changed
during each loop iteration and if it does change the color.
#php
$lastid = null;
$rowclass = 'grey';
#endphp
#foreach($payments->getDetails() AS $k=>$detail)
#php
//if userid changed from last iteration, store new userid and change color
if ($lastid !== $detail->payment->userid)
{
$lastid = $detail->payment->userid;
if ($rowclass == 'grey') $rowclass = 'white';
else $rowclass = 'grey';
}
#endphp
<tr class="{{ $rowclass }}">
<td>{{ $detail->payment->userid }}</td>
<td>${{ $detail->payment->amount }}</td>
<td>{{ $detail->payment->status }}</td>
</tr>
#endforeach
If anyone out there has a more elegant Blade-focused idea on how to solve it, I'd love to hear about it.
hey guys the methods above failed for me, incase anyone els needs this : im using laravel 5.4 with php 5.6===> this is what has worked for me;
#php
if ( $post->dbdata == 'approved'):
$color = 'green';
elseif ( $post->dbdata == 'Pending'):
$color = 'yellow';
elseif ( $post->dbdata == 'rejected'):
$color = 'red';
else:
$color = 'black';
endif;
#endphp
<tr style="background-color: {{$color}}">
<td>
<h6>{{ $post->dbdata }}</h6>
</td>
</tr>