You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why data migration? simple answer is it helps you modify database with a more systematic ways (avoid you screw things up due to human error), and good for team collaboration.
(42.12) Adding username to the regirstration flow (inside Auth folder)
- First, add the markup in register.blade.php
- Second, access the app/Http/Controllers/Auth/RegisterController.php and modify validator() and create()
Add new attribute "username"
- Third, access the database/migrations/create_users_table.php and modify the up()
Add new attribute "username"
- Fourth, access Tinker in powershell using `php artisan tinker` to interact with the database of application.
To check user table, use command `User::all();`
- Last, you will find out that username attribute is still not added
Because whenever you made changes to database, we need to recreate the database
Use command `php artisan migrate:fresh` to recreate database
Be aware that previous data will be deleted!!
- Also, need to access app/Models/User.php and add username into $fillable
Laravel provides another level of protection at Model to confirm what data can be filled
(58:35) Creating profiles controller
use make:controller.
Also, can use php artisan help command to check what is the required parameters or arg for this command. For example, php artisan help make:controller.
This part needs to deal with ProfilesController.php and routes/web.php.
The command User::find($user) is used to find the data from Models\User.php
dd() is useful function to echo out what's inside the variable
Explanation of the codes is inside ProfilesController
(1:10:20) Adding the Profiles Mode, Migration and Table
Eloquent is what laravel calls the database layer of the framework. Implementation that fecth quries, and database agnostic (can use diff database platform with the same database query)
command used is for example php artisan make:model Profile, make a model of Profile.
You can use php artisan make:model Profile -m too, the -m will automatically help you create a migration file
Manipulate database inside Tinker to test Profile model
$table->unsignedBigInteger('otherTable_id');
After adding the attribute, at the bottom add this
$table->index('otherTable_id');
This means that the 'otherTable_id' is foreign key
Example, Profile to User is one to one.
To connect relationship, we need to use some convention methods
Inside Profile model, there will be a function call user()
public function user()
{
return $this->belongsTo(User::class);
}
User will have the same thing, i.e., profile()
Following conventional naming will avoid the needs to extra config
Then you can try access data through relationship in Tinker
Solution to this is (2:04:24) Creating Through a Relationship
Also created a middleware to hide sensitive information
(2:08:12) Uploading/Saving the Image to the Project
How to upload the picture, save at the correct path
The store() method can save files in diff places, example Amazon s3
php artisan storage:link to link storage/app/public/uploads
After finished post, redirect back to profile page
To delete data (or clear database), use Post::truncate(); inside tinker. The "Post" should be your database table name
(2:19:19) Resizing Images with Intervention Image PHP Library
Adding new library using composer require library
Issue 1: Installation of Intervention\Image requires guzzlehttp\prs7:^1.7 version. Learn more about composer CLI and I post my solution in stackoverflow
Issue 2: GD Library extension not available with this PHP installation. And the solution is to enable it in php.ini file (for windows). After enabled, don't forget to restart the application.
(2:31:48) Editing the Profile
Repeat the same steps
Add edit.blade.php -> Configure route in web.php -> Add edit and update function for ProfilesController.php
(2:46:46) Restricting/Authorizing Actions with a Model Policy
Use php artisan help make:policy for more information
(3:00:00) Automatically Creating A Profile Using Model Events
Inside event, "saved" means already saved, and "saving" means before saving. The ed is pass, and the ing is before the action being done.
(3:19:48) Follow/Unfollow Profiles Using a Vue.js Component
npm run dev only run dev for once, in order to run for long time, use npm run watch
vue component is stored inside \resources\js\component\
Laravel is shipped with axios as well
vue property
Many to many relationship (need to create a pivot table, like a bridge in relational database).
First, we don't really need a new model, we need to create migration and use existing data field, use php artisan make:migration creates_profile_user_pivot_table --create profile_user (naming convention, follow alphabetic order, P first then U (Profile User), and everything is lower case, then add a _ in between)
After created migration and defined the data field, add m-2-m relationship into User.php and Profile.php
Laravel toggle feature (if you console.log(response.data); and click the follow button a few times, you will find out that the toggle will attach / detach depends on whether you followed or not followed that person)
telescope has a cache bar for you to view your cache hit or miss or set
All you need to do is at controller, do the Cache::remember() and return the $variable to view.
(4:11:44) Sending Emails to New Registered Users
Laravel is build in with mailtrap.io
Use php artisan make:mail NewUserWelcomeMail -m emails.welcome-email. The "NewUserWelcomeMail" is required name, the -m is a markdown template, and the "emails.welcome-email" is where you wanna store this mailable, and it will ultimately be in your view directory.
If you accidentally clicked the post button multiple times when creating the post, the server will get hit multiple times and in turn storing multiple samve value/data.
Looking for potential solution for front-end and back-end
sometimes when you change env, you might need to refresh your php artisan serve
An stdClass cannot convert to int (or other data type) error
Convert the array of class, into an actual array
$id = DB::select('select id from warehouses order by id desc limit 1');
// without this part, you won't be able to use to data inside $id
$currentMaxId = (array)$id[0];
// Now you can use math operation, treat it as PHP array
$newId += $currentMaxId['id'];
Understand the difference between hasMany and belongsToMany in eloquent relationship. Here's a simple explanation and a explanation with codes. Basically the difference is has is the one that has othe belongsTo's foreign key.