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
Related
Eloquent allows Enum Casting.
Eloquent also allows you to cast your attribute values to PHP enums.
To accomplish this, you may specify the attribute and enum you wish to
cast in your model's $casts property array:
use App\Enums\ServerStatus;
/**
* The attributes that should be cast.
*
* #var array
*/
protected $casts = [
'status' => ServerStatus::class,
];
Once you have defined the cast on your model, the specified attribute
will be automatically cast to and from an enum when you interact with
the attribute:
if ($server->status == ServerStatus::provisioned) {
$server->status = ServerStatus::ready;
$server->save();
}
Is it possible to use enum casting in eloquent for array values?
I have an existing eloquent model that use to have one type. And now it needs to support multiple.
Does Laravel support array enum casting or ist this not possible? What would be an alternative approach to achieve something similar?
you can use spatie/laravel-enum
after installing it:
composer require spatie/laravel-enum
you can use it for array enum casting, something like:
protected $casts = [
'status' => StatusEnum::class.':collection',
];
and if the status is nullable you can:
protected $casts = [
'status' => StatusEnum::class.':collection,nullable',
];
this package also provider validation rule and other features.
and here is an interesting pull request that has been merged, but I did not test it.
I have added a datetime in the database schema:
$table->dateTime('send_at')->nullable();
I have set the attribute to a Carbon instance in the seeder:
$invoice->send_at = Carbon::now();
When I try to get the type of the attribute inside the controller it returns a string:
dd(gettype($data['invoices'][0]->send_at));
What is going on? And how can I be sure that it's a Carbon Object instead of a string?
On your model, you need to define $dates property to create Carbon instance automatically for the column :
protected $dates = ['send_at'];
On Since Laravel 8:
protected $casts = [
'send_at' => 'datetime',
];
I have a basic resource, and I'd like to know if I must escape form data or laravel does it for me behind the scenes?
public function store(ProductRequest $request, Product $product)
{
$fields = [
'owner_id' => 1,
'title' => $request -> title,
];
$product -> create($fields);
return view('product');
}
if I pass
';drop table products;
as the title, it is being stored as is, yet I still have the table, how?
in App\Http\Kernel.php I've the default middlewares
laravel -v: 2.0.1
Laravel uses parameter binding in order to protect you against SQL injection out of the box if you use Eloquent.
If you need to use the query builder than you should use this approach to bind parameters passed to the query for SQL injection protection.
I have a JSON format like below,
{"projectId":"1","projectName":"My Project","total_project_cost":2200000,"total":2400000,"totalPercentFee":7,"adderArray":[{"name":"Adder 1","value":"100000","firmApercentage":"","firmBpercentage":""},{"name":"Adder 2","value":"100000","firmApercentage":"","firmBpercentage":""}]
In this to insert adderArray i have user laravel $cast.
So my model is looking like this
protected $table = 'projects_percent_fee_management';
protected $casts = [
'adder_data' => 'array',
'project_breakdown' => 'array',
];
protected $fillable = ['project_id','company_id','total_project_cost','adder_data','total','total_percent_fee','project_breakdown','total_budget'];
Insert is working well and it insert the adderArray data properly in adder_data field.
But while update it gives preg_replace(): Parameter mismatch, pattern is a string while replacement is an array error and in field it updates "Array".
I think while updating, you are trying to pass the parameters whose corresponding columns are not present in the table (database / model).
To make this work, use $fillable variable inside Model. e.g.:
$fillable = ['column_1', 'column_2', 'column_3', 'column_4',..., 'column_n'];
By adding $fillable inside your model the update query will take only those parameters in updating which are defined in $fillable array.
Hope it helps you. For more detailed info about Mass Assignment in Laravel - see this
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.