Difference Between Eloquent\Model::Get() and All()

Difference between Eloquent\Model::get() and all()

User::all() and User::get() will do the exact same thing.

all() is a static method on the Eloquent\Model. All it does is create a new query object and call get() on it. With all(), you cannot modify the query performed at all (except you can choose the columns to select by passing them as parameters).

get() is a method on the Eloquent\Builder object. If you need to modify the query, such as adding a where clause, then you have to use get(). For example, User::where('name', 'David')->get();.

The difference between 'find' and 'get' in Eloquent

get()

get() simply executes whatever (select) query you have built. It will return a collection (Illuminate\Database\Eloquent\Collection) in any case. That's the reason for your error message. You want the $roles of one model but you are trying to get it from a collection, which is obviously not possible.

find()

find() is used to fetch one or many models by its / their primary key(s). The return value will either be a single model, a collection or null if the record is not found.

Uses

$user = User::find(1); // returns model or null
$users = User::find(array(1, 2, 3)); // returns collection

Equivalent with first()

first() returns the first record, so you get a single model even if the result may would contain multiple records

$user = User::where('id', 1)->first();

returns the same as

$user = User::find(1);

Meaning for your case you want to use first() instead of get()

$roles = User::where('name', 'Test')->first()->roles;

Difference between select() and get() in laravel eloquent

Yes there is a difference. select() is only for defining which columns you want. get() is for actually getting the result (> executing the query) and it also allows you to specify columns.

DB::table('foo')->select(array('bar'));

Will not execute anything. You still need get() for that

DB::table('foo')->select(array('bar'))->get();

Now you receive a result with only the bar column.

The same can be done this way:

DB::table('foo')->get(array('bar'));

So syntax-wise get() alone is faster (meaning shorter) while performance wise you won't notice any difference.

Another little difference: with select() you can use the list syntax

select('foo', 'bar', 'etc', 'whatever')

and with get() you have to use an array

get(array('foo', 'bar', 'etc', 'whatever'))

Difference between get() and all() in laravel

Taken from the laravel source:

public static function all()
{
$input = array_merge(static::get(), static::query(), static::file());
// ....
return $input;
}

So all() calls get() and returns it's contents along with query(), and file() the $_FILES superglobal.

Preference will obviously depend on circumstance. I personally choose to use Input::get($key, $default) as I usually know what I am after.

The difference between an Eloquent Model and a Model?

I found the solution for my question ...

So Normally you must add an alias to your config/app.php

 'aliases' => [
'Eloquent' => Illuminate\Database\Eloquent\Model::class,

And when you create a model, Laravel will use Eloquent; instead of use Illuminate\Database\Eloquent\Model; this why i think, some person may use Model, when other may use Eloquent , it's just a matter of give a meaning sense to the namespace :3

Difference between laravel value() function and direct model access

Profession::where('name', $profession)->first() returns a model. You cannot call value on a model. However, you may call it on a query builder as explained in the docs?

So instead of calling

$profession_id = Profession::where('name', $profession)->first()->id;

you could achive the same thing with

$profession_id = Profession::where('name', $profession)->value('id');

What is difference between $this-Products and $this-Products() in laravel model?

$this->products; 
// Returns a Collection

$this->products();
// Returns a Relation instance, which is a query builder and can be of type HasMany, BelongsTo...

$this->products()->get();
// Is EXACTLY like doing $this->products for the first time.

The main difference is that products() is just a query that hasn't been executed yet, whereas products are the actual results of this query.

Honestly, even if the name is the same and can be confusing, there are no other similarities between them.

A simple analogy:

DB::table('products')->where('user_id', 18); //could be the $user->products()

DB::table('products')->where('user_id', 18)->get(); //could be $user->products

It's just an analogy, it's not exactly like this internally, but you get the point.

To add more confusion on top of it, Collection methods are ofter similar to those you find in queries; both have where(), first()...

The main thing to remember is that with parentheses, you are still building a query. Until you call get or first, you remain in a query builder.

Without, you already have your results, you are in a Collection (https://laravel.com/docs/8.x/collections).


About the difference you get between getReward1 and getReward2, it's hard to tell exactly what's happening without seeing your database structure.

It can be a lot of things, but when you are calling the sum method, you are calling it on a Collection instance in getReward1 and on a query builder in getReward2 (you are actually executing a query with SELECT SUM(reward)...).

laravel performance difference just get vs select and get

The differences between Model::get() and Model::select(['f1', 'f2'])->get() is only at the query

// Model::get()
SELECT * FROM table

// Model::select(['f1', 'f2'])->get()
SELECT f1, f2 FROM table

Both runs database query ONCE, and prepare the collection of model instances to you. select simply construct eloquent to only select fields you need. Performance gain is almost negligible or can be worse. Read about it here: Is it bad for performance to select all columns?



Related Topics



Leave a reply



Submit