Eloquent in Laravel - laravel

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(),
]);

Related

How to change url in pagination?

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

Laravel paginate not working using with query

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 5.3 Best practices - load lists from other models to populate dropdowns

i'm new to Laravel and i'm using InfyOm laravel generator to create an app.
I came to an issue and would like to know what is the best practice to do this:
I have a model "Mission". When creating a mission, it need to have a client id and agent id associated with it.
In my view, i want to display 2 dropdown lists, one containing all the active clients (id + name) and the other containing all the active agents (id + name).
my controller
public function create()
{
return view('missions.create');
}
my view blade
{!! Form::select('client_id', ?????? , null, ['class' => 'form-control']) !!}
Thanks for your suggestions
Most convenient way is to use pluck() method to build a list from a collection, for example:
public function create()
{
return view('missions.create', [
'users' => User::pluck('name', 'id')
]);
}
Then use $list variable:
{!! Form::select('client_id', $users , null, ['class' => 'form-control']) !!}

How to show data by id in laravel 5.2

I want to show data by id. If id=1 its need to show only id=1 data and if id 2 its should show only id=2 all data.
My controller method:
->with('classroom',classroomModel::find($id)->first());
Here is my view:
<li>Class code-->{{$classroom->class_code}}</li>
<li>Subject-->{{$classroom->subject_name}}</li>
<li>Section-->{{$classroom->section}}</li>
now it is only show id=1 data. If i view id=2 data then it's show id=1 data. What should to do in my controller?
You should pass variable to your controller's function, to do that you can make a link in your view:
Id 1
Id 2
Routes:
Route::get('/something/{id}', [
'uses' => 'somecontroller#yourfunction',
'as' => 'name'
]);
Function:
public function yourfunction($id)
{
// you can use $id here and return view with classroom
}
I Hope this example will help you.

How can I paginate two Eloquent collections on a single page with Laravel?

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.

Resources