DEV Community

Ayako yk
Ayako yk

Posted on

Exploring Laravel: A Learning Journal

My primary focus has been on app development using React, with a growing interest in TypeScript. However, an interesting opportunity led me to join a team that utilizes Laravel. Although I had some exposure to PHP during my academic years, I had never created a full-fledged app with it. Equipped with a foundational understanding of PHP, I found myself tasked not only with familiarizing myself with Laravel but also with elevating my skills to a production-ready level.

The initial couple of months were devoted to creating basic CRUD apps and integrating into the team dynamics. During this phase, I managed to acquire a general grasp of how Laravel operates. Recognizing the need for a deeper and more systematic comprehension, I decided to approach my learning in a more structured way.

I've decided to document my progress in a series of learning journals right here on my blog. My intention behind sharing these insights is to offer a helping hand to those who, like me, are newcomers to the world of Laravel.

As this marks the very first article, I'll delve into an exploration of what Laravel is and the essential knowledge required to start.

Laravel is a PHP framework
To start a project, you need PHP and Composer installed on your local machine.

Composer
Composer is a tool for dependency management for PHP, similar to what npm is for JavaScript dependencies. Your Composer dependencies are stored in the 'vendor' directory.

Laravel uses Docker
Docker is a tool for running applications and services in 'containers'.

Laravel Sail
Laravel Sail, as described in the documentation, is a lightweight command-line interface designed to interact with Laravel's default Docker development environment. It utilizes PHP, MySQL, and Redis and is defined in the 'docker-compose.yml' file.

To start the Docker containers, use the following command:

./vendor/bin/sail up
Enter fullscreen mode Exit fullscreen mode

Or, after configuring an alias:

sail up
Enter fullscreen mode Exit fullscreen mode

For detached mode:

sail up -d
Enter fullscreen mode Exit fullscreen mode

To stop the Docker containers:

sail down
Enter fullscreen mode Exit fullscreen mode

or

sail stop
Enter fullscreen mode Exit fullscreen mode

Before using Sail
Ensure that no other web servers or databases are running on your local machine. In my case, I had to stop other servers by using the command:

sudo killall nginx
Enter fullscreen mode Exit fullscreen mode

After starting Sail
Adjust the command usage as follows:

# Running Artisan commands locally.
php artisan queue:work

# Running Artisan commands within Laravel Sail
sail artisan queue:work
Enter fullscreen mode Exit fullscreen mode

(Source: Laravel Documentation)

Database and Migration
By default, Laravel interacts with MySQL.

Artisan
Artisan is a command-line interface (CLI).
One of its most useful features is code generation. Running the commands below will create skeletal codes for you.

sail artisan make:controller ExampleController
sail artisan make:model Example
sail artisan make:migration create_example_table 
Enter fullscreen mode Exit fullscreen mode

To run the migration:

sail artisan migrate
Enter fullscreen mode Exit fullscreen mode

Namespace
In Laravel, namespaces are used for better organization by grouping classes and avoiding naming conflicts.
They are declared at the top, below <?php

Top comments (0)