Get Attribute from Elequent where clause - laravel

So I have the following where clause where I am searching the name "Gold" inside of my tier table.
$tier = Tier::where(['name' => 'Gold'])->get(['id']);
dd($tier);
When I do the above the "id" is listed inside of the attributes but when I do as below it gives me
Undefined property: Illuminate\Database\Eloquent\Collection::$id
dd($tier->id);
All I want is to get the Id of the record where the name is "Gold".

To get actual model you should use first() instead of get(). Because there are potentially multiple rows that match get() will return a collection.
$tier = Tier::where(['name' => 'Gold'])->first(['id']);
dd($tier->id);
If you really need only the id there's even a simpler function to that, pluck().
It will get one attribute from the first result:
$id = Tier::where(['name' => 'Gold'])->pluck('id');

Related

Eloquent Collection query - 'WHERE' clause with $value parameter without invoked accessor

I have used some accessors for Model attributes.
And as my knowledge about Accessor in Laravel, it can use to create a virtual attribute based on another attribute value or modify the return value of the existing attribute.
So I create an Accessor with the public method getStatusAttribute to convert the status from integer to string value. For example, my Model has an attribute named is 'status'. In DB, this column is stored with integers like 0,1,2,...
In this method, the return value is a string based on the current status attribute value.
public function getStatusAttribute(int $value): string
{
$statuses = [
1 => 'New',
2 => 'Pending',
3 => 'Canceled'
];
return $statuses[$value];
}
Everything work fined when I retrieve attribute value from the model object entity.
But when I want to retrieve an Eloquent Collection with a specific 'status' attribute value, the $value parameter in the 'where' method expects a string value that is returned from the accessor method, not is an integer value which stored in a database. So the $value parameter would be 'New' or 'Pending' or 'Canceled' instead of 1,2,3.
I want to retrieve a collection that contains an Eloquent Model Object so that I won't use an Illuminate\Support\Facades\DB.
I know I still can retrieve the exact collection with the $value parameter as a string, but I was just wondering if we have another way to skip the accessor in the query of Eloquent Collection?
Please see an example below for more detail about what I expect.
For example, some records in the database look like the below, and the model was named 'DemoModel' :
id
status
1
1
2
3
3
1
4
3
5
2
public function getCanceledEntities($entityId)
{
$canceledEntities = DemoModel::all()->where('status', '=', 3);
if($canceledEntities->isNotEmpty()) {
return $canceledEntities;
}
}
Without an accessor, this query will return a collection containing two entities with id [ 2, 4 ].
But with an accessor (as defined in the model), it can't find a matched entity because the WHERE clause expects that the 3rd parameter is 'Canceled' instead of 3.
My alternative method is to use a string 'Canceled' in the 'where' clause. However, I still want to use the value stored in a database for the query, and I think maybe the framework has another way to write an Eloquent Collection query without invoking an accessor.
Any suggestions are always welcome.

Laravel collections convert key value to key assoc array

I use this function: $assets->pluck('url').
It outputs: ['link','link1','link2']
How to convert it to: [{url: 'link'}, {url: 'link1'}, {url: 'link2'}]
You can add functions to the relation query to only return the url field. This way you will get the desired result.
$assets = auth()->user()->assets()->select('url')->get();
You can use the ->only() collection method to specify the fields you want to return:
$assets = auth()->user()
->assets
->only(['url'])
->all();
To return a collection of which contain only certain field on model you can add an array of list of fields to retrieve as parameters of the get method on your model
ModelName::get(['id', 'url']);
or by passing list of fields as paremter to the select method
ModelName::where([...])->select('id', 'url')->get();

How to get db field from class object

