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.