PHP Laravel Application Problems and Solutions 




This is how this article started,

After working in PHP Laravel framework web project development with MySQL back-end encountered a lot of errors.

Before we proceed, 

In-case you like to view what is Laravel FrameworkPHP  and PHP on W3Schools and more.


Let's get started the 21 solved solutions,

I was involved in PHP Laravel framework web application development have this article prepared with list of application development problems and solutions.


Here are the list of problems you may encounter in application development. A simple outline drawn explaining solution. A bad method call exceptions, there is no permission named 'view_logexception' for guard 'web', and view errors 404 not found, Call to undefined method InvalidArgumentException::getStatusCode(),Unknown column 'updated_at' in the field list, Cannot create cache directory, Failed to open stream, no such file or directory, Could not find driver MySQL Query PDO exception.




PHP Laravel Framework


Let's explore a list of errors.

 

    1 . Bad method call exceptions
    2.  There is no permission named 'view_logexception' for guard 'web'
    3. This action is unauthorized
    4. Unable to create migration table
    5. View errors.404 not found. laravel
    6. Failed to open stream, no such file or directory
    7. Cannot create cache directory
    8. Laravel indicates the old Host but the solution is to change the password
    9. Could not find driver MySQL Query PDO exception
    10. Unknown column 'updated_at' in the field list
    11.  Array conversion to string
    12. Call to undefined method InvalidArgumentException::getStatusCode()
    13. How to append prefix string in PHP?
    14. PHP application login always refers to previous source (recently logged in)
    15. SSL certificate verification failed
    16.  PHP curl post always returns 0 instead of 200/400 etc
    17. Unable to set private key file
    18. How to set pem certificates (PHP)  in postman?
    19. How to check curl version?
    20. No crontab for user name - what to do?
    21. SQLSTATE[HYT00]: [Microsoft][ODBC Driver 17 for SQL Server] Login timeout expired

     The each of them detailed solution elaborated in below


Complete Solution

1. Bad method call exceptions
Step 1: Open php project Visual Studio Code
Step 2:  Press (Ctrl  + ~) shortcut key in terminal 
Step 3:  Type this composer dump-autoload
Step 4: Now, run php the project
Bad method call exceptions


2. There is no permission named 'view_logexception' for guard 'web'
Step 1: Open php project Visual Studio Code
Step 2:  Press (Ctrl  + ~) shortcut key in terminal and type in step 3
Step 3:  sudo php artisan cache:clear
Step 4: Now, run php the project
no permission named 'object' for guard 'web'


3. This action is unauthorized
If you use mvc project, sure your application must have Authorizable.php class file in app/Authorizable.php.
If a controller needs authorization rights then it is must to use as below image with numbers rounded "use Authorizable"
Note: Ensure no spelling mistake if you have permission added to database table
Unauthorized Controller


4. Unable to create migration table
Step 1: Open php project Visual Studio Code
Step 2:  Press (Ctrl  + ~) shortcut key in terminal and type in step 3
Step 3:  php artisan make:migration create_Address_table --create=Address
Step 4: Now, run php the project
 
Unable to create migration table


5. View errors.404 not found. Laravel
Step 1 : Check isHttpException object is null or not 
Step 2: As reference to the image below  
View errors 404 not found

 
  

6. Failed to open stream, no such file or directory
Step 1: Go to your terminal 
Step 2: Copy below lines and paste in terminal, shortcut key Ctrl + Shift + V
php artisan config:cache
php artisan config:clear
composer dump-autoload -o
Failed to open stream, no such file or directory


  
7.  Cannot create cache directory
Step 1 : Go to Ubuntu terminal, shortcut key Ctrl + Alt + T
Step 2 : Go to your project folder location
Step 3:  Grant permission 777 access to your user account 
Here, user account name is arjun php project is located in /home/arjun and granting owner access rights to 777 
Solution 1: sudo chown -R arjun  /home/arjun/.composer
Solution 2: sudo chown 777 -R  /home/arjun/.composer
Cannot create cache directory

8. Laravel indicates the old Host but the solution is to change the password
This is usual cache created in php project, when you change credentials in .env file. 
Imagine you have entered local database credentials in .env file and later there is a situation you have changed database login user name or password. So you have updating new user name and password in .env file save it. Now try to run your php project in web browser due to application cache login will be unsuccessful.
Step 1: Go to your terminal, shortcut key to open terminal Ctrl + ~
Step 2: Paste the below line in terminal 
php artisan config:cache
Laravel indicates the old Host but the solution is to change the password


9. Could not find driver MySQL Query PDO exception
There are two solutions to this issue 
Solution 1:
Step1: To check whether odbc driver installed or not, execute below line
odbcinst –j 
ODBC installed


Step 2: Type blue in terminal   cd /etc/php/7.3/mods-available
Step 3: sudo nano pdo.ini
Step 4: add extension as below
extension=pdo.so
extension=pdo_sqlsrv.so
Step 5: sudo service apache2 restart
Solution 2:
Step 1: Execute below lines in terminal one by one and ensure your correct php version. 
sudo apt purge php7.3-mysql
sudo apt install php7.3-mysql
sudo service apache2 restart
Here are the references to solve pdo exceptions from Microsoft and StackOverflow

