In my Laravel 5.7 app I have news page
http://mysite/all_news
with pagination implementation as:
$newsList = PageContent
::select(\DB::raw(' page_contents.*, users.username'))
->getByPageType( 'N' )
->getByPublished( true )
->orderBy('created_at', 'desc')
->join(\DB::raw('users '), \DB::raw('users.id'), '=', \DB::raw('page_contents.creator_id'))
->paginate( 4 , null, null, $page)
->onEachSide();
and route defined as :
Route::get('all_news', array(
'as' => 'all_news',
'uses' => 'PageController#all_news'
));
and in view I show pagination :
{{ $newsList->appends([])->links() }}
But rendered links in pagination looks like(and how to render them ?):
http://mysite/all_news?=2
How to make urls look like
http://mysite/all_news/2
?
Thanks!
Take a look at this:
https://laravel.com/docs/5.7/pagination#displaying-pagination-results
Specifically:
"Customizing The Paginator URI":
$users->withPath('custom/url');
The withPath method allows you to customize the URI used by the
paginator when generating links. For example, if you want the
paginator to generate links like
http://example.com/custom/url?page=N, you should pass custom/url to
the withPath method
Related
i have a problem with this eloquent. how i can pass only the blog that have the attribute categoryid that same as the id that clicked from index.blade.php ?
This is the code of show function in CategoryController.php, the parameter is the id and comes from the index.blade.php
This is the picture of show.blade.php , the page after click button from index.blade.php
You forgot the get() method after the where method on your CategoryController, something like this:
return view('categories.show', [
'category' => $category,
'blog' => Blog::where('categoryid', $category->id)->get(),
]);
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.
For a sample link,
https://ddskart.com/product/COPIER%20TONER/NP-6/7/8000/71
How do I construct a GET route ?
the route must follow the given format.
Route::get('product/{category}/{model}/{product_id}', function ($category, $model, $product_id) {
// do whatever ...
});
where the parameters could be
$category = 'COPIER TONER';
$model = 'NP-6/6/7/8000';
$product_id = 71;
Well, You can use regular expression constraints in you routes. Refer Laravel Regular Expression Constraints.
Route::get('product/{category}/{model}/{product_id}', function ($category, $model, $product_id) {
// do whatever ...
})->where([
'category' => '[\w\s]+',
'model' => '([a-zA-Z]+)\-(\d+)\/(\d+)\/(\d+)',
'product_id' => '[\d]+'
]);
Please note that in above example provided regular expressions for category, model, and product_id parameters are to match the given url in the question. You can adjust them according to your logic. The point is you can take advantage of Laravel regular expression constrains while building complex routes.
use urlencode() and urldecode() php functions.
I Laravel 5.5 I am returning users information like this...
$users = User::all();
return Response::json(array(
'error' => false,
'response' => $users,
));
I have a belongs to many categories relationship setup and would like to also show all of the categories each user belongs to.
Anyone have an example I can see?
Use the with() method to load categories for each user:
$users = User::with('categories')->get();
If you don't need to load all the columns from the categories table, use select() inside the with() closure. Also, since you're using Laravel 5.5 you could use Resource classes for formatting JSON.
I have two collections on a single page which should be both paginated. But pagination generates the same Parameter for both (?page=X).
How can I solve that kind of an issue?
You can change the param of either pagination by
Paginator::setPageName('someparam');
Read more about Pagination here In the section Customizing The Paginator URI
Note : You should do this before paginator is done i.e.,
$yourCollection = Model::paginate(10);
Example :
I assume you have two pagination like this
Paginator::setPageName('yourFirstParam');
$firstCollection = FirstModel::paginate(10);
Paginator::setPageName('yourSecondParam');
$secondCollection = SecondModel::paginate(10);
Where you use this to get in your view
Paginator::setPageName('yourFirstParam');
$firstCollection->links();
Paginator::setPageName('yourSecondParam');
$secondCollection->links();
There is a way to "automatically" set the page name (in a sense), which I'll get to in a bit.
First, if we go over the paginate method, you'll see that it accepts a pageName argument as its 3rd parameter:
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
Lets say you have a User and Post model. You can then do something like this in your controller:
$users = User::paginate(10, ['*'], 'users');
$posts = Post::paginate(10, ['*'], 'posts');
return view('example', compact('users', 'posts'));
It works like your normal pagination except the second argument specifies the columns you want to select and the third argument specifies the page name.
In your view, when you render your pagination links, you might run into a problem when you do this:
{!! $users->render() !!}
{!! $posts->render() !!}
While the pagination links will be rendered, when you click on a link to a posts page, the users query string parameter is gone. Therefore, the users are back to page one and vice versa.
To fix this, you can use the appends method to keep the query parameters for both models:
{!! $users->appends(['posts' => Request::query('posts')])->render() !!}
{!! $posts->appends(['users' => Request::query('users')])->render() !!}
All this works, but it's a bit ugly so how can we clean this up? You can create your own method to "automate" this process. In your model, you can add your own paginate method:
// Name it whatever you want, but I called it superPaginate lol
protected function superPaginate($perPage)
{
return $this->newQuery()->paginate(10, ['*'], $this->getTable());
}
This will automatically set the pagination name to the model's table name. So for the User model, the page name will be "users". For the Post model, the page name will be "posts".
There's still the problem with rendering links. You don't want to call appends all the time and specify the query parameters. To fix that, we can improve the superPaginate method into this:
protected function superPaginate($perPage, $columns = ['*'], $page = null)
{
$params = \Request::query();
return $this->newQuery()->paginate(10, $columns, $this->getTable(), $page)->appends($params);
}
Now, all you need to do is Model::superPaginate(10); and $models->render(). Everything should work properly.