PHP

php

40 PHP Tips

1. Do not use relative paths, instead define a ROOT path 2. Don’t use require , include , require_once or include_once 3. Maintain debugging environment in your application 4. Propagate status messages via session 5. Make your functions flexible 6. Omit the closing php tag if it is the last thing in a script 7. […]

40 PHP Tips Read More »

Mastering Validation in Laravel: Essential Methods for Form Validation

Post: Laravel, one of the most popular PHP frameworks, offers a robust and efficient way to validate incoming data. Proper validation ensures that your application handles user inputs securely and accurately. In this post, we will explore the essential methods for performing validation in Laravel, allowing you to build reliable and error-free web applications. Let’s

Mastering Validation in Laravel: Essential Methods for Form Validation Read More »

Understanding the query() Method in Laravel’s Eloquent ORM for Dynamic Query Building

  In Laravel, when using the Eloquent ORM, you can directly use methods like `where()` or `orderBy()` on the model itself without explicitly calling `query()`. For example: // Retrieve users with a specific condition$users = User::where(‘age’, ‘>’, 25)->get();// Retrieve users with multiple conditions$users = User::where(‘age’, ‘>’, 25)->orderBy(‘name’)->get(); In the above examples, we directly use the

Understanding the query() Method in Laravel’s Eloquent ORM for Dynamic Query Building Read More »

Getting the ID of a Newly Inserted Record in Laravel using save() Method

In Laravel, you can get the ID of a newly inserted record after calling the `save()` method on an Eloquent model by accessing the `id` property of the model. Here is an example:  // Create a new User model instance$user = new AppModelsUser;// Set some attributes on the model$user->name = ‘John Doe’;$user->email = ‘[email protected]’;// Save

Getting the ID of a Newly Inserted Record in Laravel using save() Method Read More »

Laravel Unique Validation on Multiple Columns

Let’s discuss this example:    ‘username’ => [ ‘required’, Rule::unique(‘users’) ->where(‘company_id’, $this->company_id) ] The code you provided is defining validation rules for the ‘username’ field in a Laravel application. The first rule ‘required’ means that the field is mandatory and must have a value. The second rule ‘Rule::unique’ is a dynamic rule that checks for

Laravel Unique Validation on Multiple Columns Read More »

Laravel Sanctum in Shorthand

Laravel Sanctum is a package that provides a lightweight authentication system for single-page applications (SPA), mobile applications, and simple, token-based APIs. As of the latest version of Laravel (8.x) which includes Sanctum (2.x), it provides the following methods: Auth::user() – Retrieves the currently authenticated user. Auth::id() – Retrieves the ID of the currently authenticated user.

Laravel Sanctum in Shorthand Read More »