Laravel 4 pagination - laravel

My pagination doesn't seem to work. Here's how I done it:
$players = Auth::user()->players->paginate(2);
And then done all the stuff that's needed in the view, but:
Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()
It shows the line, where that variable is.

Try
Auth::user()->players()->paginate(2);
Auth::user()->players returns a collection, the paginate function belongs to the Builder class of Eloquent.

Nevermind, solved it myself:
Solution:
Auth::user()->players()->paginate(2);

Related

Scope left Join Sub doesn't work - Laravel

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

How to use orderBy with Laravel 6 LazyCollection?

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();

Method paginate does not exist in laravel collection

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);

Is there a way to tell if this object is an eloquent object or not?

I'm trying to generalize turning arrays into json and it's getting a little messed up when I added eloquent models into the mix.
Is there a way to tell if this object is an eloquent object or not?
Like an isEloquent() function or something.
or you can use this
$isEloquent = ($yourObject instanceof Illuminate\Database\Eloquent\Model);
You can use the PHP function is_subclass_of
$isEloquent = is_subclass_of($yourObject, 'Illuminate\Database\Eloquent\Model');

Joomla 2.5 Load custom field in components layout fatal error

Im trying to load custom field in my backend component default view (default.php):
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
$productType = JFormHelper::loadFieldType('ProductType',false);
$productTypeOptions = $productType->getOptions();
But I get a fatal error:
Fatal error: Call to a member function children() on a non-object in xxx\libraries\joomla\form\fields\list.php on line 89
When I load this custom field into the form, everything works perfectly.
Any ideas?
Make sure you are adding the correct path to the fields
In your $productType->getOptions() function,
Try removing:
$options = array_merge(parent::getOptions(), $options);
Well, I tried to expand my fellow above idea but it seemed an inappropiate edit, I put it here then:
This worked for me. In your getOptions, if you have something like the getOptions found here (http://docs.joomla.org/How_to_add_custom_filters_to_component_admin), you´ll have this line:
$options = array_merge(parent::getOptions(), $options);
This is the one that makes the error. Why? Well, I´m not sure. If you see the related file, you´ll find this:
foreach ($this->element->children() as $option)
So the problem is that you are calling children() on the model parent that it seems is not initialized. Why the array_merge is needed? Is discussed here (http://forum.joomla.org/viewtopic.php?f=626&t=782877)
My explanation is more like a dirty blind patch, but hope it helps to move forward.

Resources