Composer
Try to run Composer
- In VSCode > Open any project > Terminal menu > New Terminal > type composer then click enter.
- You'll see something like this:
- In the terminal, type, composer global require laravel/installer. Hit enter.
- Try creating a new project. Type, laravel new test-project.
- In VSC, open .env file. (ctrl+p to search). By default, it will generate this file.
- For all of the projects that you make, you'll find .env.example file.
- .env is a private file (contains secret key etc.) and we do not want to share that info. We are not going to commit this file into repo.
- We'll commit .env.example instead. Others can see the example but will not know the real value that we have in .env .
- Let's say if we introduce a new variable, we'll make the changes in .env.example and commit that file. Others can just copy the changes from .env.example that they get (pull), and paste to their own .env file.
Rollback
- To be used if you no longer wish to add the changes you made. (revert/down)
- Type in php artisan migrate:rollback.
Routes
- In VSCode > ctrl+p > search routes/web > click web.php.
- By default, there are 4 different types of routes (see explorer bar).
- We have API (usually we open our API for other people to connect. They're going to consume, while we provide some data.) and SPA/WEB (backend/frontend. In web, they're combined. In spa, they work separately.).
HTTP Method
- HEAD
- GET = to fetch data. Read, not write.
- POST = write. To insert.
- PUT = write. To update. More common than patch.
- PATCH = write. To update.
- DELETE = write.
- OPTIONS = write. To update, but with attachments.
Route::get('/me', function () {return "Hello World!";});
PHP Callback Functions
- A function that is passed as an argument into another function.
- Any existing function can be used as a callback function.
- Used for easy implementation (instead of controller), for example, to redirect.
Models and Migration
- In the terminal, try typing in php artisan. We're not going to use all of the commands. It depends on what you're trying to make.
- To create a model, type php artisan make:model --help. Every command that we want to run, it'll have --help, -h, /? or ?.
- You can also do php artisan make:model ModelName. Hit enter.
- For controller, type php artisan make:controller ContName and hit enter.
Namespace
- As you can see in the ModelName.php, there is App\Models;
Now, we're going to try with the database (I use SQLyog).
- In SQLyog, create a new database. Database > create database.
- In .env.example, put your database name:
- In the database that you just created, open table 'User' (right click and refresh, if you can't find it) and add dummy values (just for testing). Don't forget to save them.
- Go back to your routes, web.php. (*Note that we don't need any functions). Add use App\Models\User after use Illuminate\Support\Facades\Routes.
- Also add: