Ignore field to select in Eloquent model - laravel

I'm using a database with images as blob data and I want to ignore the "image" field of the table/model by default but be able to use the field later. ¿Is this possible?.

You can use this in your model, for example User model:
protected $hidden = array('image');
If you use this:
$model = User::find(1);
dd($model->toArray());
You'll not see the image field but if you use this:
dd($model->image);
Then you'll see that.

Related

Column not updating after saving model in Laravel?

After i set the column in migration like this :
$table->enum('paidBy',['BANK TRANSFERT', 'CARD'])->default('BANK TRANSFERT');
And added to my model :
protected $fillable = ['paidBy'];
protected $visible = ['paidBy'];
I wanted to update the column for a model but it's not working :
$ad->paidBy = 'CARD';
$ad->save();
How can i update it ?
The problem is that i had a mutator for the column paidBy that i forgot to delete. Now it's working
A very common reason why it is not updated is that you forget to set the fillable in the model.
Put in the model you want to update: protected $fillable = ['paidBy'];
in the model.
update
Then try to update withd update function: $ad->update(['paidBy' => 'CARD']);

Laravel 5.5 - Model fields

In the Laravel 5.5 documentation it is explained that in order to define the database fields it is necessary to write something like this in the model:
protected $fillable = [
     'name', 'email', 'password', 'birth_date'
];
How can I specify the type of field (boolean, datetime, etc)?
in the $fillable array you need to define the accessible fields from ur database with ur model, read this in the laravel doc fillable.
to specify the type of field u can use the $casts array inside ur model to cast the data from ur database inside the model with a specific type for that read this in the laravel doc casts

Can you create a new Model instance without saving it to the database

I want to create a whole bunch of instances of a model object in Laravel, then pick the optimal instance and save it to the database. I know that I can create an instance with Model::create([]), but that saves to the database. If possible I'd like to create a bunch of models, then only "create" the one that is best.
Is this possible?
I am using Laravel 5.0
You create a new model simply by instantiating it:
$model = new Model;
You can then save it to the database at a later stage:
$model->save();
You can create instances with Model::make(). It works the same way as create but it doesn't save it.
Whether or not this is best practice is another matter entirely.
Yes, it is possible different ways: you can use the mass assignment without saving.
Please remember to set first the $fillable property in your model.
WAY #1: using the method fill
$model = new YourModel;
$model->fill([
'field' => 'value',
'another_field' => 'another_value'
]);
$model->save();
WAY #2: using the constructor
$model = new YourModel([
'field' => 'value',
'another_field' => 'another_value'
]);
$model->save();
In YourModel set the $fillable property with the fileds allowed for mass assignment:
class YourModel extends Model
{
protected $fillable = ['field', 'another_field'];
// ...
}
Laravel documentation: https://laravel.com/docs/9.x/eloquent#mass-assignment
there is also a method you can call it statically to get new instance:
$modelInstance = $modelName::newModelInstance();
it takes array $attributes = [] as a parameter

Laravel mass update

yesterday i asked a question about Eager Loading and Form Model Binding.
Laravel Eager Loading and Form Model Binding
This is somehow a followup question.
Now i want to update the record in the Database
$user = \User::findOrFail($id);
$user->fill(\Input::all());
$user->push();
But this dose only save the data of the user itself. Not the relations
user = saved
user->profile = not saved
on my user model i have a fillable array with all fillables columns. and on other models like the profile model i just wrote protected $fillable = ['*'];
The Soultion:
call fill() on the relations not just the user model.
$user->fill($input)
$user->profile->fill($input['profile'])
You're not retrieving profile with your user model.
$user = \User::with('profile')->findOrFail($id);
That should give you the profile properties to update.

Return Eloquent model with different name from database tables

I want to return a JSON of an Eloquent model, but I'd like to change the array keys. By default they are set as the table field names, but I want to change them.
For example if I have a users table with two fields : id and user_name
When I return User::all(); I'll have a JSON with "[{"id" => 1, "user_name" => "bob}] etc.
I'd like to be able to change user_name to username. I haven't found the way to do it without an ugly foreach loop on the model.
I'm not sure why you would want to do this in the first place and would warn you first about the structure if your app/would it be better to make things uniform throughout.. but if you really want to do it.. you could do:
$user = User::find($id);
return Response::json(array('id' => $user->id, 'username' => $user->user_name));
That will return a JSON object with what you want.
You can also change the name of the key with:
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
Just have a look at robclancy's presenter package, this ServiceProvider handles those things you want to achieve.
GITHUB LINK
Just set the $hidden static for you model to the keys you want to hide:
class User extends Eloquent
{
public static $hidden = 'id';
}
and name them the way you like with get and set functons.

Resources