How to reverse Array of Data in Laravel when using Eloquent with Pagination? - laravel

I am fetching a list of data using Eloquent method and trying to reverse the order of array, I am getting an error.
Here is the code :(Previous Code)
$temp = Product::paginate(20);
New Code
$temp = Product::paginate(20)->orderBy('id','desc');
Error :
Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.
New method :
$temp = Product::paginate(20)->sortDesc();
Error :
Method Illuminate\Database\Eloquent\Collection::appends does not exist.
Can anyone help me?

use below syntax
Product::orderBy('id','desc')->paginate(20);

try to write it in this way,
use orderBy before the paginate
$temp = Product::orderBy('id','desc')->paginate(20);

->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.
Product::latest()->paginate(20)

Related

Best approcah for getting the object in the foreach loop in laravel using eloquent?

I have some properties and i want to get the object for each property, currently, I am using the eloquent in the foreach loop like this as shown in the image that will describe the best..
but it is really not a good approach because if I have 100 published property I will making 100 calls to the DB... and that is not correct?
I need a suggestion and a proper solution to this query?
Thanks in advance
Before foreach you can get all the RentalProperty items from db like this:
$allRentalProperties = RentalProperty::all();
and in foreach loop you can get those items without connecting database like this:
$propertyObj = $allRenatalProperties -> where('id', $property['id']) -> first();
Also, you can use array shorthand.
$newArray = [];
it's much simple and readable.
You can do array pluck before loop:
$propertyIds = Arr::pluck($published_properties, 'id');
Then you can make a whereIn query to get only those data which are in $published_properties object
$propertyObj = RentalProperty::whereIn('id', $propertyIds);
Then you can access that object with id if you change array key with record id.

Laravel Eloquent ORM - How to get the all the properties of afrom/of a collection in a array?

$user_emails = ["email_1#domain.com", "email_2#domain.org"];
$users = Users::whereIn("email", $user_emails);
The table for users also has a phone column for each user. What's the best way to get a list/array of the phone number as an array?
$users->all()->phone(); // Like (which is not correct)
Try to use get() like :
$users = Users::whereIn("email", $user_emails)->get(['phone'])->toArray();
Or also pluck() like :
$users = Users::whereIn("email", $user_emails)->pluck('phone')->all();
Hope this helps.
Use pluck method to fetch a specific column's values and then use toArray on returned Collection object to get results as an array.
$phoneNumbers = Users::whereIn("email", $user_emails)->pluck('phone')->toArray();
You can get all column data with get()
Example:
$user = $user::where('email', $user_emails)->get();
You can get the list with foreach loop method.

Laravel 5.3 Method distinct does not exist

I am trying to write a query that returns the count of distinct links. However, I keep on getting
Method distinct does not exist
Here is my code
$sample = Sample::all();
$uniqueResumes = $sample->distinct()->select('link')->count();
Please try this :
$uniqueResumes = Sample::select('link')->distinct()->count();

MINUS operation in Eloquent ORM

Is there any equivalent MINUS operation from SQL using Eloquent ORM?
For example
$model1 = Model::where('some constraints applied')
$model2 = Model::where('some constraints applied')
I want to get all models that exist in $model1 but not in $model2
seblaze's answer looks good, though it will run 3 queries. Another option is diff() method of the Collection object:
$result = $model1->diff($model2);
This works after fetching data from the db with 2 queries, but complete set of data (unless there are more depending on your 'constraints applied').
The easiest way i see it is :
//Get the id's of first model as array
$ids1 = $model1->lists('id');
//get the id's of second models as array
$ids2 = $model2->lists('id');
//get the models
$models = Model::whereIn('id',$ids1)->whereNotIn('id',$ids2)->get();
This is not tested code, please read more about eloquent queries here

Group By Eloquent ORM

I was searching for making a GROUP BY name Eloquent ORM docs but I haven't find anything, neither on google.
Does anyone know if it's possible ? or should I use query builder ?
Eloquent uses the query builder internally, so you can do:
$users = User::orderBy('name', 'desc')
->groupBy('count')
->having('count', '>', 100)
->get();
Laravel 5
WARNING: As #Usama stated in comments section, this groups by AFTER fetching data from the database. The grouping is not done by the database server.
I wouldn't recommend this solution for large data set.
This is working for me (i use laravel 5.6).
$collection = MyModel::all()->groupBy('column');
If you want to convert the collection to plain php array, you can use toArray()
$array = MyModel::all()->groupBy('column')->toArray();
try: ->unique('column')
example:
$users = User::get()->unique('column');

Resources