10. Unknown column 'updated_at' in the field list
By right Laravel framework will expect created_at & updated_at column in your database table, set to false to override default setting.
Why this error occurred?
I created a database table with few columns along with created_at column, when run the project error prompted as 'unknown column 'updated_at' in field list, which means updated_at column is missing in field list of model class
How to solve?
Override default settings to false in model class, find the below image for more info
Add this line in model 
public $timestamps = false;
Unknown column 'updated_at' in the field list

11. Array to string conversion
Imagine you have array of collection and situation to convert to a string type, with the help of json_encode() function 
How to use json_encode?
In code use,  json_encode($arraylist);
Array conversion to string


12. Call to undefined method InvalidArgumentException::getStatusCode()
Solution: If you are using Laravel framework, then go to your project folder, find app>exceptions>Handler.php
Look for function render($request, Exception $expection) 
change from $exception->getStatusCode() to $exception->getCode()

13. How to append prefix string in PHP?
Imagine while working in project, you may encounter to prefix some string or char or integer and append to a string
Let's take an example here;
you have a customer id which 'cu820195' and max length should not exceed 10 chars length, you must prefix 00 or ab in front of customer id '00cu820195' or 'abcu820195'.
Just spare few minutes and take a look at below image
Append prefix string in PHP
 


14. PHP application login always refers to previous source (recently logged in)
This error is obvious when you change or update credentials in .env project file
Step 1 : Go to your application terminal, shortcut key Ctrl  + ~
Step 2: Execute below line
php artisan config:cache

15. SSL certificate verification failed
The solution is first to ensure you have valid .pem file in your project folder
Step 1 : Go to your linux/ubuntu terminal 
Step 2: Locate the pem certificate folder you placed
Step 3: Verify is your certificate is valid or not using below command
openssl verify -CAfile /home/arjun/project/access/certs
SSL certificate verification failed

16. PHP curl post always returns 0 instead of 200/400 etc
PHP curl post should return http results, returned 0 means that code implemented may be incorrect.
Let's take an example of below, starting from line number 175 ssl pem certificate are applied but the order of code, circled 1 curl_exec($curl) and circled 2 curl_getinfo($curl) must be the right order. 
 
PHP curl post always returns 0


There are a list of curl errors and
$error_codes=array(
    [1] => 'CURLE_UNSUPPORTED_PROTOCOL',
    [2] => 'CURLE_FAILED_INIT',
    [3] => 'CURLE_URL_MALFORMAT',
    [4] => 'CURLE_URL_MALFORMAT_USER',
    [5] => 'CURLE_COULDNT_RESOLVE_PROXY',
    [6] => 'CURLE_COULDNT_RESOLVE_HOST',
    [7] => 'CURLE_COULDNT_CONNECT',
    [8] => 'CURLE_FTP_WEIRD_SERVER_REPLY',
    [9] => 'CURLE_REMOTE_ACCESS_DENIED',
    [11] => 'CURLE_FTP_WEIRD_PASS_REPLY',
    [13] => 'CURLE_FTP_WEIRD_PASV_REPLY',
    [14]=>'CURLE_FTP_WEIRD_227_FORMAT',
    [15] => 'CURLE_FTP_CANT_GET_HOST',
    [17] => 'CURLE_FTP_COULDNT_SET_TYPE',
    [18] => 'CURLE_PARTIAL_FILE',
    [19] => 'CURLE_FTP_COULDNT_RETR_FILE',
    [21] => 'CURLE_QUOTE_ERROR',
    [22] => 'CURLE_HTTP_RETURNED_ERROR',
    [23] => 'CURLE_WRITE_ERROR',
    [25] => 'CURLE_UPLOAD_FAILED',
    [26] => 'CURLE_READ_ERROR',
    [27] => 'CURLE_OUT_OF_MEMORY',
    [28] => 'CURLE_OPERATION_TIMEDOUT',
    [30] => 'CURLE_FTP_PORT_FAILED',
    [31] => 'CURLE_FTP_COULDNT_USE_REST',
    [33] => 'CURLE_RANGE_ERROR',
    [34] => 'CURLE_HTTP_POST_ERROR',
    [35] => 'CURLE_SSL_CONNECT_ERROR',
    [36] => 'CURLE_BAD_DOWNLOAD_RESUME',
    [37] => 'CURLE_FILE_COULDNT_READ_FILE',
    [38] => 'CURLE_LDAP_CANNOT_BIND',
    [39] => 'CURLE_LDAP_SEARCH_FAILED',
    [41] => 'CURLE_FUNCTION_NOT_FOUND',
    [42] => 'CURLE_ABORTED_BY_CALLBACK',
    [43] => 'CURLE_BAD_FUNCTION_ARGUMENT',
    [45] => 'CURLE_INTERFACE_FAILED',
    [47] => 'CURLE_TOO_MANY_REDIRECTS',
    [48] => 'CURLE_UNKNOWN_TELNET_OPTION',
    [49] => 'CURLE_TELNET_OPTION_SYNTAX',
    [51] => 'CURLE_PEER_FAILED_VERIFICATION',
    [52] => 'CURLE_GOT_NOTHING',
    [53] => 'CURLE_SSL_ENGINE_NOTFOUND',
    [54] => 'CURLE_SSL_ENGINE_SETFAILED',
    [55] => 'CURLE_SEND_ERROR',
    [56] => 'CURLE_RECV_ERROR',
    [58] => 'CURLE_SSL_CERTPROBLEM',
    [59] => 'CURLE_SSL_CIPHER',
    [60] => 'CURLE_SSL_CACERT', Peer certificate cannot be authenticated with known CA certificates
    [61] => 'CURLE_BAD_CONTENT_ENCODING',
    [62] => 'CURLE_LDAP_INVALID_URL',
    [63] => 'CURLE_FILESIZE_EXCEEDED',
    [64] => 'CURLE_USE_SSL_FAILED',
    [65] => 'CURLE_SEND_FAIL_REWIND',
    [66] => 'CURLE_SSL_ENGINE_INITFAILED',
    [67] => 'CURLE_LOGIN_DENIED',
    [68] => 'CURLE_TFTP_NOTFOUND',
    [69] => 'CURLE_TFTP_PERM',
    [70] => 'CURLE_REMOTE_DISK_FULL',
    [71] => 'CURLE_TFTP_ILLEGAL',
    [72] => 'CURLE_TFTP_UNKNOWNID',
    [73] => 'CURLE_REMOTE_FILE_EXISTS',
    [74] => 'CURLE_TFTP_NOSUCHUSER',
    [75] => 'CURLE_CONV_FAILED',
    [76] => 'CURLE_CONV_REQD',
    [77] => 'CURLE_SSL_CACERT_BADFILE',
    [78] => 'CURLE_REMOTE_FILE_NOT_FOUND',
    [79] => 'CURLE_SSH',
    [80] => 'CURLE_SSL_SHUTDOWN_FAILED',
    [81] => 'CURLE_AGAIN',
    [82] => 'CURLE_SSL_CRL_BADFILE',
    [83] => 'CURLE_SSL_ISSUER_ERROR',
    [84] => 'CURLE_FTP_PRET_FAILED',
    [84] => 'CURLE_FTP_PRET_FAILED',
    [85] => 'CURLE_RTSP_CSEQ_ERROR',
    [86] => 'CURLE_RTSP_SESSION_ERROR',
    [87] => 'CURLE_FTP_BAD_FILE_LIST',
    [88] => 'CURLE_CHUNK_FAILED');

