It's been a few days since the Laravel released its latest version 6. I am trying to implement Laravel LazyCollection instead of the normal Collection class.
Following line works-
Drug::where('deactive',0)->orderBy('code')->get();
And when I use
Drug::cursor()->where('deactive',0)->orderBy('code')->get();
I get an error Method Illuminate\Support\LazyCollection::orderBy does not exist.
Can anyone help me how to use OrderBy with LazyCollection?
UPDATE
After getting the answer here is the correct syntax of above query
Drug::cursor()->where('deactive',0)->sortBy('code');
Offcourse, orderBy method is Query builder method. You can use sortBy as defined in the docs
I think you can do this.
Drug::where('deactive',0)->orderBy('code')->cursor();
Related
I'm new to Laravel I'm making a clone of Twitter. I'm making a scope to get all the likes from the DB, but I get an error from Tinker
I know some basic SQL Queries, but this one is quite complicated, so I've got no idea what to do now.
Tweet model
public function scopeWithLikes(Builder $query)
{
$query->leftJoinSub(
'select tweet_id, sum(liked) likes, sum(!liked) dislikes from likes group by tweet_id',
'likes',
'likes.tweet_id',
'tweet.id'
);
}
Tinker command
App\Tweet::withLikes()->first();
Tinker error
TypeError: Argument 1 passed to App/Tweet::scopeWithLikes() must be an
instance of Illuminate/Database/Query/Builder, instance of
Illuminate/Database/Eloquent/Builder given, called in
C:/wamp64/www/laravel/tweety/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
on line 1164
I hope I explained it well, but if you need more information please ask me.
Thanks for your answer!
i think that you are using the wrong class for your scope, scope use
Illuminate/Database/Query/Builder
as a parameter while you pass
Illuminate/Database/Eloquent/Builder
in your Twit model file, on the top ...
remove:
use Illuminate/Database/Eloquent/Builder;
and paste:
use Illuminate/Database/Query/Builder;
Did this solve the problem? Because I run into exactly the same issue.
When I change Eloquent to Query, the error message is still the same.
Also the source that is given on Git uses use
Illuminate\Database\Eloquent\Builder;
Hubert
hi I want to paginate rows by relation by writing some codes like this
$jobs=auth()->user()->employer->jobs::paginate(10);
I tried this code but I got this error
Method Illuminate\Database\Eloquent\Collection::paginate does not
exist.
The last part should probably be ->jobs()->paginate(10);.
If you write relation without parentheses "()" it will return collection , while with "()" will return models that paginate methods available on it.
So your code should :
$jobs=auth()->user()->employer->jobs()::paginate(10);
Try this:
$jobs=auth()->user()->employer->jobs->paginate(10);
I'm working with legacy code. The old report engine uses associative arrays, Laravel's query builder returns an array of objects.
I need to turn objects into arrays. I've tried using:
\DB::connection('tars-test') //->setFetchMode(PDO::FETCH_ASSOC)
but that gets me Class 'App\Http\Controllers\PDO' not found
It's been suggested to put ->all() at the end of the query but that throws error Call to a member function all() on array
The most efficient way would be to set the fetchmode at runtime, for the legacy function and just for the legacy function. How do I do it?
You can use 'toArray' method:
https://laravel.com/docs/5.3/collections#method-toarray
Laravel 5.3 and lower
You went the right way but as you can see laravel is trying to find PDO in App\Http\Controllers\PDO which probably means you forgot to add use Illuminate\Database\PDO;
Laravel 5.4 and up
Since laravel 5.4 this is not an option. But you still can set fetch mode globally: Laravel 5.4 - How to set PDO Fetch Mode?
Or if you still wish to change it just locally:
Return values only (no keys/associative array) in Laravel
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
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