Tag

Laravel

Laravel

Fixing XML Issue in Blade Templates

During my latest project, I dynamically created an XML sitemap for search engines using Laravel. However, while rendering the Blade template, I encountered the following error message: syntax error, unexpected identifier "version" The root cause of this issue can be traced back to PHP potentially interpreting what's known as "Short Open Tags". PHP code typically begins with <?php. However, when Short Open Tags are enabled, <? is sufficient, which can lead to a conflict

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

New Features in Laravel 11: What Web Developers Need to Know

This article provides information about the upcoming changes in Laravel 11. Please note that this is still a beta preview and changes are possible at any time. The article will be updated as new information or changes are known. Simplified Directory Structure One of the first things you'll notice is the revised and simplified directory structure of Laravel. By default, controllers no longer extend anything and the middleware directory has disappeared. Laravel currently includes nine middleware,

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Increase PHP Memory Limit when using Laravel Herd

Since the release, I have been a user of Laravel Herd. Previously, I used Laravel Valet, but Herd (in combination with dbngin) simplifies the deployment of a Laravel development environment on macOS even further. However, when first running pestphp tests, I encountered a problem: php artisan test -p led to the following error message: ..................................................... In WorkerCrashedException.php line 41:

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel Database

Resolving Issues with PHPUnit, SQLite, and dropColumn

If you're executing a Laravel migration that involves both dropping and adding columns, this can lead to complications when using SQLite, particularly when coupled with PHPUnit. <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::table('my_table_name', function (Blueprint $table) { $table->dropColumn

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Custom Favicon for the Filament Admin

If you wish to enhance your administration area, built with Filament Admin, with a custom favicon, you can follow the steps below. Place the desired favicon (in this example we use "admin_favicon.ico") in the project path under /public. In the boot() method of the file /app/Providers/FilamentServiceProvider.php, add the following code: Filament::pushMeta([ new HtmlString('<link rel="icon" type="image/x-icon" href="/admin_favicon.ico">'), ]);

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Laravel Unit Test: A facade root has not been set

Unit tests are typically meant for performing quick and simple tests. More complex tests with larger dependencies are usually implemented as feature tests in Laravel. However, when using unit tests, it may happen that you need to access elements such as Storage or similar components. When you try to access them, you may encounter the following error message: A facade root has not been set. This error occurs because of the default inclusion of: use PHPUnit\Framework\TestCase; To be able to use

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Manage Laravel Comments using Filament Admin

There is a great YouTube video about how to manage Laravel Comments, a package by Spatie, using Filament Admin. I could not find the source code anywhere else, that's why I wrote the code following the video. If anybody else may need the code, here it is: Create a Comment filament resource: php artisan filament:resource Comment --view --simple  Create a User filament resource php artisan filament:resource User --generate Create a Comments widget: php artisan filament:widget Comments Content o

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Fix "No Xcode or CLT version detected" When Running npm install

Introduction In this tutorial, we will address a common issue that occurs when running npm install after setting up a new Laravel project. If you encounter the following error message, we will guide you through the steps to resolve it and successfully run npm install. Error Message When running npm install, the following error message is displayed: sebastian@Sebastians-iMac my-project % npm install > fsevents@1.2.11 install /Users/sebastian/Development/valet/my-project/node_modules/fsevents

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Create Laravel User using Laravel Tinker

To create a new user in a Laravel application using the Tinker tool, you should follow the steps outlined below. Tinker is a powerful tool embedded in the Laravel environment that allows you to interact with your application from the command line. Start by launching Tinker. Open your terminal or command prompt and navigate to the directory where your Laravel application is located. There, enter the following command: php artisan tinker Upon entering this command, Tinker will start, and you'll s

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Ignore Case When Using sortBy in Laravel

Laravel's sortBy() function allows you to sort data, taking case sensitivity into account by default. If you wish to intentionally disable this distinction, you can do so using the parameters SORT_NATURAL|SORT_FLAG_CASE. Here's a practical example: $customers = Customer::all()->sortBy( 'name', SORT_NATURAL|SORT_FLAG_CASE ); It's important to note that the sortBy() function accepts the same parameters as PHP's native sort() function. A comprehensive overview of all available parameters can be

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Fix "No application encryption key has been specified." When Using Laravel

If you have a Laravel app cloned from a Git repository, you mostly have to create your custom .env file. This .env file contains an encryption key, that is not set, when creating a duplicate of .env.example. If you open your application site, you will get the message No application encryption key has been specified. To fix this problem, all you have to do is to run this artisan command: php artisan key:generate This command will generate an encryption key for you and adds it to your .env file.

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Allow Empty Values when Validating URL or Email in Laravel

Laravel provides some handy validators for validating URLs and emails. But what, if you have a field, that accepts an optional email or optional URL? This can be realized by nullable, an example: $validatedData = $request->validate( [ 'name' => 'required|max:100', 'email' => 'nullable|email', 'website' => 'nullable|url' ] ); There is a difference between nullable and sometimes. If you use nullable, the value can be null or empty. If you use sometimes, th

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Validate in Laravel, That at Least One Checkbox is Checked

If you have a form with multiple checkboxes (categories, tags, ...) and you want to be sure, that at lease on checkbox is checked, you can add this validation: public function store(Request $request) { $validatedData = $request->validate([ 'category' => 'required', ]); // more code } The ‘required’ validation checks, that there is at least one checked checkbox – all you need.

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Validate in Laravel, That Checkbox IDs are Valid

If you have a form with multiple checkboxes (e.g. categories, tags, …) and you want to validate, that the submitted IDs are valid entries in your database, you can add this validation: public function store(Request $request) { $validatedData = $request->validate([ 'category' => 'required', // validates, that at least one checkbox is selected 'category.*' => 'exists:categories,id', // validates, that a matching database entry is present ]); // more code } The ‘category.*’ valida

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer

Laravel

Fix "419 Sorry, your session has expired" After Renaming a Laravel Project

After renaming a Laravel Project, I always received “419 Sorry, your session has expired.” errors when submitting POST requests. The solution was pretty simple, all I had to do is to regenerate the application keys: php artisan key:generate It seems, there is a session encryption problem when renaming a Laravel app. Hint: Be careful when running the command above. The key is used for encryption, so some of your encrypted data (like passwords) may be useless.

Sebastian Widmann

Sebastian Widmann

Webdesigner / Web developer