New in Laravel 8.77: One Method for Accessors and Mutators in Eloquent

New in Laravel 8.77: One Method for Accessors and Mutators in Eloquent

Laravel Daily

2 года назад

21,949 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

FrTrash
FrTrash - 21.03.2023 02:58

Nice video, but, what if i want to cast this attribute, or better said, i have it casted as date? im having this error and i dont know hot to solve it.

Ответить
Ignacio Zamora
Ignacio Zamora - 29.01.2023 21:06

I've learned a lot from you in such a short time. Thanks a lot.

I haven't fully switched to this new approach, at least for the getter/accessor part, because I haven't found a way to not loose access to the original attribute value. I mean, the one that hasn't been modified by the accessor mechanism. At least with the getXXXAttribute() approach you can add a new attribute with a different name and you don't loose access to the original attribute value.

Ответить
Dirk Robbertse
Dirk Robbertse - 26.01.2023 13:25

I followed you on Laracast aswell, Love your content btw... Just a question if you have a table "unit_price" in the migrations and a "tax" table. if i calc the tax amount how do a save it in the db ? because i get the tax amount witch is correct but it does not save in the db, sorry im still learning programming

Ответить
Jon C
Jon C - 18.03.2022 22:12

Will your quickadminpanel be updated to reflect this method on generated code?

Ответить
elyambay
elyambay - 16.02.2022 23:27

I like this approach, it's more elegant in deed. I have a question though, how can we add a custom field to the model?
Like when we have first_name and last_name fields in the database and we want to have the full_name attribute by joining the first two fields.

Ответить
Pixii Bomb
Pixii Bomb - 16.01.2022 18:42

I like that Laravel is going more towards the C# way of doing things, but it seems this can ONLY be done with Models? Is that correct?
This is a Laravel specific implementation and not a PHP 8 implementation?

Ответить
Amine Daimallah
Amine Daimallah - 27.12.2021 23:14

yeah i like this way, looks much clean, nice video

Ответить
Mike Barrowman
Mike Barrowman - 27.12.2021 21:19

No, will not be using this. Appreciate the option, but IMHO it is less readable, and I prefer to keep them separate. Most importantly, we've got several hundred of these across our apps, certainly not going to incur cost just to change to sugar. Unfortunate the docs will change - that means we have to train new devs into something that was standard until now. Not a big deal for one thing... but that creeps up on you when supporting legacy code bases.

Ответить
Thomas Potter
Thomas Potter - 26.12.2021 14:59

Like vue computed get set, shorter and nicer. Loved it thanks for the update!

Ответить
Br A
Br A - 26.12.2021 06:58

Cool use of PHP8 named arguments.

Ответить
Ryan B
Ryan B - 24.12.2021 22:54

I hope the old method doesn't go away. I like this new method for simple cases of setting custom casts. But there are sometimes where there is more logic behind the accessor and/or mutator and I think it's cleaner to separate them into their own methods, particularly when its not just a single line return.

I also wonder will this new method work if I only supply just a getter or just a setter, or if it requires both to be defined. For example I define a getter but no setter to create a psudo field that doesn't actually exist in the DB. I do that a lot to create a separate property for a display version of a value versus the calculable version (for example with currency having an integer in cents for storing/calculating on say an 'amount' property, but having a getter to make a formatted string for display using a made up property like 'amount_display').

Ответить
OhKannaDuh
OhKannaDuh - 24.12.2021 20:43

Don't you lose any form of intellisense on this now? Or can you treat attribute like a genric in the docblock? (@returns Attribute<Carbon>)
I suppose you can still document the class to have the correct type for the property.

Ответить
vak vak
vak vak - 24.12.2021 17:32

I have only one question. Why Laravel hates my code?

I had a model with field profit. And don't like Laravel's magic when I can call $model->profit to get the value. So I create a real method to call it $model->profit() and it returns a Decial object. But with this update, my method does not work anymore. Because it should return an Attribute.

So the changes are great but I think you should not do something like this in minor updates. Laravel 9 - sure, no problem. But updating Laravel 8 I expect my code to still work.

Ответить
François M
François M - 24.12.2021 10:25

I like this new method. :)

Why didn't you use the Laravel method to parse dates directly, thought?

Ответить
Autocall
Autocall - 23.12.2021 20:57

Sir! Сould you please make another great tutorial video about elegant way to filter and sort data in "api routes" with "index" method with optional query params? 

For example: /products?orderBy=created_at&order=desc&created_at>=somedate&created_at<=somedate&category=[somearray with category names or ids]&name="Samsu%"&limit=20 etc


thank you!

Ответить
Autocall
Autocall - 23.12.2021 19:52

Cool! Thank you!

Ответить
Mehdi Yaghoubi
Mehdi Yaghoubi - 23.12.2021 18:36

prefer old syle.

Ответить
Raziul Islam
Raziul Islam - 23.12.2021 18:35

Can you tell me Which package was used to build the DataTable?

Ответить
Ali Al Qahtani
Ali Al Qahtani - 23.12.2021 18:32

Nice

Ответить
Ricko
Ricko - 23.12.2021 16:41

Great news, I didn't like the old version

Ответить