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>
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
hi forum how to convert created_at column as header table and synchronized it with title column.
this is my database
below is my blade code
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
#foreach($users as $u=>$user)
#foreach($user->posts as $p)
<th>{{$p->created_at}}</th>
#endforeach
#endforeach
</tr>
</thead>
<tbody>
#foreach($users as $u=>$user)
<tr>
<td>{{$u+1}}</td>
<td>{{ $user->name }}</td>
#foreach($user->posts as $p)
<td>{{$p->title}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
this is my blade view
i have 2 table, user table and post table. a user could have many posts.
this is my post model
this is my user model
this my post controller
thank you.
You can collect all created_at in a new array with the help of #php directive and use 2 nested loops for comparison while displaying.
Run over all created_at and check if any of the post's timing matches. If yes, display the anchor tag and break out of the loop. If none of them matches, print an empty td.
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
#php
$created_ats = []
#endphp
#foreach($users as $u => $user)
#foreach($user->posts as $p)
<th>{{ $p->created_at }}</th>
#php
$created_ats[] = $p->created_at
#endphp
#endforeach
#endforeach
</tr>
</thead>
<tbody>
#foreach($users as $u => $user)
<tr>
<td>{{$u+1}}</td>
<td>{{ $user->name }}</td>
#foreach($created_ats as $created_at)
#foreach($user->posts as $p)
#if($p->created_at == $created_at)
<td>{{$p->title}}</td>
#break
#endif
#if($loop->last)
<td></td>
#endif
#endforeach
#endforeach
</tr>
#endforeach
</tbody>
</table>
</div>
If you want dates in sorted format, collect all of them in your controller, usort them with the help of DateTime class and pass them to the view.
Controller code:
<?php
public function index(){
$users = User:all();
$created_ats = [];
foreach($users as $u=>$user){
foreach($user->posts as $p){
$created_ats[] = $p->created_at;
}
}
usort($created_ats,function($date1,$date2){
$date1 = \DateTime::createFromFormat('Y-m-d H:i:s', $date1);
$date2 = \DateTime::createFromFormat('Y-m-d H:i:s', $date2);
return $date1 <=> $date2; // needs PHP7 minimum
});
return view('posts.index',compact('users','created_ats'));
}
Blade:
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
#foreach($created_ats as $created_at)
<th>{{ $created_at }}</th>
#endforeach
</tr>
</thead>
<tbody>
#foreach($users as $u => $user)
<tr>
<td>{{$u+1}}</td>
<td>{{ $user->name }}</td>
#foreach($created_ats as $created_at)
#foreach($user->posts as $p)
#if($p->created_at == $created_at)
<td>{{$p->title}}</td>
#break
#endif
#if($loop->last)
<td></td>
#endif
#endforeach
#endforeach
</tr>
#endforeach
</tbody>
</table>
</div>
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>
Currently im using Laravel-Excel (MaatWebsite) and wanted to check whether excel data is same with data in database "furnitures" table
Controller
// assume $id is retrieve from parameter
$check_furnitures = Furniture::where('company_no', $id)->get();
$rows = Excel::load('storage\\app\\public\\upload\\furniture.xlsx', function($reader){$reader->noHeading();$reader->skipRows(1)})->get();
View
<table>
<thead>
<tr>
<td>Chair Name></td>
<td>Desk Name></td>
<td>Carpet Name></td>
<td>Status</td>
</tr>
</thead>
<tbody>
<?php $arr = array(); ?>
#foreach($rows as $key => $value)
#foreach($check_furnitures as $fur)
#if(
$fur->chairs == $value[1] &&
$fur->desks == $value[2] &&
$fur->carpets == $pvalue[3]
)
<?php $temp_array[] = $value[0] ?>
#endif
#endforeach
#endforeach
<tr>
<td>{{$value[1]}}</td>
<td>{{$value[2]}}</td>
<td>{{$value[3]}}</td>
<td>
#foreach($arr as $test)
#if($test == $value[0])
DUPLICATED VALUE
#endif
#endforeach
</td>
<tr>
<table>
Excel File
The most efficient way is to index your database results first instead of looping. Laravel has a $collection->keyBy('id') so that you can $collection->get($id) later.
Having said that, you can do it this way:
<table>
<thead>
<tr>
<td>Chair Name></td>
<td>Desk Name></td>
<td>Carpet Name></td>
<td>Status</td>
</tr>
</thead>
<tbody>
#php
$arr = array();
$check_furnitures->keyBy('number');
#endphp
#foreach ($rows as $key => $row)
<tr>
<td>{{ $row->chair }}</td>
<td>{{ $row->desk }}</td>
<td>{{ $row->table }}</td>
<td>
#if($check_furnitures->get($row->number) !== null)
DUPLICATED VALUE
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
I am trying access my model in my blade template. So I can loop thorough data.
I have tried calling global function like
{{ Auth::user }}
and it works fine. I can output user data on my view.
I have created a model called students which holds students data with user_id which is coming from user table. Like 1->N relationship. A user has multiple students associated with it. How can I call custom model in my view.
Pass student data to view
$students = Student::all();
return view('student_view')
->with('student_data', $students);
View like this in table
<table id="table-student" class="table table-bordered table-striped">
<thead>
<tr>
<th width="5%" class="text-center">No.</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach($student_data as $student)
<tr>
<td width="5%"><?php echo $i; ?></td>
<td>{{ $student->name }}</td>
<td>{{ $student->description }}</td>
</tr>
<?php $i++; ?>
#endforeach
</tbody>
</table>
You can create a variable and pass it to the view:
$user = User::where('id', 1)->with('students')->first();
return view('some.view', compact('user'));
Then you can itearte over students in the view:
#foreach ($user->students as $student)
{{ $student->name }}
#endforeach
In your view your can directly call the relation method as:
#foreach(auth()->user()->students as $student)
{{ $student->name }}
#endforeach
Assuming your relation method name is students.
I am currently working on a new large app and I am trying to ensure I implement best practices from the start. I am new to Laravel so one question I have is how can I limit repetition throughout my app.
As an example I currently have a resource controller for an admin section that makes a view like so:
public function index()
{
// $data will be passed to the view
$data = array(
'pageTitle' => 'Manage Products',
'btn' => array(
'title' => 'Add product',
'url' => 'admin.catalog.product.create',
)
);
return View::make('catalog::product.index', $data)->with('products', Product::all());
}
My view file looks likes so:
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>SKU</th>
<th>Price</th>
<th>Qty</th>
<th>Created</th>
<th><i class="icon-cog"></i></th>
</tr>
</thead>
<tbody>
#foreach ($products as $product)
<tr>
<td>{{ $product->pid }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->sku }}</td>
<td>{{ Currency::display($product->price) }}</td>
<td>{{ $product->quantity }}</td>
<td>{{ Dates::showTimeAgo($product->created_at) }}</td>
<td>
Edit
{{ Form::open(array('route' => array('admin.catalog.product.destroy', $product->pid), 'method' => 'delete', 'data-confirm' => 'Are you sure?')) }}
<button type="submit" href="{{ URL::route('admin.catalog.product.destroy', $product->pid) }}" class="btn btn-danger btn-mini">Delete</button>
{{ Form::close() }}
</td>
</tr>
#endforeach
</tbody>
Is there a way that I can just use the one view file for all admin tables and pass what columns I want for the header and body through the controller instead of hard coding this in the view like above?
I know I can pass the $data array but not sure how to go about passing the columns I want through this. Any ideas or suggestions would be much appreciated.
I don't think this really is a question regarding Laravel, but hey, here we go.
You could add functions to your models to map column names to your values.
public function getColumns()
{
return array(
'ID' => 'pid',
'Name' => 'name',
....
);
}
public function getColumnData()
{
$columns = $this->getColumns();
$records = [];
foreach ($columns as $column) {
if ('name' == $column) {
$data = '' . $product->name . '';
} if else (..) {
...
} else {
$data = $this->{$column};
}
}
return $records;
}
And your view would become something like this:
<thead>
<tr>
#foreach ($records->first()->getColumns() as $column)
<th>{{ $column->name }}</th>
#endforeach
</tr>
</thead>
<tbody>
#foreach ($records->all() as $record)
<tr>
#foreach ($record->getColumnData() as $data)
<td>{{ $data }}</td>
#endforeach
</tr>
#endforeach
</tbody>
I would like this
View:
<table>
<thead>
<tr>
#foreach ($records[0] as $k => $v)
<th>{{ ucwords(str_replace('_', ' ', $k)) }}</th>
#endforeach
</tr>
</thead>
<tbody>
#foreach ($records as $record)
<tr>
#foreach ($record as $col)
<td>{{ $col }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
Controller: (Using getColumns helper function)
$users = User::all();
// In the first array, just pass the fields
// you want to get, second one is data array
$data['records'] = getColumns(array('id', 'username', 'email'), $users->toArray());
return View::make('user.users', $data);
Or, you may use it like this
$users = User::all()->toArray();
// In this example, I want to show only
// username, first_name and last_name
$records = getColumns(array('username', 'first_name', 'last_name'), $users);
return View::make('user.users')->with('records', $records);
This is a helper function, it's possible to use this from any model/controller
function getColumns($keys, $result)
{
$records = array();
foreach ($result as $row) {
$record = array();
foreach ($row as $k => $v) {
if(in_array($k, $keys)) $record[$k] = $v;
}
$records[] = $record;
}
return $records;
}
I'm using helper function because, I can use it with any model and it's in my filters.php file, you can put it anywhere. Actually, I have a file custom_helpers.php file in app folder where filters.php and at the end of my filters.php file, I've this
include "custom_helpers.php";
In this custom_helpers.php I've this helper function with other functions. Here is an example with screenshot
$users = User::take(5)->get()->toArray();
$records = getColumns(array('username', 'first_name', 'last_name'), $users);
return View::make('user.users')->with('records', $records);
Screen Shot:
Thanks for all the responses. I actually ended up using a package created by Rob Gordijn. Works great for me and prevents a lot of repetition. You can get this package at the URl below:
https://github.com/RobGordijn/Bamboo