If i use
$categories= Category::orderby('id', 'desc')->paginate(3);
it works but if i use
$categories= Category::with('subcategories')->orderby('id', 'desc')->paginate(3);
paginate doesn't work! how to fix it?
UPDATE:
<tbody>
#foreach ($categories as $category)
#foreach($category->subcategories as $sub)
<tr>
<td class="text-center">{{ $sub->id }}</td>
<td class="text-center">{{ $sub->title }}</td>
<td class="text-center">{{ $sub->category->title }}</td>
</tr>
#endforeach
#endforeach
</tbody>
You need to approach this problem from the opposite side of the relationship.
Find all subcategories, with their parent category, order them by ID descending and then paginate to three per page.
$subcategories = Subcategory::with('category')
->orderBy('id', 'desc')
->paginate(3);
<tbody>
#foreach($subcategories as $subcategory)
<tr>
<td class="text-center">{{ $subcategory->id }}</td>
<td class="text-center">{{ $subcategory->title }}</td>
<td class="text-center">{{ $subcategory->category->title }}</td>
</tr>
#endforeach
</tbody>
Related
i need help with my foreach loop in view
Controller.php
$expenses = Expense::groupBy('category_id')->selectRaw('sum(amount) as sum, category_id')->pluck('sum','category_id');
$categories = Category::all();
return view('dashboard.index', compact('expenses','categories'));
index.php
<table class="table">
<thead>
<th>Category</th>
<th>Expenses</th>
</thead>
<tbody>
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
#foreach($expenses as $expense)
<td>{{ $expense }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
I get the output like this.
enter image description here
It outputs the expense 2 times, is there a limiter for "foreach loop" or is there another way on doing this?
You need to add a condition for that $category->id is the same as category_id
#foreach($expenses as $key => $expense)
#if($key == $category->id)
<td>{{ $expense }}</td>
#endif
#endforeach
The right way to do it is by using join or relationship.
$expenses = Expense::join('categories','categories.category_id','expenses.category_id')->groupBy('expenses.category_id')->selectRaw('sum(expenses.amount) as sum, expenses.category_id','categories.name')->get();
<table class="table">
<thead>
<th>Category</th>
<th>Expenses</th>
</thead>
<tbody>
#foreach($expenses as $expense)
<th>{{ $expense->name }}</th>
<td>{{ $expense->sum }}</td>
#endforeach
</tbody>
</table>
You should limit the results in controller, but here's how you can accomplish this in a blade. Not pretty
#foreach ($element['subs'] as $item)
#if($loop->index < 10)
// Your code
#endif
#endforeach
You should compare category_id with category->id.
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
#foreach($expenses as $expense)
#if($expense->category_id == $category->id )
<td>{{ $expense->sum }}</td>
#endif
#endforeach
</tr>
#endforeach
I am newbie in laravel, i'm trying to show the table like image below
https://i.stack.imgur.com/l52Vo.jpg
#foreach($report_data as $index => $item)
<tbody class="table-content">
<tr>
<td>{{ $index+1 }}</td>
<td style="text-align:left;">{{ $item->customer_fullName }}</td>
<td>{{ $item->customer_phone }}</td>
<td>{{ $item->customer_detail_licensePlate }}</td>
<td>{{ $item->vehicle_brand_name }}</td>
<td>{{ $item->vehicle_model_name }}</td>
</tr>
</tbody>
#endforeach
but the output is like image below
https://i.stack.imgur.com/rSktI.png
Thanks!!
I would do it as so:
In the controller, select the customers eager loading the vehicles.
$customers = Customer::with('vehicles')->get();
return view('customers')->with('customers', $customers);
In the view:
<table>
#foreach($customers as $index => $customer)
<tr>
<td rowspan="$customer->vehicles->count()">
{{ $index+1 }}
</td>
<td rowspan="$customer->vehicles->count()">
{{ $customer->fullName }}
</td>
<td rowspan="$customer->vehicles->count()">
{{ $customer->phone }}
</td>
<td> {{$customer->vehicles[0]->licensePlate }} </td>
<td> {{$customer->vehicles[0]->brandName }} </td>
<td> {{$customer->vehicles[0]->modelName }} </td>
</tr>
#for($i=1;$i<$customer->vehicles->count())
<tr>
<td> {{$customer->vehicles[$i]->licensePlate }} </td>
<td> {{$customer->vehicles[$i]->brandName }} </td>
<td> {{$customer->vehicles[$i]->modelName }} </td>
</tr>
#endfor
#endforeach
</table>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Customer</th>
<th scope="col">Licence Number</th>
<th scope="col">Brand</th>
<th scope="col">Brand Type</th>
</tr>
</thead>
<tbody>
#foreach($report_data as $index => $item)
<tbody class="table-content">
<tr>
<td>{{ $index+1 }}</td>
<td style="text-align:left;">{{ $item->customer_fullName }}</td>
<td>{{ $item->customer_phone }}</td>
<td>{{ $item->customer_detail_licensePlate }}</td>
<td>{{ $item->vehicle_brand_name }}</td>
<td>{{ $item->vehicle_model_name }}</td>
</tr>
</tbody>
#endforeach
</tbody>
use this CDN for Bootstrap style
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
You need not to iterate tbody everytime, just iterate <tr></tr> as below
<table>
<thead>
<tr>
<th>No</th>
<th>Customer</th>
<th>Phone</th>
<th>License Number</th>
<th>Brand</th>
<th>Brand Type</th>
</tr>
</thead>
<tbody class="table-content">
#foreach($report_data as $index => $item)
<tr>
<td>{{ $index+1 }}</td>
<td rowspan="2" style="text-align:left;">{{ $item->customer_fullName }}</td> //rowspan should be in some condition
<td>{{ $item->customer_phone }}</td>
<td>{{ $item->customer_detail_licensePlate }}</td>
<td>{{ $item->vehicle_brand_name }}</td>
<td>{{ $item->vehicle_model_name }}</td>
</tr>
#endforeach
</tbody>
</table>
new to Laravel, and just having this problem that I couldn't figure it out.
I have a post controller set up to query out all blog posts. On the database, I have a integer column and named as "status", 0 as inactive, 1 as active. The question is how and where to place the logic to check whetherthe status is 0 or 1, and show "inactive" or "active" in the view. Thanks in advance.
public function index()
{
$posts = Post::where('user_id', 1)->orderBy('id', 'desc')->paginate(5);
return view('post.index')->withPosts($posts);
}
Here is the view
<table class="table">
<tr>
<th>Date Created</th>
<th>Title</th>
<th>Status</th>
</tr>
#foreach ($posts as $post)
<tr>
<td>{{datePretty($post->created_at) }}</td>
<td>{{ $post->title }}</td>
<td></td>
</tr>
<br>
#endforeach
</table>
you can have that logic inside your blade template:
#foreach ($posts as $post)
<tr>
<td>{{datePretty($post->created_at) }}</td>
<td>{{ $post->title }}</td>
<td>#if($post->status) active #else inactive #endif</td>
</tr>
<br>
#endforeach
I want to add pagination to sample table 'passenger'.I have downloaded pagination package by php artisan vendor:publish --tag=laravel-pagination .In my controller code I have added paginate function.table displays one row but pagination controller is not shown (<<1 2 3>>).I want to show pagination control. How can I get pagination control.
Contrller travelApprovalController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class travelApprovalController extends Controller {
public function index(){
$users = DB::table('passenger')->paginate(1);
return view('approval_view',['users'=>$users]);
}
}
approval_view.vlade.php
<table border = 1>
<tr>
<td>ID</td>
<td>Passanger Name</td>
<td>Destination</td>
<td>Created Date</td>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->p_id }}</td>
<td>{{ $user->p_name }}</td>
<td>{{ $user->destination }}</td>
<td>{{ $user->created_date }}</td>
</tr>
#endforeach
</table>
web.php (route)
Route::get('view-records','travelApprovalController#index');
<table border = 1>
<tr>
<td>ID</td>
<td>Passanger Name</td>
<td>Destination</td>
<td>Created Date</td>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->p_id }}</td>
<td>{{ $user->p_name }}</td>
<td>{{ $user->destination }}</td>
<td>{{ $user->created_date }}</td>
</tr>
#endforeach
{{$users->links()}}
</table>
I had used a custom query in my controller in laravel here is my code in my controller:
public function c_viewSystemUser()
{
$title = "View System Users";
$jSu = DB::select(DB::raw("SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID"));
return View::make('ssims.view_systemUser',compact('title','jSu'));
}
I wanted to display its result in my blade file view_systemUser
here is my code inside the view:
#if ($jSu)
<table class="table table-striped table-hover" id="detailTable">
<thead>
<tr>
<th>System User ID</th>
<th>SystemUser Type ID</th>
<th>System UserName</th>
<th>System User Description</th>
</tr>
</thead>
<tbody>
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->dbo_systemusers.SystemUserID }}</td>
<td>{{ $vu->dbo_systemtypes.SystemType }}</td>
<td>{{ $vu->dbo_systemusers.SystemUserName}}</td>
<td>{{ $vu->dbo_systemusers.SystemUserDescription}}</td>
</tr>
#endforeach
</tbody>
</table>
And I am having an error:
Undefined property: stdClass::$dbo_systemusers (View:
C:\xampp\htdocs\laravel3\app\views\ssims\view_systemUser.blade.php)
Any suggestions?
Try this:
#foreach ($jSu as $key => $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach
You forgot to join dbo_systemusers with dbo_systemtypes.
Try this code
SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName from dbo_systemusers INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID
And Try this on View page
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach