Laravel: paginating a fluent query - php

I'm trying to paginate a page in my view like this:
#foreach($tasks as $task)
{{ $task->user_id }}
{{ $task->client_id }}
{{ $task->description }}
{{ $task->duration }}
{{ link_to_route('clients.show', 'View client', array($task->client_id), array('class' => 'btn btn-primary')) }}
#endforeach
{{ $tasks->links() }}
Using the following query in my controller:
$tasks = DB::table('tasks')
->join('users', 'tasks.user_id', '=', 'users.id')
->join('clients', 'tasks.client_id', '=', 'clients.id')
->select(array('tasks.description', 'tasks.duration', 'tasks.client_id', 'tasks.user_id', 'users.email', 'clients.name'))
->where('tasks.group_id', '=', $usergroup)
->orderBy('tasks.created_at', 'DESC')
->paginate(20);
return View::make('tasks.index', compact('tasks'));
It shows the tasks fine but there's no pagination link showing up so I can't head over to the next batch of 20 results.
Any ideas on how I can make this work?
I've tried #foreach($tasks->result as $task) in my view as suggested in http://forums.laravel.io/viewtopic.php?id=4092 but it gives me an error "Undefined property: Illuminate\Pagination\Paginator::$result"

For those playing at home - I discovered the answer to this:
The compact function is converting the object to an array. Change your return view method to:
return View::make('tasks.index')->with('tasks', $tasks);
And you're in the clear!
Another point - if you're using Bootstrap 3 RC1 like me you'll find that pagination breaks because of the way BS3 styles pagination - if you have that issue head over here for the solution: https://github.com/laravel/laravel/issues/2215
:)

Related

laravel 8 join query table.column_name does not work in blade or controller

here is my code:
$data = Courses::select('courses.id','courses.standid', 'courses.publisher', 'courses.coverpic', 'courses.course_sts', 'standards.standtitle AS stitle', 'subjects.subtitle AS btitle')
->join('subjects', 'subjects.id', '=', 'courses.subjectid')
->join('standards', 'standards.id', '=', 'courses.standid')
->get();
and blade codes are:
#foreach ($data as $row)
<p>{{ $row->stitle }}</p>
#endforeach
the query works fine when i check dd($data) under attributes, but the stitle and btitle columns never shows inside blade as {{ $row->stitle }} or {{ $row->btitle }}
what am I missing or how to collect the joining table column into the blade file?
cheers
Thanks for response. It was the redirect method that taking me back to history page rather then the post update status.
it was return redirect (the_view_name)->with(vars)
instead of return view (the_view_name)->with(vars)
my first experience to ask a question at stackoverflow and resolved by myself.

Laravel 5.4: Avoid html in the Controllers and DB queries in the blade

I have a problem I can not solve. I have a foreach that prints me an HTML every time it finds value in the database, and it all works.
However, I would like to avoid putting html in the controller.php file.
At the moment I did:
$html_console='';
if($article->id_game > '0'){
$prel_console = \DB::table('info_game')
->where('id_game', '=', $article->id_game)
->get();
foreach($prel_console as $name_console)
{
$name_console_game = \DB::table('console')
->where('id', '=', $name_console->id_console)
->first();
$html_console.='<span class="label">'. $name_console_game->abb_cat.'</span>' ;
}
}
While in the blade:
{!! $html_console !!}
I tried to do this in the blade:
#foreach ($prel_console as $name_console)
<span class="label margin-top-5 font-size-10">{{ $name_console_game->abb_cat }}</span>
#endforeach
If I put the foreach in the blade, how do I deal with the query "name_console_game"
If you have a one to many relation between info_game table which should have a InfoGame model and console table with Console model then your could do something like this:
controller:
public function someMethod()
{
// assuming that you already have an $article object
$infoGame = InfoGame::where('id_game', $article->id_game)->get();
return view('some.view', compact('infoGame'));
}
view location views/some/view/blade.php
#foreach($infoGame->console as $name_console_game)
<span>{{ $name_console_game->abb_cat }}</span>
#endforeach

Can't access value from controller (Laravel 5.3)

