i have dynamic table and that table datas are listed one table i will try this way
$cat= DB::table('categories')->get(); //these is take table name
foreach($cat as $data){
$res= DB::table($data->name)->get(); //query build
}
return view('admin.company')->with('result',$res)->with('catg',$cat); // pass to view page
and i print data in view page but it list one table data only i print like this
#foreach($result as $datas)
<td > {{$datas->phone}} </td>
<td > #foreach($catg as $cat) #if($cat->id==$datas->cat_id){{$cat->name}} #endif #endforeach </td>
<td > {{$datas->discription}} </td>
#endforeach
how to list all tables data in one view any way? i am using laravel new version !
This is failing to work because you are assigning to $res each time in your loop, so it will only be the last table information that shows.
Change the inner part of your loop to $res[] = DB::table($data->name)->get();
You will then need to create an outer loop in your view, as such
#foreach($res as $table)
#foreach($table as $datas)
<td > {{$datas->phone}} </td>
<td > #foreach($catg as $cat) #if($cat->id==$datas->cat_id){{$cat->name}} #endif
#endforeach </td>
<td > {{$datas->discription}} </td>
#endforeach
#foreach
It may help also in your question to add why you are trying to what you are doing. I can see many other problems with your approach, but without knowing your data, it is hard to provide a better solution.
You can do that more cleverly
$cat= DB::table('categories')->get(); //these is take table name
foreach($cat as $data){
$res[$data->name] = DB::table($data->name)->get(); //query build
// or
// $res[] = [
// 'category' = $data,
// 'data' => DB::table($data->name)->get()
//]
}
return view('admin.company')->with('result',$res);
In view
#foreach($res as $tableName => $data)
#foreach($data as $datas)
<td > {{$datas->phone}} </td>
<td > {{$tableName}} </td>
<td > {{$datas->discription}} </td>
#endforeach
#foreach
Related
In my controller I've this method:
public function code($code)
{
$user = Auth::user();
$instituteCodes = DB::table('institute_codes')->where(['code' => $code, 'institute_id' => Auth::id()]);
if($instituteCodes->exists()){
$claim = DB::table('code_claims')->where('institute_code_id', $instituteCodes->value('id'))->get();
$allusers = collect($claim);
$allusers->values()->all();
$course = Course::findOrFail($instituteCodes->value('course_id'));
return view('institute.code', compact(['allusers', 'course']));
}
}
institute_codes table
id
code
course_id
1
ABC
1
2
ZXC
5
course table
id
name
1
Python
2
Laravel
I've a route which passes $code parameter to public function code() I'm getting same course data on using different codes in blade view but when I use return $course or dd($course) in controller it returns the the expected result i.e. different courses for different codes. But in blade I'm getting same courses. Any help will be appreciated.
This is happening!
Why both codes are returning same course in blade and different courses(this is what I want) on using dd or return.
Edit 1
Blade View
<div class="table-wrapper">
<table class="fl-table">
<thead>
<tr>
<th class="long-table-row">Email</th>
<th>Code</th>
<th>Course</th>
<th>Language</th>
<th>Enrolled On</th>
<th>Progress</th>
<th>Certificate</th>
<th>Action</th>
</tr>
</thead>
#foreach ($allusers as $item)
<tr>
<td class="long-table-row">
{{ $item->user_email }}
</td>
<td class="uppercase">{{ $code }}</td>
<td>{{ $course->c_name }}</td>
<td>{{ $course->language->name }}</td>
<td>29/07/2022</td>
<td>100%</td>
<td><i class="bi bi-check-circle-fill" style="font-size: 1.2vw; color: rgb(0, 200, 90);"></i></td>
<td></td>
</tr>
#endforeach
<tbody>
</table>
</div>
I'm getting same c_name and language name for different codes.
I think you are iterating through the allusers but for course you are only picking the first course in the foreach loop of allusers.
Below is my controller code.
foreach ($events as $eventId) {
$eventArray[] = Events::where('id', $eventId['event_id'])->get();
}
View
#foreach ($eventArray as $key => $value)
<tr>
<td>{{ $value }}</td>
</tr>
#endforeach
The result obtained is as follows.
[{"id":1,"event_title":"yoga","event_date":"2019-06-24 00:00:00"}]
How can I display the above results in a table?
You can change get to first as you are fetching event id wise data,
// first here to fetch one data
$eventArray[] = Events::where('id',$eventId['event_id'])->first();
Then in blade you can access with object syntax as ->
#foreach ($eventArray as $key => $value)
{{ dd($value)}}
<!-- or -->
{{ $value->id }}
#endforeach
Then you will know how to use it.
Recommended approach,
// fetch the required event ids
$eventIds = array_column($events, "event_id");
// then fetch the data for those ids
$eventArray = Events::whereIn('id', $eventIds)->get();
There is no need for that loop in your controller:
$events = Events::whereIn('id', array_column($events, 'event_id'))->get();
return $this->view('yourview', compact($events));
In your view:
<table>
<thead>
<tr>
<th>Title</th>
<th>Date</th>
</tr>
</thead>
<tbody>
#foreach ($events as $event)
<tr>
<td>{{$event->event_title}}</td>
<td>{{$event->event_date}}</td>
</tr>
#endforeach
</tbody>
</table>
I want to retrieve values from one database table but separated in two adjacent tables based on different id_produk (foreign key) values.
This is code in Controller :
public function index()
{
$item_ict = Item::where('id_produk', '1');
$item_cm = Item::where('id_produk', '2');
return view('item/index', compact('item_ict', 'item_cm'));
}
Then, I call $item_ict and $item_cm in index
<table class="table">
<thead>
<tr>
<th>ICT</th>
<th>CM</th>
</tr>
</thead>
<tbody>
#foreach ($item_ict as $itemict)
#foreach ($item_cm as $itemcm)
<tr>
<td>{{ $itemict -> nama_item }}</td>
<td>{{ $itemcm -> nama_item }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
is it the correct way when i wrote that foreach? Nothing errors, but no values exited. How the way to fix it?
Or i'm thinking about call it use query in index page, but i dont know how. is it possible? how?
Combine your items into one array so that you only need to do one loop.
Also, use ->get() to actually get the items. As it stands, your code is still just the query builder.
public function index()
{
$all = [];
$item_ict = Item::where('id_produk', '1')->get();
$item_cm = Item::where('id_produk', '2')->get();
for ($i = 0; $i < max($item_ict->count(), $item_cm->count()); $i++) {
$all[] = [
isset($item_ict[$i]) ? $item_ict[$i] : null,
isset($item_cm[$i]) ? $item_cm[$i] : null,
];
}
return view('item/index', compact('all'));
}
<table class="table">
<thead>
<tr>
<th>ICT</th>
<th>CM</th>
</tr>
</thead>
<tbody>
#foreach ($all as $items)
<tr>
<td>{{ $items[0] ? $items[0]->nama_item : null }}</td>
<td>{{ $items[1] ? $items[1]->nama_item : null }}</td>
</tr>
#endforeach
</tbody>
</table>
I have a problem with #foreach in my blade.php. When I insert movie with multiple categories my other moves to the right. Here is how it looks in browser
and here is code in blade
#if($movies)
<div class="row">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Categories</th>
<th>Actors</th>
</tr>
</thead>
<tbody>
#foreach($movies as $movie)
<tr>
<td>{{$movie->name}}</td>
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
#foreach($movie->actors as $actor)
<td>{{$actor->actor_name}}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
#endif
Your issue lies here:
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
You are saying that for each category, it should insert a new <td> into you table. This will of course skew the content since your table expects a set number of columns.
Instead, you should move your foreach statement inside the <td></td>
<td>
#foreach($movie->categories as $category)
{{$category->category_name}}
#endforeach
</td>
This will print the links for each category in the same column.
I need help with looping my product options, this is what i have now:
What I want is simply get 2 row's only one for color and another for size and in dropdown in front of each one have all items.
here is my blade code:
<tbody>
#foreach($product->suboptions as $option)
<tr>
<td style="width: 150px;">{{ $option->option->title }}</td>
<td class="text-left">
<select name="" id="">
<option value="{{$option->id}}">{{$option->title}} - {{ number_format($option->price, 0) }}</option>
</select>
</td>
</tr>
#endforeach
</tbody>
UPDATE
my dd result of {dd($product->suboptions)}}
You can first group them together with the mapToGroups() function for a collection
https://laravel.com/docs/5.5/collections#method-maptogroups
$something = $product->suboptions->mapToGroups(function ($item, $key) {
return [$item->option->title => $item];
});
You should dd() that to see what the output is and understand it.
After that you can just cycle through them with foreach, your blade should be something like
<tbody>
#foreach($something as $optiontitle => $optioncollection)
<tr>
<td style="width: 150px;">{{ $optiontitle }}</td>
<td class="text-left">
<select name="" id="">
#foreach($optioncollection as $suboption)
<option value="{{$suboption->id}}">{{$suboption->title}} - {{ number_format($suboption->price, 0) }}</option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>
For accomplishing that you need to get option first, and write a method to access options via products id or products object. so you can right something like this in products model:
public function get_option($product_id){
$sub_options = Product::whereId($product_id)->first()->suboptions()->get();
$option = array();
foreach($sub_options as $sub_option){
$option[] = $sub_option->options()->get();
}
return $option;
}
And then in view, you just call this method and put it in 2 foreach, one for option and the other for sub-option. like this code below:
<tbody>
#foreach($product->get_option($product->id) as $key=>$value)
<tr>
<td style="width: 150px;">{{ $value->title }}</td>
<td class="text-left">
<select name="" id="">
#foreach($value->suboptions() as $key=>$value2)
<option value="{{$value2->id}}">{{$value2->title}} - {{number_format($value2->price, 0) }}</option>
#endforeach
</select>
</td>
</tr>
#endforeach
</tbody>
I don't know whats your model methods but you can get the concept.
I Repeat again
you should not copy my code exactly. just follow along with the concept of what am I saying.
But this time according to your eloquent methods. this code should work.