I execute this update statement which works fine.
$g = Group::where([['id', $idgroup], ['admin', $currentUser]]);
$g->update(['admin' => $iduser]);
After this statement I need the field name from my Group object. But with the following statement I get Undefined property: Illuminate\Database\Eloquent\Builder::$name.
$groupname = $g->name;
Group is my model (DB table) which contains the field id, name, admin. How can I get the field name from this object?
Thank you
Without finder it will return you a query builder but when you need to fetch model instance then apply get (for Collection of model) or first(for single model instance) on query builder. For your case you need to call first() method like this
$g = Group::where([['id', $idgroup], ['admin', $currentUser]])->first();
$g->update(['admin' => $iduser]);
Then you can do
$groupname = $g->name;
You can check the details here for retrieving model
If you want to get one record from database then use first():
$g = Group::where([['id', $idgroup], ['admin', $currentUser]])->first();
$g->name; //it will output name
OR
If you want to get all record from database then use get():
$groups = Group::where([['id', $idgroup], ['admin', $currentUser]])->get();
To get name from here you will have to run loop because get() will return a collection
foreach($groups as $g){
$g->name; //it will give name
}
Your $g variable is a Illuminate\Database\Eloquent\Builder which represent a query, not a single object, which means it doesn't have the Group properties. It's very likely you have more than one record under $g, you have two options retrieve all the names fields like this
$g->pluck('name')
This will give you a collection of all the names, or if you're certain there's one single object, you can do $g->first()->name, it will give you the name of the first object (only one if you're sure there is only one object)

How to select specific columns in laravel eloquent

lets say I have 7 columns in table, and I want to select only two of them, something like this
SELECT `name`,`surname` FROM `table` WHERE `id` = '1';
In laravel eloquent model it may looks like this
Table::where('id', 1)->get();
but I guess this expression will select ALL columns where id equals 1, and I want only two columns(name, surname). how to select only two columns?
You can do it like this:
Table::select('name','surname')->where('id', 1)->get();
Table::where('id', 1)->get(['name','surname']);
You can also use find() like this:
ModelName::find($id, ['name', 'surname']);
The $id variable can be an array in case you need to retrieve multiple instances of the model.
By using all() method we can select particular columns from table like as shown below.
ModelName::all('column1', 'column2', 'column3');
Note: Laravel 5.4
You first need to create a Model, that represent that Table and then use the below Eloquent way to fetch the data of only 2 fields.
Model::where('id', 1)
->pluck('name', 'surname')
->all();
Also Model::all(['id'])->toArray() it will only fetch id as array.
Get value of one column:
Table_Name::find($id)->column_name;
you can use this method with where clause:
Table_Name::where('id',$id)->first()->column_name;
or use this method for bypass PhpStorm "Method where not found in App\Models":
Table_Name::query()->where('id','=',$id)->first()->column_name;
in query builder:
DB::table('table_names')->find($id)->column_name;
with where cluase:
DB::table('table_names')->where('id',$id)->first()->column_name;
or
DB::table('table_names')->where('id',$id)->first('column_name');
last method result is array
You can use get() as well as all()
ModelName::where('a', 1)->get(['column1','column2']);
From laravel 5.3 only using get() method you can get specific columns of your table:
YouModelName::get(['id', 'name']);
Or from laravel 5.4 you can also use all() method for getting the fields of your choice:
YourModelName::all('id', 'name');
with both of above method get() or all() you can also use where() but syntax is different for both:
Model::all()
YourModelName::all('id', 'name')->where('id',1);
Model::get()
YourModelName::where('id',1)->get(['id', 'name']);
To get the result of specific column from table,we have to specify the column name.
Use following code : -
$result = DB::Table('table_name')->select('column1','column2')->where('id',1)->get();
for example -
$result = DB::Table('Student')->select('subject','class')->where('id',1)->get();
use App\Table;
// ...
Table::where('id',1)->get('name','surname');
if no where
Table::all('name','surname');
If you want to get a single value from Database
Model::where('id', 1)->value('name');
Also you can use pluck.
Model::where('id',1)->pluck('column1', 'column2');
You can use Table::select ('name', 'surname')->where ('id', 1)->get ().
Keep in mind that when selecting for only certain fields, you will have to make another query if you end up accessing those other fields later in the request (that may be obvious, just wanted to include that caveat). Including the id field is usually a good idea so laravel knows how to write back any updates you do to the model instance.
You can get it like
`PostModel::where('post_status', 'publish')->get(['title', 'content', 'slug', 'image_url']`)
link
you can also used findOrFail() method here it's good to used
if the exception is not caught, a 404 HTTP response is automatically sent back to the user. It is not necessary to write explicit checks to return 404 responses when using these method not give a 500 error..
ModelName::findOrFail($id, ['firstName', 'lastName']);
While most common approach is to use Model::select,
it can cause rendering out all attributes defined with accessor methods within model classes. So if you define attribute in your model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* Get the user's first name.
*
* #param string $value
* #return string
*/
public function getFirstNameAttribute($value)
{
return ucfirst($value);
}
}
And then use:
TableName::select('username')->where('id', 1)->get();
It will output collection with both first_name and username, rather than only username.
Better use pluck(), solo or optionally in combination with select - if you want specific columns.
TableName::select('username')->where('id', 1)->pluck('username');
or
TableName::where('id', 1)->pluck('username'); //that would return collection consisting of only username values
Also, optionally, use ->toArray() to convert collection object into array.
If you want to get single row and from the that row single column, one line code to get the value of the specific column is to use find() method alongside specifying of the column that you want to retrieve it.
Here is sample code:
ModelName::find($id_of_the_record, ['column_name'])->toArray()['column_name'];
If you need to get one column calling pluck directly on a model is the most performant way to retrieve a single column from all models in Laravel.
Calling get or all before pluck will read all models into memory before plucking the value.
Users::pluck('email');
->get() much like ->all() (and ->first() etc..) can take the fields you want to bring back as parameters;
->get/all(['column1','column2'])
Would bring back the collection but only with column1 and column2
You can use the below query:
Table('table')->select('name','surname')->where('id',1)->get();
If you wanted to get the value of a single column like 'name', you could also use the following:
Table::where('id', 1)->first(['name'])->name;
For getting multiple columns (returns collection) :
Model::select('name','surname')->where('id', 1)->get();
If you want to get columns as array use the below code:
Model::select('name','surname')->where('id', 1)->get()->toArray();
If you want to get a single column try this:
Model::where('id', 1)->first(['column_name'])->column_name;

