[01/10] Laravel Travel API: DB Schema - Models and Migrations

preview_player
Показать описание
00:00 Intro
01:47 Role model/migration
03:09 User-Role many-to-many
05:46 Travels: irregular noun
09:00 Slug: observer/package
13:59 number_of_nights Accessor
16:03 Tour model/migration
17:52 Price accessor/mutator
19:07 IDs to UUIDs
22:01 Visual DB Schema

We start/continue our course about Laravel Travel API. Today we're talking about models and migrations!

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

Thank you so much for make youtube version of this premium course. I was a member of laravel daily premium but not now.

monjuaflame
Автор

Personally, I would name the "price" column as "price_in_cents" for clarity.

This way any developers who take over the code in the future will know that any computation involved has to take into account that the price is in fact stored in cents.

We will never know when a scenario where computation is to be ran via raw SQL and having a clear naming will avoid any potential computational errors.

errorlade
Автор

Thanks for publishing this kind of course. There are lots of things that new to learn... Keep doing good work..☺

mayanksgajjar
Автор

Thank you! Thank you! Thank you! Thank you! Thank you!

handymanus
Автор

Amazing thank you for the brilliant content. Teacher

syedhaaris
Автор

Things I have done differently:
- Specify pivot name in User <-> Role relationship ( ) and vice-versa
- Specify a composite primary key for the role_user table ( *$table->primary(['role_id', 'user_id']);* )
- Declare inverse relationships (Role <-> User, Tour <- Travel)
- In migrations that declare foreign keys (role_id, user_id, travel_id), explicitly drop them in the *down()* method to avoid problems should migrations be reversed ( *Schema::table('role_user', function (Blueprint $table) { });* )
- Declare *number_of_days* as an *unsignedSmallInteger* because it makes more sense to me. An UNSIGNED TINYINT already has a range of 0 to 255, which should be largely sufficient, but just in case I went with UNSIGNED SMALLINT which has a range of 0 to 65535 which is very much overkill. A Travel would need to last for more than 179 and a half years to break this.
- Declare *number_of_nights* as a virtual column in the migration, so it can be queries through SQL. (*$table->unsignedSmallInteger('number_of_nights')->virtualAs('greatest(0, number_of_days - 1)');*) This declaration takes into account the possibility of a Travel that doesn't even last a full day (number_of_days = 0), making sure number_of_nights will never be a negative number. A caveat of this approach: number_of_nights won't be available immediately upon creation in Eloquent unless it's manually set through a post creation event (created, saved) or the Travel model is retrieved from the database.

intipontt
Автор

Great video and serie, thank you for taught this kind of things.

descerebradodemas
Автор

thank you a lot for the full version of a course!!!

syracuse
Автор

Excellent video, I'm really learning a lot with you. But a clarification, in the migration of the travels table, in the down() method, you must also change the name of the table.

orestebarranco
Автор

Thanks for the upload. But I'am facing a problem. When I add new decimals price it inserted perfectly e.g (9999.99) => But the problem is when i updated the price with the 99 in it's decimal place, it rounds to 10000 e.g. (9999.99) => (10000). It will be helpful if you would kindly provide a solution.

mszaman
Автор

Thank you for the lessons.
I have one question about "price" column. Why we not use casts attribute (CastsAttributes) for this field?

SeydametBilyalov
Автор

new things learned: travel doesn't have plural

giacomogaravaglia
Автор

I have a question, instead of using TravelObserver, do you think it is easier to use booted() function inside the model, here is an example.
protected static function booted()
{
static::creating(function ($model) {
$model->user_id = Auth::user()->id;
});
}

mahdimiad
Автор

Hi Povilas, thank you for another interesting series. Is there a particular reason you creating slug in the observer vs in the controller?

VadimBesedin
Автор

Hello, small question why do you use for ending_date, starting_date date type not timestamp ? Always have problem what better to choose.

karlson
Автор

I used to use nullable for the majority of the database fields, but don't know if it's a good practice!?

mustafabde
Автор

i have a problem with this function in Travel
model public function NumberOfNights()
{
$Nights = $this->NumberOfDays ;
return $Nights;
}
the output of number of nights in postman is Null and when i make it NumberOfdays - 1 the output of number of nights is -1

iconicae
Автор

I want a link to the project on GitHub, is it possible?

rayaAghawani
Автор

about the slug, if this is gonna be a website I wouldn't recommend the way you did it.. because you need the user to actually control the slug, so the attribute should take its value from the slug "which the user should be able to change" not from the name

Because some times the user needs to change the name of the tour and in your example the slug would change, and that case it is not SEO friendly because then the old URL which had the old slug would which will be a 404, or even if you added a way to auto make a route perm redirect, its still not very SEO friendly..

So from experience I found the best solution so far is to keep the user in control of the slug because mostly if they have a link that is doing well in search engines they usually keep the slug the same even if they change the name

islamyacoub
Автор

Without having seen the video before: Why are the days saved in the trip table? In the examples there are two tours with 18 and 6 days for the Japan trip. So it makes more sense to store the individual trip duration in the trip table.

matthiashoffmann