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);
Related
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 am trying to get all the results where the user's status is teacher (which is a field in my table).
When I tried to use pagination, it runs into an error.
Here is my code in the controller where I'm fetching the results:
public function index()
{
$status = User::where('status', '=', 'teacher')->get();
return view('Profile.alluser')->with('status', $status)->paginate(3);
}
*note laravel version is 5.8
You need to paginate a Query, while you are trying to paginate a view, please try the code below:
public function index()
{
$status = User::where('status', '=', 'teacher')->paginate(3);
return view('Profile.alluser')->with('status', $status);
}
I am working on project useing laravel and vuejs the problem is that I am trying to select multi rows and delete them the error is
No query results for model [App\Models\Country] delete_all
my route link
Route::delete('/delete_all','CountriesController#deleteAll');
delete function in the country controller
public function deleteAll(Request $request){
$ids = $request->ids;
DB::table("countries")->whereIn('id',explode(",",$ids))->delete();
return response()->json('Selected Countries Deleted Successfully',200);
}
the delete method in the component
delAll(){
axios.delete('/dashboard/countries/delete_all', {
ids: this.selected
}).then(response => {
toastr.success(response.data);
})
}
where can I find the Error
just trying
DB::table("countries")->whereIn('id',explode(",",$ids))->get()->delete();
or simple
Countries::destroy(explode(",",$ids));
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.
$category = Category::all();
$product = Product::all()->where('category_id', $category->name)->get();
Change category id from its name and link two tables in one in Laravel 5.5.
kindly refer to Laravel documentation regarding setting up relationships:
https://laravel.com/docs/5.5/eloquent-relationships
The relation between Category & Product is one to many, so it will be:
// app/Category.php
public function products()
{
return $this->hasMany(Product::class);
}
You need to return a view and pass the data instead of returning a collection:
return view('some.view', [
$category => Category::all();
$product => Product::where('category_id', $category->name)->get();
]);
https://laravel.com/docs/5.5/views#passing-data-to-views
Also, when you do this:
$product=Product::all()->where('category_id',$category->name)->get();
You're loading all products from DB into memory and then filtering them because all() is executing the query. Do not do this.