17. Unable to set private key file
curl: (58) unable to set private key file: 'server.key' type PEM
The actual issue was used a wrong certificate file. This means that cURL was looking for a private key that belongs to that certificate and couldn’t find it  
To ensure that your certificate and key match, you can use the following commands:
Solution:
$ openssl x509 -noout -modulus -in server.crt | openssl md5
$ openssl rsa -noout -modulus -in server.key | openssl md5
You have to see something that looks like the following:
(stdin)= 47f0c86371b31432504f195357cf2947
If the two values don’t match, you’re not using the correct combination.
 

18. How to set pem certificates (PHP) in postman?
When working with server-to-client communication, server requires SSL certificate authorized client to initiate process.
In PHP LINUX environment Pem format certificate files are needed. In here used certificate authority, client, key pem files along with password.
1. Go to your postman API endpoint settings
2.  Upload certificate authority pem files which is ca.pem
3. Enter your host server ex: https://myhserver.com
4. Upload client certificate file client.pem
5. Upload key file key.pem
6. Finally, enter certificate password
Set pem certificates (PHP) in postman

19. How to check curl version?
Go to Linux command window, enter this command and view the image below
~$ dpkg -l curl
Linux Curl Version


20. No crontab for user name - what to do?
This happens sometimes, when you try to access crontab in Linux.
Let's say your Linux user name is xyz
Go to Linux command line and enter this below 
~$ crontab -u xyz -e 
Crontab Linux


21. SQLSTATE[HYT00]: [Microsoft][ODBC Driver 17 for SQL Server] Login timeout expired
In Linux Ubuntu environment, used Microsoft ODBC Driver 17 to communicate with SQL Server in another server. 
These could be are the reasons for error
TCP 1433 is the default SQL Server port, which (usually) goes to the first instance installed on a given box/VM. If additional named instances of the engine are added, they get dynamic ports.  
SQL Browser (which listens on UDP 1434) is used to hand out the proper ports to incoming requests. The specific port(s) used by an instance can be checked/configured using the SQL Server Configuration Manager.  
Sometimes DBAs or System Admins run SQL Server on a non-default port for security or other reasons. 
Execute blow lines in PHP Environment one by one
php artisan clear-compiled
php artisan cache:clear
php artisan config:clear
php artisan view:clear
php artisan config:cache
php artisan route:cache
php artisan optimize
   

Summary

We have seen the most 21 application errors and solution during PHP application development. In this article referred Linux-Ubuntu 18.04 and Visual Studio Code for PHP program execution.  

The purpose is to help everyone who encounter the same as I did, and the example given and images referred would help.