mardi 12 mars 2019

Laravel Eloquent Where casting string to another type

I've had some difficulty updating a model where a string passed into where is being re-cast or converted to some other type.

There's a varchar column in the database which holds "dates" in the YYYY-MM format. A query on this column is giving unpredictable results using get() or first() seeing as the query is where period = 2019-01 when it should be (and works manually with) where period = "2019-01".

I ended up using DB::raw to force a string, but i'm not satisfied with this and am still a bit perturbed, seeing as i've identified other instances of this which might only be working coincidentally :\

I tried casting to a string or parsing with Carbon directly before the where and also adding "period" to the $casts array in the model, without success.

Version is 5.4.

Edit - I'm using:

Model::where('period', $request->period)->first();

Where $request->period is being sent as a string. If i get the query that's being sent to the database, i'm getting 2019-01 instead of '2019-01'.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire