Eloquent Accessor Needs Parameters: What To Do?

Eloquent Accessor Needs Parameters: What To Do?

Laravel Daily

1 год назад

6,077 Просмотров

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


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

@reviewworld8622
@reviewworld8622 - 08.05.2023 18:40

Can we use dynamic scope for this scenario?

Ответить
@maciejmaciej4113
@maciejmaciej4113 - 02.05.2023 14:36

Apartments::with(['prices' => function($query) use ($startDate) {
$query->where('start_date'...
}])->get();
And then you can calculate sum in view.

Or you can make special method like this:

public function pricesBetween(): HasMany
{
$startDate = self::$startDate;
return $this->HasMany(ApartmentPrices::class)
->where('start_date'...
}
Apartments::$startDate = 'some date';
Apartments::with('pricesBetween')->get();

Ответить
@free2idol1
@free2idol1 - 08.04.2023 08:47

how about using spatie DTO instead of that native Resource? is that bad practice to use request() in DTO?

Ответить
@lahcenfahmi5361
@lahcenfahmi5361 - 07.04.2023 23:18

Great course, What about a default price for appertmente without sending dates what should do?

Ответить
@Raftor74
@Raftor74 - 07.04.2023 21:31

You can use type-hint in ApiResources like: /*@property Model $resource */

and then get access to the Model via $this->resource->your_attribute_or_metod.

It's more clear in my opinion.

Ответить
@ernestofavio6735
@ernestofavio6735 - 07.04.2023 19:04

Please make videos about microservices

Ответить
@wrends
@wrends - 07.04.2023 13:19

why have this kind of logic on a Apartment Model, and not in some higher level layer like Booking (manager or service) class?

Ответить
@antonmykhailovskyi447
@antonmykhailovskyi447 - 07.04.2023 13:03

Since model is not appropriate place for placing business-logic, I`d rather move that method to service like ApartmentService

Ответить
@LaravelJutsu
@LaravelJutsu - 07.04.2023 12:55

What I like to do is using the state pattern to remove logic from Models.

Then it would give something like :


$this->state->priceByDates(..., ...);


Advantages: keep models thin, reusability, external logic bound to a Model can be found in one class.

There's no perfect solution of course!

Thank you Mister, you are a great inspiration to me, furthermore you're someone with human values ❤

Ответить
@sayed1821
@sayed1821 - 07.04.2023 12:31

Cant we use constructors ?

Ответить
@MichaelPritchardRR3
@MichaelPritchardRR3 - 07.04.2023 12:02

Possibly a withSum(['AppartmentPrice' => function... the closure would have the query between dates for the appartment_id)

Ответить
@OkanUltimatum
@OkanUltimatum - 07.04.2023 10:19

I would strongly recommend using an action or service class, such as \App\Actions\PriceCalculator::handle(Apartment $apartment, Carbon $from, Carbon $to): float. It's not ideal for the apartment model to be aware of the price calculation process, which is more of a business logic concern. By separating this logic into a dedicated class, you can improve the overall structure and maintainability of your codebase.

Ответить
@olivierperrier114
@olivierperrier114 - 07.04.2023 09:50

I don't if it's valid in your case but sometimes it's a good way to use local scope. In your api you call the relation prices() then you link a scope that calculate prices for specific dates and execute the query.

Ответить
@user-wd3yd4li4c
@user-wd3yd4li4c - 07.04.2023 09:44

Hey Povilas! Love your content.
I want to ask you shortly if you can make an article let's say what's requirements for setting a macos for Laravel devs, I mean instead of xampp what to use for having an apache and etc. I think that would be really helpful , don't you think so? There is not a lot of stuff out there.
Thanks and have a wonderful day!

Ответить
@devsbuddy
@devsbuddy - 07.04.2023 08:39

I think the better term will be "getter" instead of method. As we are not relying on the accessor we are creating our own getter method.

Ответить
@mohamedalkomy7886
@mohamedalkomy7886 - 07.04.2023 08:33

I would make separate action class CalculateApartmentPriceByDates for example, because by adding more and more methods to this model I will end up with a big mess.

Ответить
@laravelstart
@laravelstart - 07.04.2023 08:31

What you will do if between start<>end intersect different price periods, what actual price will be? )

Ответить
@oluchukwuughagwu8293
@oluchukwuughagwu8293 - 07.04.2023 08:10

Thanks for this Sir. A question please. Instead of $this->calculatePriceForDates() in the controller, we can also do Apartment:: calculatePriceForDates()
Is this correct?

Ответить