Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Wolfie89's avatar

PHP & Laravel do's and don'ts beginner edition

Hi everyone :)

Just diving into PHP and Laravel and would love to get some insights on some do's and don'ts you wish you knew when you first started. Thanks in advance

0 likes
5 replies
LaryAI's avatar
Level 58

Hello and welcome to the world of PHP and Laravel! Here are some do's and don'ts that can help guide you as you start your journey:

Do's:

  1. Follow PSR Standards: Make sure to follow the PHP Standard Recommendations (PSR) to write clean and consistent code. This will make your code more readable and maintainable.

  2. Use Eloquent ORM: Take advantage of Laravel's Eloquent ORM for database interactions. It provides a more expressive and easier way to interact with your database compared to writing raw SQL queries.

  3. Leverage Artisan Commands: Get familiar with Laravel's Artisan command-line tool. It can help you with various tasks such as creating controllers, models, migrations, and more.

  4. Understand MVC: Make sure you understand the Model-View-Controller (MVC) architecture. Laravel is built on this pattern, and understanding it will help you organize your code properly.

  5. Utilize Migrations and Seeders: Use migrations for database schema management and seeders to populate your database with initial data.

  6. Read the Documentation: Laravel has excellent documentation. Make it a habit to read and refer to it often.

  7. Version Control: Use version control systems like Git from the start. It's essential for tracking changes and collaborating with others.

  8. Learn Blade Templating: Blade is Laravel's templating engine. It's powerful and allows for clean template inheritance.

Don'ts:

  1. Don't Ignore Security Practices: Always sanitize user input to prevent SQL injection and other security threats. Laravel provides several built-in functions to help with this.

  2. Don't Write Spaghetti Code: Avoid mixing HTML, PHP, and SQL in the same files. Keep your code organized and separated according to its purpose.

  3. Don't Repeat Yourself (DRY): If you find yourself writing the same code over and over, consider abstracting it into a function or service.

  4. Don't Neglect Validation: Always validate user input on both the client and server sides. Laravel provides a powerful validation class to make this easier.

  5. Don't Forget Testing: Write tests for your code. Laravel comes with PHPUnit set up out of the box.

  6. Don't Overuse Plugins and Packages: While packages can save time, relying too much on them can make your application bloated and harder to maintain.

  7. Don't Ignore Performance: Be mindful of your application's performance. Use caching, optimize queries, and be careful with loading large datasets.

  8. Don't Be Afraid to Ask for Help: The Laravel community is vast and generally very helpful. Use forums, Stack Overflow, and the Laracasts discussion boards when you're stuck.

Here's a simple example of a migration and a model in Laravel:

// Example of a migration
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('content');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

// Example of a model
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    // The attributes that are mass assignable.
    protected $fillable = ['title', 'content'];

    // Other Eloquent model methods and properties...
}

Remember, practice is key. The more you code, the better you'll get. Happy coding!

gych's avatar

Hey, Its best to start with the Laravel Path here on Laracasts. The path starts with PHP for beginners and contains a lot of good information.

https://laracasts.com/path

Good luck !

1 like
puklipo's avatar

First, learn the basics of PHP and front end.

Don't jump into Laravel.

Please or to participate in this conversation.