How to remove public from Laravel url version 5 and above

How to remove public from Laravel url version 5 and above

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.

  1. First, you need to add a local address to your host file.
  2. If on windows go to C:windows/system32/drivers/etc/hosts edit this file an administrator.
  3. add a new line at the end, 127.0.0.1             laravel.local
  4. save (as administrator).

Once done, then navigate to your Xampp or Wamp folder

  1. open, httpd-vhosts.conf file from xampp/apache/conf/extra directory
  2. Add the below lines to it and save it.

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.

  1. Open your web hosting WHM, Cpanel or Cloud Account.
  2. Navigate to Add-ons or Domain on WHM and Cpanel or websites on Cloud.
  3. Manage or edit the domain listed for your laravel project.
  4. You may see the pointing directory something like username/public_html/laravel_website_domain/
  5. you need to change and point it to the public directory like this username/public_html/laravel_website_domain/public
  6. cpanel document directory popup
  7. 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)

  1. Go to Laravel project
  2. navigate to public directory
  3. cut or copy the htaccess file and paste it in the root directory just besides .env and other files.
  4. rename your server.php file to index.php
  5. 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.

1071 Specified key was too long Laravel Migration Error

1071 Specified key was too long Laravel Migration Error

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 the Schema at the top of the file just before public class and write default string length inside the boot the function

Where Null or Not Null in Laravel, Empty and Not Empty in Laravel

Where Null or Not Null in Laravel, Empty and Not Empty in 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

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.

It will return all those records which has no null values.

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.

 

Specific Table Migration in Laravel

Specific Table Migration in 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.

 

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

or

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.

Laravel Composer Install, Don’t Install Laravel Framework

Laravel Composer Install, Don’t Install Laravel Framework

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

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.

  1. go to composer.json file
  2. look for packages which you don’t use
  3. remove them from the require list
  4. 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.

 

How to Redirect Unauthorized Users | This action is unauthorized Laravel Policy

How to Redirect Unauthorized Users | This action is unauthorized Laravel Policy

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.

 

 

Now scroll down to the public function render($request, Exception $exception) function

 

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.