Eloquent model columns as properties

I am working with laravel and eloquent, I'm sorry if this is a stupid question, I've already made some search on google but couldn't find something like this.
I have a Company model which has a "hasMany" relationship with a CompanyProperties model. The CompanyProperties has two columns: "name" and "value".
id | Name | Value
1 | primaryColor | #000
2 | secondaryColor | #333
When I get the companies with Company::all(); it loads perfectly the CompanyProperties of each company. Every CompanyProperty returns a collection, which I can access using $company->CompanyProperties[0...n].
What I'd like to do if it's possible is that the CompanyProperties are accessible using the "name" column value as property or as an array. Using the example of my table above:
$company->CompanyProperties['primaryColor']
or
$company->CompanyProperties['secondaryColor']
Is there a way to do this?
Oh yea, many thanks in advance. I appreciate any advice you could have on this (even if I have to change the "properties" approach, haha).
Laravel 5.1 and before: Use the lists method
Laravel 5.2: Use pluck
$properties = $company->companyProperties()->pluck('Value', 'Name');
This returns an array where the value is the the 1st argument ('Value') and the key is the 2nd argument ('Name'), e.g.
$value = $properties['primaryColor']; // '#000'
However, this can be pretty inefficient because you're running a query for each company that you fetch. If it's just one or a few companies that's ok but given that you're using Company:all() I would recommend eager loading:
$companies = Company::all();
$companies->load([
'companyProperties'
]);
That will fetch the properties for all companies in a single query. Now for every $company, $company->companyProperties is a Collection. So you can use the keyBy method:
foreach ($companies as $company) {
$properties = $company->companyProperties->keyBy('Name');
$value = $properties['primaryColor']->Value; // '#000'
}
Notice that the syntax is slightly different, $properties['primaryColor'] is still a companyProperties object, so you have to get the attribute ->Value

Resources