How to Send Access-Control-Allow-Origin in Laravel Without a Package

If you want to send CORS headers to the client depending on the request parameters or settings in the database, you can send them manually. It is not so difficult. First, you need to comment out the default HandleCors middleware in app/Http/Kernel.php, because it overrides CORS headers that are set in the response: If you … Read more

How to Fix Wrong Timezone in created_at in Laravel

You may notice that when you serialize the an Eloquent model using the toArray() method or a collection of Eloquent models, all timestamps, for example, created_at and updated_at will have a UTC timezone. However, when you serialize any date manually using Carbon the timezone is correct. Looks like it is a bug and the toArray() … Read more

How to Fix Cross Origin Request Blocked in Laravel

Such an error may appear in the browser JavaScript console when the browser blocks API requests because the API is not located on the same domain as your application. If you want to access an API from a different domain, your browser uses the policy for Cross-Origin Resource Sharing (CORS) to determine whether the request … Read more

How to Fix Target Class Does Not Exist in Laravel

This exception occurs when the Laravel service container can’t find a class you want to get through dependency injection or directly from the container. Moreover, this does not necessarily have to be the class name. It can also be a class alias. You can face the error in a few different cases. In this article, … Read more

How To Get IP Address in Dockerized Laravel

Getting a client IP address in Laravel is pretty simple. You don’t need to know which header contains the IP address, because there is the ip() method in the Illuminate\Http\Request class. However, if your Laravel application is running in a Docker container behaind a proxy, the method above will always return Docker internal IP address … Read more

How to Fix Permission Denied in Laravel

After deploying a new project or executing artisan commands on the server, you can encounter permission denied error in Laravel when it attempts to write logs or cache. Misconfigured write permissions or ownership on the storage directory cause this issue. In this short article, I will explain why it happens, how to fix it, and … Read more

How to Pass PHP Variable to JavaScript in Laravel

Sometimes, you may need to pass certain PHP variables to JavaScript code in a Blade template or even to a separate JavaScript file that is imported into the template. In this short article, I will show you how to pass PHP variable to JavaScript in Laravel and avoid errors in the JavaScript console. How to … Read more

How to Fix Job Timed Out in Laravel

If you use queues in Laravel, you most likely encountered the Job has timed out error when processing jobs in a queue. In older versions of Laravel you can see this error message directly in the terminal or the failed_jobs database table after the job has failed when job is running longer than 60 seconds. … Read more

How to Run Laravel Scheduler in Docker

In this short article, I will show how to run scheduled cron jobs in Laravel for an application that is deployed in a Docker container. The most proper way would be to run cron directly in a separate container and process that job there. But this will increase the load on the system and resource … Read more

How to Download File from URL in PHP

You may need to get file contents by URL quite often. For example, when you need to import some data from a third-party service, download an image or a backup. There are several ways to download files in PHP. You can use the file_get_contents() function, which stores the contents of any file into a variable, … Read more