Getting data from API works in Postman but not working in localhost

If you are getting the data from an external API when using Postman, but when you try the code provided by Postman in your local host you get no data. A reason for that could be the SSL, first, try to install SSL for your local hosting server. Usually, OpenSSL will do the trick. Here is a link to a step-by-step of how to install SLL over WAMP.

https://infyom.com/blog/how-to-enable-localhost-https-ssl-on-wamp-server

If you still can’t connect to the external API, the reason could be an issue with the SSL certificate validation. (after all, this is a self-signed certification). This error usually occurs when the certificate authority (CA) that issued the SSL certificate used by the server you’re trying to connect to is not recognized or trusted by your system.

If you are using cURL, to resolve this issue, you have a few options:

1- Update your CA certificates: Your system’s CA certificate bundle might be outdated. You can try updating it to include the latest CA certificates. The location of the CA certificate bundle can vary depending on your operating system and cURL installation. In some cases, it may be located at /etc/ssl/certs/ca-certificates.crt.

2-Specify the CA certificate bundle path explicitly: If you have access to the CA certificate bundle, you can specify its path explicitly using the –cacert option with cURL. For example:

curl --cacert /path/to/ca-certificates.crt https://example.com

3- Ignore certificate validation (not recommended): If you’re in a development or testing environment and certificate validation is not crucial, you can bypass the certificate validation check using the -k or –insecure option with cURL. However, this approach is not recommended in production environments because it exposes you to potential security risks. Example:

curl -k https://example.com

If you are using PHP, here is an example

<?php

// Create a cURL handle
$ch = curl_init();

// Set the URL
curl_setopt($ch, CURLOPT_URL, "https://example.com");

// Disable SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if ($response === false) {
    $error = curl_error($ch);
    echo "cURL error: " . $error;
}

// Close the cURL handle
curl_close($ch);
?>

Facing a hard time installing Gulp?

If you are trying to install Gulp and you always get errors related to node-sass, here is a small tip that could fix it for you

Simply uninstall node-sass and install dart-sass

npm uninstall gulp-sass
npm install gulp-dart-sass

Then, update your Gulpfile.js to use gulp-dart-sass instead of gulp-sass for compiling Sass.

By following these steps, you should be able to resolve the compatibility issue with Node Sass and successfully run gulp watch without encountering the “Node Sass does not yet support your current environment” error.

How to abort with custom message in Laravel

If you want to abort with a custom message, you will find that in some code like this,

abort(400, 'custom error message');

the above code will show you the standard 400 error without any custom messages. You need to write the code like this

abort(
    response()->json(['message' => "My Custom error message"], 400)
  );

How to solve “Attempt to read property of Null”

Sometimes in Laravel, when you try to show a property of an object, for example :

{{ $user->name }}

you get an error like this :

Attempt to read property on null

which simply means that the object itself “user” is null and you can’t get a name property of Null. To solve this, you need to make sure that the user object is there first and then get the name but that will be a lot of coding and if statement, while there is a one-character solution to this, it is called a Nullsafe Operator and it is in PHP 8
the code will look like this

{{ $user?->name }}

you can use the same method in a chain of objects or even methods

$country = $session?->user?->getAddress()?->country;

There are other options that you can check in this article

How to use withSum() with where condition

You can use withSum in query with where condition, here is an example:

If you want to list all Projects with the sum of all invoices that were issued this year:

$projects = Project::select('id', 'name', 'created_at')
            ->withSum(
                 ['invoices as prev_invoices' => function($query) {
                     $query->whereYear('invoices.issue_date', date('Y'));
                }], 'amount' 
             )
            ->get();

How to set a function in a model as an attribute

If you have a model with a function that you want to use it as an attribute, all you have to do is to use protected $appends in the Model class

For example, you have a Project class with a function that gets the sum of all invoices for that project

class Project extends Model
{
protected $appends = ['prev_invoices'];
....
public function getPrevInvoicesAttribute()       
    {
        $sum = Invoice::where('project_id', $this->id)
            
            ->sum('amount');
        return $sum;
    } 

Note that the function name must be formatted like that: “get” at the beginning, “Attribute” at the end, and the name to be CamelCase.

How to use “OR” while searching in datatables js

If you want to search for any of multiple strings in datatables , you can use the regex option, here is an example

 table.columns( 3 ).search('string A| String B', true, false).draw();

3 is the index of the column you want to search

String A, String B are the 2 strings you want to search any of them

true: to indicate that you want to use regex

false : tell datatables not to use smart search option as it uses regex and may conflict with the regex you are using

How to view raw SQL of Laravel query?

There are a number of ways to view the raw SQL statement of your Larael query builder

->dd()

A simple way to show you the SQL statement along with its binding is replacing ->get(), first(), .. with ->dd(), here is an example:

$users = User::select('name')
                  ->where('id', '<', 20)
                  ->where('is_active', 1)
                  ->dd();

and the output will be like this:

"select `name` from `users` where `id` < ? and `is_active` = ?"
array:2 [▼
  0 => 20
  1 => 1
]
Continue reading “How to view raw SQL of Laravel query?”

Password protect a folder in xampp

Here is how to protect a folder with username and password in xampp :

  1. Open the command promote
  2. Go to your xampp folder and then go to: /apache/bin/
  3. Run this command :

    htpasswd.exe -c -b .htpasswd my_user _name my_password

    Don’t forget Change “my_user_name” and “my_password”
  4. A file with the name of .htpasswd will be created at the folder of /apache/bin
  5. Create a file with the name of : .htaccess at the folder you want to protect ( don’t forget the dot in .htaccess)
  6. Write the following in the .htaccess file:

    AuthType Basic
    AuthName “v3d”
    AuthUserFile “c:/xampp/apache/bin/.htpasswd”
    require valid-user


    Note that the path in AuthUserFile line (c:/xampp/apache/bin/.htpasswd) is the path to your xampp folder where the .htpasswd file is created, very important to keep this file away from the httdocs folder.

Can’t install extensions on VSCode?

If you are trying to install extensions on VSCode but you can’t. Try to visit this site from your browser:

http://cdn.vsassets.io/

Most probably you will get a “This site can’t be reached” message, to solve this you have to change your default DNS to google free public DNS

8.8.8.8
8.8.4.4

If that may not work for you then yo can try to change the DNS for IPv6 to:

2001:4860:4860::8888
2001:4860:4860::8844

Here is a link for more details on changing the DNS to google DNS

https://developers.google.com/speed/public-dns/docs/using