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 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 Change Column in Migration Laravel

Laravel allows developers to use migrations for creating database tables. However, it’s not always easy to determine in advance which columns will be needed in the future or what type of data they will store. Consequently, rarely you may want to change a column name or even its data type. You can use the renameColumn() … Read more

How to Return Exception as JSON in Laravel

By default, when something throws an exception when accessing an API in Laravel, it is not always returned in a JSON response. A JSON response is returned only when the client explicitly indicates that they want to receive JSON in the response. In all other cases, plain HTML code will be returned, or even a … Read more