Laravel Paginate not working with specific model or table - laravel

Paginate not working for specific model. with all Models, paginate is working fine except one specific model. Also tried with query builder.
$doubts = DB::table('users')->paginate(10);
dd($doubts->count());
//output: 10
$doubts = DB::table('doubts')->paginate();
dd($doubts->count());
//output: 0
$doubts = DB::table('doubts')->get();
dd($doubts->count());
//output: 8
Don't know what am I missing here. Please help.

Maybe I think you have to put
How many results you want to show per page
So if you want 8
Then I think it should be something like this
$doubts = DB::table('doubts')->paginate(8);
dd($doubts->count());
And if you are using pages Number under your data then you might need the query builder.

You should pass number argument inside paginate function.
Look at laravel pagination documentation
And make sure that DB::table('doubts')
Is returning collection

I thing you must add parameter in paginate(), to show how much data that show per page. You can read documentation in : https://laravel.com/docs/9.x/pagination#paginating-query-builder-results
https://laravel.com/docs/9.x/pagination#cursor-pagination
because you are not add parameter in paginate, it means not data that show every page.
$doubts = DB::table('doubts')->paginate(10);

Related

How to get attribute from a collection, with multiple objects inside of an array in Laravel?

I have a collection that contains an array with 2 objects in it. I'm trying to get the email attribute, right now there are only 2 objects, but there could be 100 objects, so I'm not sure if I need a loop or if I need to use array_merge, or something.
I've attached a screenshot of the data, I'm not showing the attributes, but just know that one of them is email.
I'm die/dumping a variable called $tokens.
In the end, it should return a list of emails, for example:
john.doe#email.com
jane#email.com
thomas.brown#email.com
And I need this logic in the controller, not in the view (.blade), because I'm trying to save this data in a csv file, using fputcsv, that's the main objective and idea here.
Thank you to #jagcweb for the inspiration, his code/answer just needed a couple of tweaks. The following works and does what I was trying to do:
$emails_array = array();
for($i=0; $i <= sizeof($tokens)-1; $i++) {
array_push($emails_array, $tokens[$i]["email"]);
}
Now the $emails_array contains an array with both emails in it.
This sounds like what pluck is for:
$tokens->pluck('email')
Laravel 8.x Docs - Collections - pluck

Magento rest api: how to get orders by ids?

How to get multiple orders filtered by Id from magento with help of rest api?
I tryed to use http://192.168.0.104:4422/magento/api/rest/orders?filter[1][attribute]=entity_id&filter[1][in]=1&filter[1][in]=3
but it returns me:
<magento_api>
<messages>
<error>
<data_item>
<code>401</code>
<message>oauth_problem=signature_invalid</message>
</data_item>
</error>
</messages>
</magento_api>
But if i use request with only one filter in parameter http://192.168.0.104:4422/magento/api/rest/orders?filter[1][attribute]=entity_id&filter[1][in]=1 - it works great.
For me the problem was in filters( I tried on 1.7.0.1). Looks like some magento versions have problems with filters. So you should use 1 filter, after that filter results in your code.
I found the answer in this blog post. In short, if you follow this structure (this was for my use case with a list of products):
http://magentohost.com/api/rest/products?filter[1][attribute]=entity_id&filter[1][in][1]=1&filter[1][in][2]=3
You'll be set! (this gets two products similar to above with ids 1 and 3). Notice the [1] and [2] after both of the [in]. I wish Magento had better documentation for this.
Try aggregating values.
complexFilter { key = "increment_id", value = new associativeEntity {key = "in", value = "1,2,3"} };

Laravel4 pagination is stuck

I have a quite-complex query with lots of joins and wheres via Eloquent instance which starts with
$user = new User;
$query =User::join('table','users.id','=','table.id');
After that i'm looping and adding lots of ->whereRaw() entries.
In the end of it, im running:
return $query->paginate(30);
on the view itself i'm running
{{ $records->appends($_GET)->links() }}
Everything looks OK but the thing is that after the first page (when I paginate to page #2) the paginator links are stuck on 2 and doesnt go on.
Probably a CodeIgniter thinking tried on Laravel.. ;)
You're passing the page number to appends (as you're passing the whole of $_GET) which is why it gets stuck. Either be more specific with your appends call or use Input::except('page') or similar.

Paginate causes crashing in Laravel 4

I am learning about Laravel 4 and I'm trying its pagination. I created a simple query to test the pagination, yet it always end up hanging. If I use get(), it works fine, but when I replace get() with paginate(), it hangs. Here is my code:
DB::table("samp_tbl")
->select("id","activity")
->whereNull("deleted_at")
->orderBy("id","desc")
->paginate(5);
Could someone tell me what's wrong with my code?
In case anyone else comes across this issue it's because you are using orderBy. In the Note: area on this page http://laravel.com/docs/4.2/pagination#usage it explains that laravel has issues with groupBy. However I also would assume this would go for orderBy as well. Writing a custom query would be recommended in your case.
create a model for your database and it will work fine

Prestashop: Get id of parent category

I've faced the problem while coding my very first prestashop template. So what I want to do is to get the list of all sub categories which are under the same parent category. So running foreach loop I need to send parent category id.
In the internet there's not so much information about prestas' coding, modules or stuff, but I found two possible solutions.
One was: {$product->id_category_default} but that works only inside a product page (?) Maybe I'm wrong and it's possible to get a solution with this?
Another way sounds bit desperate: {$cookie->last_visited_category}
problem with this one, that it works only if you come from parent category directly, but fails when followed by direct link or any other way.
Also I was thinking of writing php function, which sends MySQL query and returns parent category id, but is this optimal way to do? Also I haven't tried writing my own php functions in smarty, seems it woks in some different way than just calling function.
So to conclude, do anyone knows how to get parent category id without separate php function and if it's so desperate, where and how to define your own php functions for smarty and how to call them.
I'm working with prestashop 1.4.
Thank you for your attention.
I've used this code in a module, you might need to adapt it but basically, the id of the parent category is stored in the database.
$parentCategoryList = array(2, 3, 4, 5);
if(!in_array($id_category, $parentCategoryList)){
$parentCategory = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT DISTINCT c.id_parent
FROM '._DB_PREFIX_.'category c
WHERE c.id_category = '.(int)($id_category)
);
$id_category_parent = $parentCategory[0]['id_parent'];
}
else{
$id_category_parent = $id_category;
}
In my code, the $parentCategoryList stores the id of my main categories (change it to your values). Then it checks if the category you're viewing ($id_category) is a main category. If not, it looks for the parent category in the database.
I am not really happy about the use of an array to store manually the main categories, but it works.
Hope this helps!

Resources