Viewing Database Models from Image Upload Service series.

Database Models

Image Upload Service

In this video lesson we will generate our Eloquent Models in a new models folder. We will also go through and fill in all the properties required for us to connect to the database and write information into our tables.

Video Information

Hi everyone , welcome back to Laracademy. It's Mickey again and in this video we are going to work on our database models. In our current project we are going to only have a single model. Also in the previous lesson depending on if you created a model with the migration, or just a migration these first steps will be different. Let's load up the terminal and create the model.

php artisan make:model Models\Image

Laravel has created a default model file for us and we can find it in app\Models\Image.php. Now because our database table is the plural for image, which is images we do not need to tell Laravel what table to use. However, in the odd case that you do you can overwrite the table variable.

protected $table = 'images';

Next we want to tell Laravel what fields we are going to allow to be filled in by the application. We do this with a $fillable property.

protected $fillable = [
  'original_name',
  'storage_name',
  'view_count',
  'last_accessed_at',
];

Now we want to tell Laravel about our last accessed at value. We are storing is as a date/time so wouldn't it be nice if Laravel automatically converted that into a Carbon object for us. The Carbon object will allow us to perform date operations quickly and easily. So let's set that up.

protected $dates = [
  'last_accessed_at',
  'created_at', // here because we are overwriting the property
  'updated_at', // here because we are overwriting the property
];

That brings this lesson to an end. We have our database migrated and a model so that our application can talk to our database. Thank you for watching and see you in the next lesson.

Do you have a question? Go ahead and ask it below.

Please login to ask your question