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
Related
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);
I am migrating an old TYPO3-extension to the current build and am trying to change my database access to using doctrine. For the most part that worked great, but now I came upon a few select queries, that make use of SQL-functions e.g. "Year(FROM_UNIXTIME())".
I tried using the sql function as is in the following form:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('mytable');
$queryBuilder
->select('i.*','c.title AS cat_title','c.uid AS cat_uid')
->from('mytable','i')
->join('c'
...
)
->join('d'
...
)
->where(
$queryBuilder->expr()->eq('Year(FROM_UNIXTIME(i.datetime))', $queryBuilder->createNamedParameter($year, \PDO::PARAM_STR))
)
The problem is only with the where part, if i get a read-out on the SQL statement the where clause is simply omitted, as if the line of code never happened.
I then proceeded to make debug statements around the where statement and encapsulate it in a try catch block with a log attached to it:
It executes fine, without error, but does nothing.
After google, I now believe, that i would need to implement that function again using the DQL user functions, but the documentation on the typo3 site offers no mention of this and so i am a little unsure as to how to proceed.
Can someone point me in the right direction?
Any help would be much appreciated
Regards
Theo
I defaulted to using php to parse the years to full dates and convert them to unix timestamps. The option of using the ORM was there, but simply too much to handle.
I implemented it as follows:
// to convert towards db:
$queryBuilder->createNamedParameter(mktime(0,0,0,1,1, (int)$year), \PDO::PARAM_INT)
// and back:
$queryBuilder->select('i.datetime AS yearb')
// ... the rest of the query seems of litle interest
;
$current = date("Y",$queryBuilder->execute()->fetch()['yearb']);
You can use plain SQL:
$queryBuilder
->select('i.*','c.title AS cat_title','c.uid AS cat_uid')
->from('mytable','i')
->join('c'
...
)
->join('d'
...
)
->where(
'Year(FROM_UNIXTIME(i.datetime)) = '.$queryBuilder->createNamedParameter($year, \PDO::PARAM_STR)
)
I'm working on a Laravel project and I'm using https://laravel.com/docs/5.3/scout with ElasticSearch on a model Offer.
I already have some offers in my database, so I just run the command
`php artisan scout:import "App\Models\Offer"`
for generate the index for use my datas with ElasticSearch.
After that it's ok, I can search in my offers with, for example :
`$offers = Offer::search($request->keywords)->get();`
Now I have a function for create other offers in my database, and I dont know how can I refresh the index for use these new datas ?
In the documentation https://laravel.com/docs/5.3/scout#adding-records, I can read
all you need to do is save a model instance and it will automatically be added to your search index
I tried this and no, when I save() a new Offer, I can't find it in my index.
I tried to re run the command php artisan scout:import "App\Models\Offer" after add anew OFfer, but it's the same, I can't find it in my index.
Did I miss something ? Any ideas ?
Sorry for a late response on this but I ran into this issue myself when I attempted to use Scout. Everything went fine until I realized that the project's data would scale at a rate that went far beyond what scout could handle. In this case, however you can use the push() method instead of save(). I'm not sure why this isn't documented at all and it's rather frustrating but there's an answer at least.
So use:
->push()
instead of:
->save()
to update your indexes.
If that does not work for your specific version there is another way you can do it but it is "slightly" redundant. It involves a mix of using the queue system with the Artisan system and a command. In this you:
Create a queue/job php artisan make:job JobNameHere (As of Laravel 5.2 - 5.4)
Add use Artisan; to the top of that newly created Job Class so you can pull in the Artisan's functionality
In the handle of that Job Class add
class JobNameHere implements ShouldQueue {
...
...
public function handle() {
Artisan::call('scout:import', ['name' => "App\YourModelNameHere"]);
}
}
Add a dispatch call to that job class right after your DB push() process is called.
Example:
class YourController extends Controller {
public function yourUpdateMethod(Request $request) {
//Some code you wrote
//Some more code you wrote
$update_obj->push( $array_to_update_obj);
dispatch(new JobNameHere());
}
}
Test your index by searching on it
If all went well then you should no longer get any errors. Please leave a comment and let me know how it went... provided you're still having this issue.
I would also like to mention that Laravel Scout does not support ElasticSearch anymore as of August of 2016 (I believe). No one really knows why the support was removed but there are a few tutorials out there to help you get Laravel and Elastic search working together again.
I will also note, based on my research, that if your project is small then you should be fine to use Scout and not need to use ElasticSearch. If your project is going to get huge, then you're probably better off finding a package that supports and well documents how handle Laravel's relationships between models. Elastic search is capable of accomplishing this but there are tons of docs that make figuring it out difficult. Here are some semi-decent tutorials to help get you going down the right path.
https://tirdeamihai.com/blog/laravel-and-elasticsearch
https://laravel-news.com/laravel-and-elasticsearch
Plastic is a package that I would currently recommend as it's being actively worked on. Elasticquent hasn't been touched or updated since last year in June.
https://github.com/sleimanx2/plastic#1---create-a-new-index
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.
I read that Laravel 4 is not going to change much on the surface, saying that much of the old code would be compatible.
I try to use Fluent's insert_get_id like it says in the docs, but the function doesn't exist.
Am I doing it wrong? If not, are there more changes in Fluent and / or Eloquent?
Laravel 4 has been changed to become PSR-1 complaint and as such is now camelCased instead of snake_cased. Try changing to insertGetId().
I believe functions went from underscores to camelCase... try insertGetId