I am trying to make a paging in Laravel, but i keep getting errors.
I try put
->paginate(3)
On the return, but i keep getting errors like Call to undefined method Laravel\Paginator::get() and Call to undefined method Laravel\Paginator::order_by()
public function get_index()
{
$categories = Category::all();
return View::make("stories.index")
->with("title","Sexnoveller")
->with("categories", $categories)
->with("stories", DB::table('stories')
->order_by('id', 'desc')->get());
}
To use pagination call paginate() instead of get(). In your case that would be:
return View::make("stories.index")
// ...
->with("stories", DB::table('stories')
->order_by('id', 'desc')->paginate(3));
Then in the view just remember to iterate over $stories->results.
I do recommend to create a model for that Stories table. Once done, you could do something like:
Story::orderby('story_name', 'ASC')->paginate(10);
Related
I'm trying to get posts on Index page using laravel but getting errors.
public function index()
{
$posts = Post::orderBy('created_at', 'desc');
return view('pages.index')->with('posts', $posts);
}
The above snippet defines what I want to do, but, getting this error on index page:
count(): Parameter must be an array or an object that implements Countable
Same snippet works on other pages but it's not working on index page.
Extract the results using get method like so:
$posts = Post::orderBy('created_at', 'desc')->get();
I'm trying to loop through the items using eloquent in laravel but I'm getting 0. Please see my code below.
Model
Class Store{
public function products(){
return $this->hasMany('App\Product');
}
}
Controller
$products_count = 0;
foreach($store->products() as $product)
{
if($product->status == 1)
{
$products_count++;
}
}
dd($products_count);
Note: I have data in my database.
You can also use withCount method something like that
Controller
$stores = Store::withCount('products')->get();
or
$store = Store::where('id', 1)->withCount('products')->first();
WithCount on the particular status
$stores = Store::withCount(['products' => function ($query) {
$query->where('status', 1);
}
])
->get();
ref: withcount on relationship
That's because $store->products() returns an eloquent collection which doesn't contain the data from the database yet. You need to do $store->products instead.
If you need to get the count from the database then use
$store->products()->where('status', 1)->count()
With the function-annotation (i.e. products()) you are retrieving the \Illuminate\Database\Eloquent\Builder-instance, not the actual Eloquent-collection.
Instead, you would have to use $store->products – then you will get retrieve the related collection.
In Laravel $store->products() makes you access the QueryBuilder instance, instead there is the Laravel way of doing $store->products, which loads the QueryBuilder and retrieves the collection automatically and down the line is easy to optimise.
i tried to use pagination in my laravel view i got this problem
Method Illuminate\Database\Eloquent\Collection::links does not exist. (View: C:\wamp\www\project\resources\views\demmande\demmandes.blade.php)
here is my controller function
public function ViewDemmandes(){
$listdemmande=Demmande::paginate(10)->sortByDesc('created_at');
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = $listvillee;
$demmande = $listdemmande;
$categorie = $listcategorie;
return view("demmande.demmandes",compact('villes','categorie','demmande'));
}
but when i delete sortByDesc function like this
public function ViewDemmandes(){
$listdemmande=Demmande::paginate(3);
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = $listvillee;
$demmande = $listdemmande;
$categorie = $listcategorie;
return view("demmande.demmandes",compact('villes','categorie','demmande'));
}
it works fine please can you help me resolve this problem
The ->paginate(10) will return an instance of LengthAwarePaginator. Which implements all the methods the Collection has (->sortByDesc() being one of them). But calling a collection method will return the underlying collection, not an instance of paginator.
So in your case you're overriding the paginator with the collection being returned from ->sortByDesc().
Sort with SQL instead of on a collection:
$listdemmande = Demmande::orderBy('created_at', 'DESC')->paginate(10);
// Or using `->latest()` shorthand:
// $listdemmande = Demmande::latest()->paginate(10);
If there's a reason why you want to sort after fetching the query, you could override just the paginators underlying collection:
$listdemmande = Demmande::paginate(10);
$listdemmande->setCollection($listdemmande->sortByDesc('created_at'));
You can use like this in controller
$listdemmande=Demmande::orderBy('created_at', 'desc')->paginate(10);
And also don't forget to add in blade view to add this ...
After foreach add to this
{{$listdemmande->links()}}
Your links error is solve.
This work perfect:
public function scopeHBO($query)
{
return $query ->where('network', '=', "hbo");
}
Call in Controller: It Works!
$events = Schedule::HBO()->orderBy('searchdate')->get();
When I add another Query Scope like so:
public function scopeHBO($query)
{
return $query
->where('network', '=', "hbo")
->where('searchdate', '>=', 'NOW()');
}
OR:
public function scopeDate($query)
{
return $query->where('searchdate', '>= ', 'NOW()');
}
Then call in the controller:
$events = Schedule::HBO()->Date()->orderBy('searchdate')->get();
I get an error: Undefined variable: event. I tried with with Raw MySql in the same model and it works. Whenever i add a query scope, does not matter what it is.. i get that same error Undefined variable: event.
NOW() is a function, so you need to use a raw query:
where('searchdate', '>=', DB::raw('NOW()'))
Then you can use the scopes. (Do note that I think scopeDate must be called as date(), not Date() - not 100 % sure on that though.)
This sounds less like a generic problem with Laravel, and more like a problem with you specific application.
My guess (which is a wild guess), is that adding that second where clause in your scope method
return $query
->where('network', '=', "hbo")
->where('searchdate', '>=', 'NOW()');
ended up creating a SQL query that returned 0 rows. Then, somewhere in your other code you're doing something like
foreach($events as $event)
{
//...
}
//referencing final $event outside of loop
if($event) { ... }
As I said, this is a wild guess, but the problem doesn't seem to be your query code, the problem seems to be the rest of your code that relies on the query returning a certain number of, or certain specific, rows/objects.
I have a model called "User", which "belongsToMany" Items.
This relationship works fine, so I can easily do something like this:
User::find(4)->items->find(1)->name
Now, I would like to do something like this:
User::find(4)->items->where('name', '=', 'stick')->get()
I would expect the code to return all the user's items with the name "stick", but unfortunately that is not what happens. I receive this error:
"Call to undefined method Illuminate\Database\Eloquent\Collection::where()"
I also tried to build a query scope:
public function scopeName($query, $name)
{
return $query->whereName($name);
}
The query scope works when I do something like this:
Item::name('SC')->get()
but
User::find(4)->items->name('SC')->get()
still does not work.
Can you help me returning all the user's items, which have the name 'stick'?
If you're looking to just get a single user's items named "stick", this is how you would do it:
$stickItems = Item::whereUserId(4)->whereName('stick')->get();
Here we are using Eloquent's dynamic where methods, but you could rewrite it like so:
$stickItems = Item::where('user_id', '=', 4)->where('name', '=', 'stick')->get();
That should get you what you want.
You have to call the items() method, not use the magic property:
User::find(4)->items()->where('name', 'stick')->get();
// ^^