by Editorial Staff | May 5, 2021 | Laravel
All beginners have misunderstood the Laravel routing and MVC structure and want to remove public from laravel url. The public clause is there for security purposes.
Whenever you try to run your application using your local server including Xampp or Wamp, you always encounter the public clause in your laravel URL.
Solution #1 – Best Way to Remove Public from Laravel URL
You can map your local IP or local web address to the public directory of Laravel.
- First, you need to add a local address to your host file.
- If on windows go to C:windows/system32/drivers/etc/hosts edit this file an administrator.
- add a new line at the end, 127.0.0.1 laravel.local
-
- save (as administrator).
Once done, then navigate to your Xampp or Wamp folder
- open, httpd-vhosts.conf file from xampp/apache/conf/extra directory
- Add the below lines to it and save it.
|
<VirtualHost *:80> ServerName laravel.local ServerAlias laravel.local DocumentRoot "C:\xampp\htdocs\laravel\public" <Directory "C:\xampp\htdocs\laravel\public"> Options +Indexes +Includes +FollowSymLinks +MultiViews AllowOverride All Require local </Directory> </VirtualHost> |
3. Now restart your Apache server.
Now you can navigate to http://laravel.local in your browser and you won’t see any public clause in your url.
This is the only best way to remove public from laravel url.
Solution #2 On Live server (Shared Hosting or Cloud)
If you are running your Laravel project live on server using Shared Hosting or Cloud then you need to point the domain to public directory.
- Open your web hosting WHM, Cpanel or Cloud Account.
- Navigate to Add-ons or Domain on WHM and Cpanel or websites on Cloud.
- Manage or edit the domain listed for your laravel project.
- You may see the pointing directory something like username/public_html/laravel_website_domain/
- you need to change and point it to the public directory like this username/public_html/laravel_website_domain/public
- Once it is pointed to the public, you are good to go.
Also, read more about Specific table migration in laravel
Solution #3 Put .htaccess and index file in root (Not Recommended)
- Go to Laravel project
- navigate to public directory
- cut or copy the htaccess file and paste it in the root directory just besides .env and other files.
- rename your server.php file to index.php
- And you are good to go. (php artisan serve command won’t work anymore)
This is how to remove public from laravel url in the 3 different ways. Let me know in the comment section which one worked for you.
I personally do not recommend the third solution but sometimes it works for many people. It’s not recommended due to a security issue because it will make a loophole to let someone view the env file and you never want this to happen.
by Editorial Staff | Apr 16, 2020 | Laravel
PDOException::(“SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes”)
While migration sometimes you face SQLSTATE[42000] error which says the specified key was too long.
You may face this error because you are running an older MySQL version, you may need to update your MySQL version to v5.7.7+.
As per the migration guide under Index Length MySQL/MariaDB,
You may also want to read Specific Table Migration in Laravel if ever need to migrate a specific table.
To figure out this error all you have to navigate to your Providers folder and open AppServiceProvide.php file.
SOLUTION: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long;
|
use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); } |
Use the Schema at the top of the file just before public class and write default string length inside the boot the function
by Editorial Staff | Jan 30, 2020 | Laravel
You are stuck in finding those values which are null or not null in laravel and don’t know how to check where null or where not null conditions using laravel eloquent model. Laravel provides built-in eloquent functions that perform the same duty for us.
Solution – Laravel is NULL?
To find the is null value in model eloquent query you have to put the where clause like Model::where(“column name”, null) like below
|
MyModelName::where('username', null)->get()->all(); |
It will return all records which has null username.
Solution – Laravel is Not NULL?
To find those records which have no null records in laravel by checking a specific columns you can use the WhereNotNull() function.
|
MyModelName::whereNotNull('username')->get()->all(); |
It will return all those records which has no null values.
|
MyModelName::where('username', '<>', null)->get()->all(); |
This will do the same because in SQL we use <> signs for NOT, by running the above query will return all those records which no null value.
by Editorial Staff | Jan 7, 2020 | Laravel
You need to specific table migration in Laravel because you don’t want to drop or trunk the existing data within the tables or for some other reasons you just don’t want to play with the database.
Create a migration first
Open your terminal and run this command.
|
php artisan make:migration create_mrasta_table |
Create a new folder in migrations
Here you will need to create a new folder within the migrations folder, for now let’s create a new folder labeling as ‘ mrasta ‘
Move your file
You need to move your newly created migration file to the newly created folder (mrasta)
[[ find how to redirect unauthorized user access to 403 custom page ]]
Open the terminal once again and finally hit the below command
|
php artisan migrate --path=/database/migrations/mrasta |
or
|
php artisan migrate:refresh --path=/database/migrations/mrasta |
By running the above migrate command, it will only migrate the newly created files within the specified directory which you have created, in the above example the command will migrate all of the files in “mrasta folder”.
Wrapping up Specific Table Migration in Laravel
That is how we do migrate a specific table in Laravel, if you still face any trouble, let us know in the comment section. We will get back to you as soon as possible.
by Editorial Staff | Jul 17, 2019 | blogC, Laravel
While updating the composer you encounter an unknown and strange error “Laravel Composer Install, Don’t Install Laravel Framework”. That is happened due to mismatch of laravel version and the package version.
Sometimes package version is latest and laravel version is old or vice versa sometimes. Both needs to be latest or same version to support each other.
Possible Solution 1: Don’t Install Laravel/Framework
|
composer require laravel/framework |
By running this command, it will revert or update the composer.json after that you can run composer update command to make the changes you want.
Possible Solution 2:
Follow the below steps to diagnose the problem.
- go to composer.json file
- look for packages which you don’t use
- remove them from the require list
- then run composer update
Conclusion
If you still need help regarding the issue of Laravel Composer Install, Don’t Install Laravel Framework, don’t hesitate to ask us in the comment.
by Editorial Staff | Dec 28, 2018 | Laravel
The problem in Laravel is it doesn’t redirect unauthorized users to a specific page but just throw an exception to the user, to redirect to 403 custom forbidden page you have to edit the laravel by default code.
Possible Solution 1 – Redirect Unauthorized Users Laravel
Please navigate to the App\Exceptions\Handler.php and open the Handler.php file.
And use Authentication Exception Class at the top.
|
use Illuminate\Auth\Access\AuthorizationException; |
Now scroll down to the public function render($request, Exception $exception) function
|
public function render($request, Exception $exception) { if ($exception instanceof AuthorizationException) { if ($request->expectsJson()) { return response()->json(['error' => 'Unauthorized.'], 403); } // TODO: Redirect to error page instead // Redirect user from here whatever the route you want. return redirect()->route('error'); } // this will still show the error if there is any in your code. return parent::render($request, $exception); } |
Final Words – This Action is Unauthorized Laravel
That’s it. You don’t need to do anything else. Redirect the user from the route using middleware. If you still have a question please ask in the comment we will try our best to keep in touch with you.