I want to access values from my query in view.
$bookings = DB::table('bookings')
->join('staffs', 'staffs.id' , '=', 'bookings.staff_id')
->join('customers', 'customers.id' , '=', 'bookings.customer_id')
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
->orderBy('customers.name', 'desc')
->get();
return view('booking.index')->with('bookings', $bookings);
This is simple enough to understand. What I am trying to do is that i want to get start_time, end_time which are in bookings table, name which is staffs table also name in customers table.
Right now I am doing like:
#foreach($bookings as $booking)
{{ $booking->start_time }}
{{ $booking->end_time }}
{{ $booking->name }} // name column which is in customers table
#endforeach
But these things don't seem to work.
It's all because you are using column names instead of aliases which you set.
Take a look at it, by example customers.name is named Customer-Name.
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
If you want to access data you need to do it like that:
#foreach($bookings as $booking)
{{ $booking->start_time }}
{{ $booking->end_time }}
{{ $booking->Customer-Name }} // name of alias
#endforeach
Anyway I don't think that dash in alias is good solution, you should use underscore instead. (customers.name as Customer_Name) and $booking->Customer_Name
Try this
$bookings = DB::table('bookings')
->join('staffs', 'staffs.id' , '=', 'bookings.staff_id')
->join('customers', 'customers.id' , '=', 'bookings.customer_id')
->select('bookings.id', 'bookings.start_time', 'bookings.end_time', 'bookings.service', 'staffs.name as Staff-Name', 'customers.name as Customer-Name')
->orderBy('customers.name', 'desc')
->get();
return view('booking.index')->with(compact('bookings'));
The $bookings contains an array of data. You can print these in your view ike this..
#foreach($bookings as $booking)
{{$booking->start}}
{{$booking->end_time}}
#endforeach

Search query lost on pagination in laravel

I'm quite new to laravel and backend stuff altogether so this might be a newb question!
My website has a search engine which is used to look up users.
Right now I'm using a pretty simple search controller, below is the code.
class SearchController extends Controller {
public function getResults(Request $request) {
$query = $request->input('query');
if (!$query) {
return redirect()->route('home');
}
$users = User::where(DB::raw("CONCAT(first_name, ' ', last_name)"), 'LIKE', "%{$query}%")->where('role', '=', 2)
->orWhere('username', 'LIKE', "%{$query}%")->where('role', '=', 2)
->orWhere('profile_text', 'LIKE', "%{$query}%")->where('role', '=', 2)
->orWhere('keywords', 'LIKE', "%{$query}%")->where('role', '=', 2)
->simplePaginate(1);
return view('search.results')->with('users', $users);
}
}
And the results page:
#extends('templates.default')
#section('content')
<h3>Results for "{{ Request::input('query') }}"</h3>
#if (!$users->count())
<p>No results found, sorry.</p>
#else
<div class="resultRow">
<div class="">
#foreach ($users as $user)
{{ $user->username }}
#endforeach
{!! $users->render() !!}
</div>
</div>
#endif
#stop
So if I were to search "John", I'd get the result of all the Johns, with the URL being http://localhost/search?query=John .
However, if I were to click on the next page of results (http://localhost/search?page=2), the query is lost, so my search controller just sends me back to my home page.
How do I keep the query through pagination?
EDIT: I'm pretty sure my problem is that it's going through the search controller once again after it click on page 2, but I have no idea how I would fix that.
Switched
{!! $users->render() !!}
with
{!! $users->appends(Request::except('page'))->render() !!}
Works like a charm. Credit goes to : Laravel 5 route pagination url encoding issue (unrelated issue)
in your getResult function, pass a $query variable to the view
return view('search.results', ['users' => $users, 'query' => $query]);
Then in your view, appends the query or other variables you need
{{ $users->appends(['query' => $query])->links() }}
you can check more in laravel docs
You can use paginate the following way.
$users->withQueryString()->links()

Laravel Count with where inside blade

I'm trying to get the total comments the user have..
Controller:
public function index()
{
$setting = Setting::findOrFail(1);
$comments = Comment::where('published', '=', '1')->get();
$users = User::all();
return view('page.index', compact('setting', 'comments', 'users'));
}
View:
#foreach($comments as $comment)
{{ count($users->where('user_id', '=', $comment->user_id)) }}
#endforeach
The problem is that it only returns 0 and i have 2 comments there.. even using the user id to instead of "$comment->user_id" it doesnt work. still display 0.
$users is a collection, not a query. Treat it as such:
#foreach ($comments as $comment)
{{ $users->where('user_id', $comment->user_id)->count() }}
#endforeach
The collection's where method does not take an operator.
From the wording in your question it seems you actually want it the other way around:
$comments = Comment::wherePublished(1)->get()->groupBy('user_id');
Then in your view:
#foreach ($users as $user)
{{ $comments->has($user->id) ? count($comments[$user->id]) : 0 }}
#endforeach
I'm late to answer your question and Joseph already showed you the problem but you may also do it differently. You can group comments using Collection::groupBy, for example:
$comments = Comment::all()->groupBy('user_id');
Then in your view you may try this:
#foreach($comments as $user => $userComments)
// count($userComments)
// Or
#foreach($userComments as $comment)
// {{ $comment->title }}
#endforeach
#endforeach
In the first loop (#foreach($comments as $user => $userComments)), the $user is the user_id and it's value would be all comments (an array) by this user but in group.

Categories