Is there a way to retrieve a model in Laravel w3coded laravel models with all the attributes, even if they're null? w3coded laravel models It seems to only return a model with the w3coded laravel models attributes that aren't null.,The reason for this w3coded laravel models is that I have a function that will update the w3coded laravel models model attributes from an array, if the w3coded laravel models attributes exists in the model. I use the w3coded laravel models property_exists() function to check the model if w3coded laravel models it has a particular attribute before setting it. w3coded laravel models The array key and model attribute are expected w3coded laravel models to match, so that's how it works. , w3coded laravel models w3coded laravel models Thanks! w3coded laravel models This led me in the right direction. w3coded laravel models Realistically, I didn't need every attribute on w3coded laravel models the model. Just a few select ones to update, w3coded laravel models which coincidentally are also the within the w3coded laravel models $fillable array when I thought about it. So I'm w3coded laravel models actually able to use $fillable to check if the w3coded laravel models model should have the attribute. w3coded laravel models – kenshin9 w3coded laravel models Nov 6 '15 at 3:17 w3coded laravel models ,What's ultimately happening is that I w3coded laravel models have a single array of attributes, and then w3coded laravel models perhaps two models. And I run my setter w3coded laravel models function, passing in the attributes array, and w3coded laravel models each of the objects in separate calls. If the w3coded laravel models model has a matching property, it gets w3coded laravel models updated.
Here are two ways to do this. One method is to define default attribute values in your model.
protected $attributes = ['column1' => null, 'column2' => 2];
If you don't want to set default attributes though, I wrote up a quick method that should work.
public function getAllAttributes()
{
$columns = $this->getFillable();
// Another option is to get all columns for the table like so:
// $columns = \Schema::getColumnListing($this->table);
// but it's safer to just get the fillable fields
$attributes = $this->getAttributes();
foreach ($columns as $column)
{
if (!array_key_exists($column, $attributes))
{
$attributes[$column] = null;
}
}
return $attributes;
}
$model->getAttributes();
Above will return an array of raw attributes (as stored in the database table)
$model->toArray()
If you are trying to do this after instantiating like so:
$model = new Model;
Like so:
if (array_key_exists('foo', $model->getAttributes())) {
$model->foo = 'new value';
}
What if you were to explicitly declare all the fields you want back.
public function getSomeModelFromArray(Request $request)
{
// This will only give back the columns/attributes that have data.
// NULL values will be omitted doing it this way.
//$model = $request->all();
// However by declaring all the attributes I want I can get back
// columns even if the value is null. Additional filtering can be
// added on if you still want/need to massage the data.
$model = $request->all([
'id',
'attr1',
'attr2',
'attr3',
//...
]);
//...
return $model;
}
I have this snippet on my other project to load all model attributes and relation.
public function forModel($with)
{
$this->load($with);
$attributes = $this->toArray();
// Normalize Null Relation To Empty Model
foreach ($attributes as $key => $value) {
if (
is_null($value) &&
method_exists($this, Str::camel($key)) &&
$this->{Str::camel($key)}() instanceOf \Illuminate\Database\Eloquent\Relations\Relation
) {
$relation = $this->{Str::camel($key)}();
$model = $relation->getModel();
$attributesForRelation = $model->getAttributes();
foreach ($model->getFillable() as $column)
{
if (! array_key_exists($column, $attributesForRelation))
{
$attributesForRelation[$column] = null;
}
}
$attributes[$key] = $attributesForRelation;
} else {
$attributes[$key] = $value;
}
}
return $attributes;
}
Last Update : 2023-09-22 UTC 13:49:18 PM
Last Update : 2023-09-22 UTC 13:49:10 PM
Last Update : 2023-09-22 UTC 13:49:02 PM
Last Update : 2023-09-22 UTC 13:48:50 PM
Last Update : 2023-09-22 UTC 13:48:43 PM
Last Update : 2023-09-22 UTC 13:48:27 PM
Last Update : 2023-09-22 UTC 13:48:10 PM