How to get column names and value of a table created on runtime with laravel.
$table = 'table_name';
$columns = DB::getSchemaBuilder()->getColumnListing($table);
$records = DB::table($table)->get();
Now i get can column name by this in view.
<tr>
#for($i = 0; $i < sizeof($columns); $i++)
<th>{{ ucfirst(str_replace('_', ' ',$columns[$i]) )}}</th>
#endfor
</tr>
Now how can i display column values below column headings
#foreach($records as $key=>$row)
{{ $row->id}}
#endforeach
I need this type of output
Table Name
id | colum_1 |colum_2 | etc
1 | test | test11
2 | asdf | asf
Change your second #foreach to be:
#foreach($records as $row)
<tr>
#foreach($row as $data)
<td>{{ $data }}</td>
#endforeach
</tr>
#endforeach
Related
Leave:
id
user_id
start_date
end_date
1
2
2022-06-01
2022-06-03
Blade:
#foreach ($users as $row )
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $row->id }}</td>
<td>{{ $row->name }}</td>
<td>{{ $row->dpt->department }}</td>
#php
$today = Carbon\Carbon::now();
$leave = App\Leave::where('user_id', $row->id)->whereBetween('') <!-- incomplete -->
#endphp
<td>Leave Status</td>
</tr>
#endforeach
In this table, all user lists are shown. Here in the Leave Status column, it will show On Leave if the user is on leave today. I think I have to match today's date with start_date and end_date of the leave table and also match the user_id
What will be the eloquent query for this?
Example:
id
name
Leave Status
1
Jhon
Present
2
Doe
On Leave
3
Laura
On Leave
If you also have the relationships defined in the model you could load them with constraints in your controller:
$users = User::with(['leaves' => function ($query) {
$query->where('start_date', '>=', Carbon::now()))
->where('end_date', '<=', Carbon::now()));
}])->get();
This way you don't have to query in your blade templates and you can just check if count($row->leaves) > 0
have you try this:
\Carbon\Carbon::now()->between($row->leaves()->last()->start_date,$row->leaves()->last()->end_date) ? 'On Leave' : 'Present';
I have an sql table knowledgebase like so
id , categoryid, title
id
categoryid
title
1
1
apple
2
1
fb
3
2
google
4
2
DB
5
3
Reebok
In my laravel blade, I am trying to create a tree view to look like the following
-1
--apple
--FB
-2
--google
--DB
-3
--Reebok
My controller does a basic query and returns the entire table to the view. I am a newbie to laravel so far, I can get a basic table to work like
#foreach($knowledgebase as $key => $value)
<tr>
<td>{!! $knowledgebase ->id !!}</td>
<td>{!! $knowledgebase ->title!!}</td>
<td>{!! $knowledgebase ->categoryid !!}</td>
</tr>
#endforeach
How would I iterate categroyid column , display first category and all child titles and then move on to the next categoryid.
Update
public function show($id) {
//get article
$knowledgebase = \App\Models\Knowledgebase::Where('knowledgebase_slug', request('knowledgebase_slug'))->first();
return view('knowledgebase', compact('knowledgebase'));
}
You will need to retrieve in controller the categories before ($categories)
#foreach($categories as $category)
<tr>
<td>{{ $category->id }}</td>
<td>{{ $category->title}}</td>
<td>{{ $category->categoryid }}</td>
</tr>
#endforeach
Anyways, I managed to solve this by adding a function children in my model. So in my Model added
public function children(){
return $this->hasMany('App\Models\Knowledgebase', 'categoryid');
}
After this, I simply called it as a foreach inside my foreach
#foreach($categories as $category)
<li>{{ $category->title}}</li>
#if($category->children)
#foreach($category->children as $child)
<ul>
<li class="pl-3"> {{ $child->title}} </li>
</ul>
#endforeach
#endif
#endforeach
I am trying to display the list of vendor with their purchase orders in a table.
VENDOR MODEL
public function vendorPo()
{
return $this->hasMany('App\PurchasedOrder', 'vendor_id','vendor_id');
}
VENDOR CONTROLLER
public function vendor()
{
$vendorPo = Vendor::with('vendorPo')
->get()
->keyBy('id')
->groupBy('vendor_name');
$purchaseOrders = PurchasedOrder::all()->where('publish','=','1');
return view('backend.purchase-order.vendor', compact('purchaseOrders'))
->with('vendorPo', $vendorPo);
}
for my index
#foreach ($vendorPo as $vendor => $vendor_list)
<tr>
<th colspan="7"
style="background-color: #F7F7F7"> {{ $vendor }}: ({{ $vendor_list->count() }} vendors)
</th>
</tr>
#foreach ($vendor_list as $key => $vendorPoList)
<tr>
<td>
{{ $vendorPoList->vendorPo->po_id}}
</td>
</tr>
#endforeach
#endforeach
if I do this
...
#foreach ($vendor_list as $key => $vendorPoList)
<tr>
<td>
{{ $vendorPoList}}
</td>
</tr>
#endforeach
...
the result is this
and if I
...
<td>
{{ $vendorPoList->vendorPO->po_id}}}
</td>
...
the result is error
Facade\Ignition\Exceptions\ViewException
Property [po_id] does not exist on this collection instance. (View:
Please help me. Thanks!
You are getting this issue because your vendorPO relation is a HasMany relation. If you pull back the records from that sort of relation, the results are always an array (even if there is only one record), which is always converted by Eloquent into a Collection.
Therefore, $vendorPoList->vendorPO is a Collection, and there is no variable on a Collection called po_id.
Depending on your data model, you should either revisit the relation, or do a #foreach on $vendorPoList->vendorPO.
i Have 3 Table , the name is : user , machine , maintance . this schema like this
*User
id | name | email | password | created_at | updated_at
1 | john | A#mail | ******* , etc
*Machine
id | name_machine | created_at | updated_at
1 | shock | .....etc
*Maintance
id | Machine_id | user_id | question | status | created_at |
updated_at
1 | .........1........ |.......1......| blablabla etc ..
now i want to show data on table "Maintance" with show data "name" (user table) and name_machine( machine table) by id .
this is my controller and my view
public function show($id)
{
$maintance = Maintance::all();
return view('users.view',['maintance' => $maintance]);
}
my view
<table class="table table-hover">
<tbody><tr>
<th>Machine</th>
<th>Question</th>
<th>User Input</th>
<th>Action</th>
<th>Tanggal</th>
</tr>
#foreach ($maintance as $i)
<tr>
<td>{{ $i->machine_id}} </td>// i want to show name machine here
<td>{{ $i->Question}}</td>
<td>{{ $i->user_id}}</td> // name users to
<td><span class="label label-primary">Lihat Data</span></td>
<td>{{ $i->created_at}}</td>
</tr>
#endforeach
</tbody></table>
this view data is not having error ,but i dont know how to showing this data from different table . can someone help ?
Assuming your Maintance model belows,
public function user(){
return $this->belongsTo(User::class);
}
public function machine(){
return $this->belongsTo(Machine::class);
}
assuming that your Model have these above relations.
and do the below to get your data,
$maintance = Maintance::with(['user','machine'])->get();
and in your view
<table class="table table-hover">
<tbody><tr>
<th>Machine</th>
<th>Question</th>
<th>User Input</th>
<th>Action</th>
<th>Tanggal</th>
</tr>
#foreach ($maintance as $i)
<tr>
<td>{{ $i->machine->machine_name}} </td>// i want to show name machine here
<td>{{ $i->Question}}</td>
<td>{{ $i->user->name}}</td> // name users to
<td><span class="label label-primary">Lihat Data</span></td>
<td>{{ $i->created_at}}</td>
</tr>
#endforeach
</tbody></table>
How to make increment row number with laravel pagination ? When i use pagination and i go to page 2 and above it will back to beginning. for example i will paginate(3)
<thead>
<tr>
<th>No</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach ($telephone->results as $telp)
<tr>
<td>
{{$i++}}
</td>
<td>{{ $telp->name }}</td>
</tr>
#endforeach
</tbody>
when i go to page 2 the number will start from 1 again.
i need to make it when i go to page 2 it will start from 4
In Laravel 5.3 you can use firstItem():
#foreach ($items as $key => $value)
{{ $items->firstItem() + $key }}
#endforeach
The below works with laravel 5.4
<?php $i = ($telephone->currentpage()-1)* $telephone-
>perpage() + 1;?>
#foreach($telephone as $whatever)
<td> {{ $i++ }}</td>
#endforeach
Edited
The below works for laravel 5.7 above
#foreach ($telephone as $key=> $whatever)
<td>{{ $key+ $telephone->firstItem() }}</td>
#endforeach
You should be able to use the getFrom method to get the starting number of the current pages results. So instead of setting $i = 1; you should be able to do this.
<?php $i = $telephone->getFrom(); ?>
In Laravel 3 there is no getFrom method so you need to calculate it manually.
<?php $i = ($telephone->page - 1) * $telephone->per_page + 1; ?>
Laravel 5.3
#foreach ($products as $key=>$val)
{{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
#endforeach
For Laravel 6.2:
$loop - just in case, is a built-in instance in Blade
#foreach($array as $item)
<tr class="table-row">
<td class="site-id">
{{($array->currentPage() - 1) * $array->perPage() + $loop->iteration}}
</td>
</tr>
#endforeach
</table>
You can simply add the following line
$i = ($telephone->currentpage()-1)* $telephone->perpage();
in place of
$i = 1;
#php($sl = ($yourVariable->perPage() * $yourVariable->currentPage()) - ($yourVariable->perPage() - 1))
#foreach($yourVariable as $item)
<td>{{ $item->key_name }}</td>
.....
#php($sl++)
#endforeach
In Laravel 6
#php $i = ($data->currentpage()-1)* $data->perpage() + 1;#endphp
#foreach($data as $banner)
<tr>
<td>{{$i}}</td>
<td><img src="{{ URL::to('/') }}/dist/img/{{$banner->name}}" height="250" width="600"></td>
</tr>
#php $i += 1; #endphp
#endforeach
If you are using Laravel Pagination. It works very well
The backend
public function index()
{
return view('your.telephone.index', [
'telephone' => Telephone::paginate(15)
]);
}
Blade front end
<td>{{ (($telephone->currentPage() * 10) - 10) + $loop->iteration }}</td>
And don't forget embed the page
{{ $telephone->links() }}
For $loop->iteration
See the doc here: https://laravel.com/docs/7.x/blade#the-loop-variable
For $telephone->currentPage()
See the doc here: https://laravel.com/docs/7.x/pagination#paginator-instance-methods
Or avoid php tags completely by
#foreach ($users as $key => $user)
{{ (Input::get('page', 1) - 1) * $users->getPerPage() + $key + 1 }}
#endforeach
You can use it in your controller. example given below
$records = Table::paginate(20);
return view('yourview',compact('records')->with('i', ($request->input('page', 1) - 1) * 20);
Note: paginate value must be equal to with() function vlaue
after that you can use $i variable in you blade file. controller calculate value according to page number and return to blade
In blade file. use inside the loop
{{ ++$i }}
Hopefully this will help you
Laravel 8++
$loop->iteration is available out of the box inside loop.
{{ ($barangs->currentPage() - 1) * $barangs->count() + $loop->iteration }}
I'm using this, in Laravel 8, accessing $loop, and links()->paginator:
#foreach ($users as $user)
<tr>
<td class="text-center">{{ ($users->currentPage() - 1) * $users->links()->paginator->perPage() + $loop->iteration }}</td>
<td class="text-center">{{$user->name}}</td>
</tr>
#endforeach
Which works properly even when the final page is only partially filled.
Could also use $users->perPage() in place of $users->links()->paginator->perPage()