BTT Database structure and models

 Laravel provides us with Eloquent, an object-relational mapper (ORM). Thanks to it each database table has a corresponding "Model" that is used to interact with that table. Eloquent models allow us to retrieve, insert, update, and delete records from the table.

The database for final application looks like this:



users table consists of:
-
'id' of the user which is autoincremented and unique for each user
- username
- email which is also unique for each user
- is_admin integer which states if the user is an admin or not
- password
- rememberToken which is used by the framework to help against 'Remember Me' cookie hijacking. The value is refreshed upon login and logout.
- timestamps which show in database when the user account was created and last updated

users table migration file:



User Model looks like this:

 

$fillable property specifies which attributes should be mass-assignable. This lets developers inject an entire set of user-entered data from a form directly into an object or database.
$hidden property limit chosen attributes that are included in the model's array or JSON representation.
$casts property provides a convenient method of converting attributes to common data types.

We can see now that if we will try to display JSON fields marked as hidden (password and tokens) in User Model won't be shown:

I did that using the code below in one of the views to quickly explain how JSON data protection works.

<p>{!! json_encode($users) !!}</p>

Posts table consists of:
- 'id' of the post which is autoincremented and unique for each post
- title
- body which is just text content of the post
- thumbnail which is a cover image of the post
- timestamps which show in database when the post was created and last updated

 Posts table migration file:

 

Post Model looks like that:

 

$guarded = []; states that all fields are allowed for updating.
belongsTo() method will make Eloquent try to match the user_id from the Post model to an id on the User model. Whenever we will call an author() function, we will call the owner of chosen post.
hasMany() method states the relationship between post and comments and shows that a post can have many comments but a comment can only have one post. We will call certain post's comments with comments() method. 

comments table consists of:
- 'id' of the comment which is autoincremented and unique for each comment
- body which is just text content of the comment
- post_id which is a foreign key that indicates to which post that comment belongs
- user_id which is a foreign key that indicates to which user that comment belongs
- timestamps which show in database when the comment was created

 

Appeared problem and its solution!
I wanted comment to be deleted if its parent (post or user it belongs to) gets deleted as well. To do that we use cascadeOnDelete() method which deletes children with their parents according to relationship. I quickly found out, that the relatoinship won't be actually created without the constrained() parameter added to that line of code. However, for some reason it wasn't working for me either and even though in my database all tables types are stated as InnoDB ones, I had to specify that again manually with $table->engine = 'InnoDB'; which fixed the whole problem. 

Comment Model looks like this:

 

In post() function belongsTo() method will make Eloquent try to match the post_id from the Comment model to an id on the Post model. Whenever we will call the post() function, we will call the post that this comment belongs to.
In author() function belongsTo() method will make Eloquent try to match the user_id from the Comment model to an id on the User model. Whenever we will call the author() function, we will call user that created certain post.
use HasFactory; was added there in case I wanted to seed some fake comments to see how everything works.

Factories and Seeders
For my project I didn't need to use any factories or seeders but as it might be quite useful in future development I will briefly explain how it works on User factory example.

 

Factories use faker to generate fake data for us. In factory file we can define a blueprint for what fake user data should be generated, in this case a username will be generated as well as an unique email, password (same for each record) and remember_token consisting of 10 random characters. 

Then in the seeder file we just specify how many records we want it to seed after running the command, in this case its 10:

Komentarze