The error you are having is because the double curly braces {{ which are used in React JS for styling the tag, are also used by Laravel blade template to render the value of a PHP variable. Laravel will think of it as a PHP variable and will throw an error.
To fix this error, you need to create a separate object in your React JS component. That object will have all the styles for each tag. Modify your React JS component to the following:
You can see that instead of setting the CSS directly in the style attribute, I have created a separate variable and used its value as a variable. This way your error gets fixed. Also, it will help you in setting CSS styles to multiple tags without duplicating them.
Most programmers find it difficult and confusing to deploy a Laravel website on a live server. Some don’t know where to start. Some get stuck in creating a virtual host. Some got attracted by cloud servers, but later realized that deploying on a cloud is very difficult.
Most of the developers starts tempering the directory structure of Laravel. Some create their own .htaccess files in order to cope with this problem.
Laravel entry point is public folder
The reason why programmers face difficulty in deploying a Laravel website is because Laravel entry point is its “public” folder, not the root folder (like core PHP or other CMS like WordPress etc). It means that if your domain is “adnan-tech.com”, then your website will be accessible from “adnan-tech.com/public”. This is not a good impression. User must access your site from main domain, not by appending “public” at the end.
Solution
In this article, I will not create a virtual host, nor temper the directory structure of Laravel. Each hosting has a file manager where all files of that server are stored. “public_html” is the primary folder for your domain. For the sake of simplicity, I will assume that you want to deploy your Laravel website in this folder. However, if you are deploying on sub-domain, you can select that sub-domain’s folder.
Upload all files inside “laravel/public” folder to the “public_html” folder.
Then you need to create a new folder at the root of your server.
I am naming it “laravel-server”.
Create a zip of your laravel project (except the “public” folder).
Upload the zip file to the “laravel-server” folder and extract it.
After extracting, you can delete the zip file.
Now open “public_html/index.php” and set the file’s paths to point to “laravel-server” folder.
At this point, you will see your Laravel project running live.
Storage files
Laravel uses a storage folder where all publicly uploaded files can be accessed. All your uploaded files will be stored at “laravel-server/storage/app/public”. We cannot give relative path to this folder. Because, imagine you have uploaded some images and they are now saved in storage folder. You create an <img /> tag and link the image to storage folder (like we did in index.php).
The only problem in this approach is, anyone can open “inspect element” from his browser and check the relative path to your “laravel-server”. Although, it is still very difficult to hack your server, but it is a good idea to keep your server hidden and secure as much as you can.
In order to hide the actual path of the file, we will create a symbolic link to this storage folder. Just like we create shortcuts of our games on Desktop, but the actual files are in some other location. User access the shortcut file and the shortcut file points towards the actual path of the file.
Open terminal from your cPanel.
At the root, you need to run the following command:
This will create a shortcut of “laravel-server/storage/app/public” folder in “public_html” folder and name the shortcut folder as “storage”.
Now, every file you upload at “laravel-server/storage/app/public” will be accessible from “public_html/storage”.
That’s how you can deploy your Laravel website on live server without compromising securety and without tweaking with the directory structure of Laravel. Although, I hope this tutorial helps you, but if you still face any problem to deploy your Laravel website, you can let me know.
In this article, I will share with you, a boilerplate for authentication created in Laravel 10 and React JS. User authentication is a module used in almost every project. Whether you are creating a blog website, or an E-commerce or a video streaming website, you need to have a page where user can create an account.
User authentication is also very crucial because it plays an important part in email marketing. You can get a gaint list of emails of your users, then you can use it to send emails for your new projects or updates etc.
This boilerplate will help you speed-up the development process by providing all the features you need for user authentication. So you don’t have to re-develop the user authentication module for each new project.
Admin can enable or disable email verification feature.
These are all the features added so far in the authentication boilerplate developed in Laravel and React JS. I will keep adding more features. You can also give your own suggestions for new features or improvements.
Tech stack:
PHP +8.2
Laravel +10
React +18
Bootstrap +5
How to setup:
Goto file “config/database.php” and set your database credentials.
If you face any issue in this, kindly let me know: support@adnan-tech.com
Conclusion
In this article, you have been introduced to source code for user authentication that will be used in almost website. You can download the source code for free and use it as many time as you want.
In this article, I will show you, how you can share value between multiple components in React JS. If you are new to React JS, check this tutorial to setup React JS in your project.
Step 1:
First, we are going to create a global state variable. This will be like a simple Javascript object.
This will accept a callback function that will be defined in the component (later in this tutorial). And that callback function will be pushed into the listeners array.
And create final method in this object will update the values of the state in this object. That function will also call the callback function of all the components are listening to this state. And inside that callback will send the new state object as well as the state currently updated in this call.
First, we are updating the globalState.state object. Passing the object will update only the provided values. So even if your globalState has more variables in state object, this will only update the variables that are passed in setState method.
This will also call all the listeners callback function from globalState as well. But right now, the listeners array is empty. It will only have value when some component listens to it.
Step 4:
We will create another component that will listen to this global state by calling the subscribe method.
First, we are creating a state variable and initializing it with the globalState.state variable. So, initially, the value of state inside Header component will be same as in globalState object.
Then we are calling the subscribe method and passing a callback function in it.
This callback function will only be called when the globalState.setState is called from Header component.
When this callback is called, we are updating the state variable with what is received from Header component.
Now you will be able to view the user name in Content component, the user name that you have set from Header component. That’s how you can share value between components in React JS.
If you are using WordPress, you will notice that when you upload an image media, it creates multiple images of different resolutions. In this article, I will show you how you can disable multiple image sizes on WordPress.
Open your active theme’s functions.php file. It will be in “wp-content/themes/{your_active_theme_name}/functions.php”. Then add the following 2 hooks at the top of functions.php file:
If you are using the DataTable Javascript library and are having trouble searching in all fields of the dataset except for the only one visible, then I will show you, how you can search in all fields of the DataTable.
By default, DataTable searches in all fields only on those fields that are visible on the table. Let’s say you have a dataset containing “name”, “designation”, and “country” fields. But you are displaying only “name”, and “designation” on the table.
In Laravel, generally, you can send the response back to the client only from the method that was directly called from the route. But there is a way to send a response from other method as well.
Note that the function “responseOtherMethod()” is called directly from the route. But it is further calling the “otherMethod()” function. Right now, you will see an empty blank page when you access that route:
http://localhost/laravel/response-other-method
Laravel is calling both of these functions, it is just not returning the response from the second function. To return the response from otherMethod() method, just call throwResponse() from it.
Sometimes CMD picks the wrong version of PHP and not the one we install via XAMPP or MAMP.
Let’s say you have downloaded and installed XAMPP or MAMP with the latest PHP. But when you run the command “php -v” in your terminal, it displays the wrong version of PHP (typically the previously installed version of PHP). This is a common issue when you try to upgrade your PHP version. Previously, you could have upgraded the PHP version easily using Homebrew. But nowadays, Homebrew itself has become too complex and it has its errors.
Problem with Laravel 10
You will also face that error when you try to create a project in Laravel 10+ version. Because Laravel 10 requires PHP greater than 8 and a lot of developers are using PHP 7. The reason why developers are still using PHP 7 is because most of the shared hosting servers are still running on PHP 7. As PHP 8 is a big upgrade, a lot of things have been changed. So changing the PHP version could crash the entire project. On the local computer, it’s fine. But if you have a live website or an API, a crashed website means losing a lot of users and potentially a lot of customers which results in a loss of revenue (which no one wants).
Updating PHP version
To update the PHP version, you simply need to open CMD anywhere in your computer and run the following command:
Here, you can change the path to your own PHP file. Typically, it is in your XAMPP/MAMP “bin” folder. Inside “bin”, you will find the “php” folder. Inside that folder, you can go to your desired PHP version folder. And every PHP version folder also has a “bin” folder in it. Go it in and paste its path there.
The above line is for Mac users. For Windows users, the path will look like:
export PATH="C:\XAMPP\bin\php\php8.2.0\bin:$PATH"
Check PHP version
After that, you need to close the terminal and open it again. Then run the following command to verify the PHP version:
php-v
Now, you should be able to see your selected PHP version.
So that’s it. That’s how you can upgrade your PHP if your CMD is picking the wrong PHP version. If you face any issues with this, kindly do let me know.
Imagine if you are displaying an image from the server in your React app. And the image gets deleted from the server. You do not want to show the broken image icon, as it does not look professional. You can show a placeholder image in your ReactJS App if the original image fails to load.
The following code will by default show the “Jackie Chan 2.jpg” image. If that image fails to load, it will render the “default-img.jpg” image inside the “img” folder.
Be careful: If the placeholder image does not exist as well, it will stuck in an infinite loop. To solve this problem, DO NOT save the placeholder image on the server. Always keep the placeholder images on the client side.
As an example, you can use this image as a placeholder. Make sure to download it in your “img” directory. That’s how you can show a placeholder image in ReactJS if the original image fails to load. You can also read more articles on ReactJS.