I am trying to load a paginated data, so far able to load the whole records.
Now not sure how to load the Laravel paginated data, as when I send call without pagination I get a response like
{
'users':[{
'id':1,
'name':'Aamir'
},{
'id':2,
'name':'Jamshed'
}]
}
But when I enable pagination, there is a whole bunch of data with this, and ember starts to give an error
GET http://localhost:8000/api/users?citySef=Lahore&group=Ap
200 OK
87ms
vendor.js (line 9861)
WARNING: Encountered "total" in payload, but no model was found for model name "total"
This has to be fixed on the laravel end,
$city = blood_cities::where('sef' , '=', [$citySef])->get();
$users = blood_users::where('city_id', '=',$city[0]->id)
->where( 'group', '=', $group )
->paginate(15);
$return = array(
'users' => $users->items(),
'meta'=> array(
'total'=>$users->total(),
'current_page'=>$users->currentPage(),
)
);
return $return;
now ember will not give errors we will get the result.
Update: 2015-05-25
able to create pagination from http://blog.ksol.fr/paginated-apis-with-ember-js/
Related
I Am using in Laravel 8 app next lines of codes, the problem is it is not filtering anything.
When I am using whereIn instead my app returns items as expected but when I add whereNotIn something goes wrong and not returning items opposite items to whereIn but all items.
$callback = function ($query) {
$query->whereHas('documents', function($query) {
$query->whereNotIn('document_type_id', $this->negative_documents);
});
};
$companies = $companies->whereHas('people', $callback)->with('people', $callback);
Any ideas?
Currently my index method works and looks like this:
return view('books.index', [
'books' => Book::with(['author', 'publisher'])->paginate(15)
]);
But I would like to use filter() to filter results upon request, this is my best shot so far:
return view('books.index', [
'books' => Book::latest()
->filter(request(['search']))
->with(['author', 'publisher'])
->paginate(15)
->withQueryString()
]);
But I keep getting the error:
Call to undefined method Illuminate\Database\Eloquent\Builder::filter()
Any help is greatly appreciated!
In Laravel you have a filter helper. But this works only in collection and not as query builder.
$books = Book::latest()
->with(['author', 'publisher'])
->paginate(15);
$filteredBooks = $books->filter(function ($value) {
return $value == request(['search']);
})
dd($filteredBooks);
I need to fetch the data from database and display it view .
This is my web.php
Route::get('/businesscard/{name}', function ($name) {
//$username= App\users::where('username', $name);
$username=DB::table('users')->select('*')
->where('username', '=', $name)
// ->groupBy('status')
->get();
return view('auth.pro')->with(['username' => $username]);
return array(
'name' => $name
);
});
If the user enters domain.com/businesscard/username I need to fetch the data for the username and display it in view .It is working.But I need to remove businesscard .User need to enter domain.com/username. I have tried the below code.
Route::get('/{name}', function ($name) {
//$username= App\users::where('username', $name);
$username=DB::table('users')->select('*')
->where('username', '=', $name)
// ->groupBy('status')
->get();
return view('auth.pro')->with(['username' => $username]);
return array(
'name' => $name
);
});
If there is data it is working .but other pages are not working like login and register .Users are entering their username in registration .
The order of your route matters. See order of route declarations in laravel package
So the /{name} should be registered as the last route to avoid matching for other routes.
/{name} it means / with any value. If you try /login or /register.Then your logic is confused with this so that other pages are not working. Best way to develop as you expect like first one.
Another thing in your code there is two return second one is not doing anything. After the first one it return to view so second one unused. remove that return as well.
I can view all my posts and include their respective owner and category in the query.
public function index()
{
$posts = Post::with('user', 'category')->get();
return response()->json([
'posts' => $posts,
], 200);
}
Note: I used the with helper because thr front-end of the site is in vuejs.
Now I want to add pagination in my code but I get the following error:
"Method Illuminate\Database\Eloquent\Collection::with does not exist."
This is what I tried:
$posts = Post::paginate(2)->with('user', 'category')->get();
How can I use the laravel pagination?
For get the result you must be use paginate, get, first methods the end of the query
Post::with('user', 'category')->paginate(2);
Laravel Cache::remember is returning a LengthAwarePaginator object as an array.
function getNotifications( $userID ) {
$values = Cache::remember('cache-key', 10, function() {
$query = DB::table( 'user_notifications' )
->leftJoin( 'notifications', 'user_notifications.notification_id', '=', 'notifications.id' )
->where( 'user_notifications.user_id', $userID )
->select( 'notifications.*' )
->orderBy('created_at', 'DESC')
->paginate(5);
return $query;
});
return $values;
}
If I dd($query) before returning from Cache closure, it's returning the following object, that accepts $value->links() to display pagination.
But whenever the Cache is storing $query into $values it's returning values as an array:
I tried commenting out the unserialize-block:
/*foreach( $values as $key => $value ) :
$values[$key]->meta = self::maybeUnserialize($value->meta);
endforeach;*/
and confirmed that, that's not the cause.
I also tried, but failed:
$values = collect($values);
With multiple check and cross-check I am confirming that, the issue is the Cache::remember.
How can I force the Cache::remember return things as it is? So that I can let $object->links() work for me.
The actual code can be found here.
The issue is, Cache is made to store data, not the instance. So there are two ways to do this:
When caching, cache information per page, or
Get all the data while fetching, but make your own pagination
Solution 1:
We went for the second. But in case you need to go for the first solution, here's the code, I got from Laracasts, provided by chrisml:
$statuses = Cache::remember("statuses_{$id}_page_{$page}", 3, function() use ($event, $sort) {
return $event->statuses()
->with('comments')
->latest()
->paginate(10);
});
On the above code, the cache key is changing on every page, so the cache is storing per page.
Solution 2:
But for my case, we thought we should go for the second, and that would be wise for us, in our case. So, we've to make our own pagination. My luck that psampaz did the base for us on their blog:
Custom data pagination with Laravel 5 — by psampaz
So, instead of using ->paginate() we're fetching all the data first, and caching them as previous.
$values = Cache::remember('cache-key', 10, function() {
$query = DB::table( 'user_notifications' )
->leftJoin( 'notifications', 'user_notifications.notification_id', '=', 'notifications.id' )
->where( 'user_notifications.user_id', $userID )
->select( 'notifications.*' )
->orderBy('created_at', 'DESC')
->get(); // <----------- here
return $query;
});
But before returning the $values, we're making our own pagination. We made some fixes to the psampaz's code:
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
function getNotifications( $userID ) {
// Collapsed the cached values here :)
$values = Cache::remember('cache-key', 10, function() {...});
// Get current page form url e.g. &page=6.
$currentPage = LengthAwarePaginator::resolveCurrentPage();
// Get current path.
$currentPath = LengthAwarePaginator::resolveCurrentPath();
// Create a new Laravel collection from the array data.
$collection = new Collection($values);
// Define how many items we want to be visible on each page.
$perPage = 5;
// Slice the collection to get the items to display on the current page.
$results = $collection->slice(($currentPage - 1) * $perPage, $perPage)->all();
// Create our paginator and pass it to the view.
$values = new LengthAwarePaginator($results, count($collection), $perPage, $currentPage, ['path' => $currentPath]);
return $values;
}
And finally, we can easily use the $object->links() for pagination, and it's awesome! :)