How to Manually Logging In A User in Laravel

If you want to log an existing user into your laravel application, There are various methods available we will discuss them one by one. To login the user manually in Laravel you need to follow the simple steps below for login.

Step 1. Fetch the user from the DB which you want to logging In in the Laravel.

$user = User::find(1); //You can add as per your condition (with email,password etc.)
Auth::login($user);

It will allow the user to logging in the Laravel application.

To manually login the user in Laravel has second optional argument, which is used for remember me. If you pass the second argument as true. If this argument passed as true then user will be logged in for indefinite time.

Auth::login($user, true);

https://laravel.com/docs/8.x/authentication#authenticate-a-user-instance

What is the difference between = , == and === ?

All of the above are called operators. Operator is a string which instructs PHP to perform given action.

= is called assignment operator. It is used for assign value to a variable. It will assign value left to the variable.

$x = 5; //5 is assigned to variable x.
$name = "Test string"; // Test string assigned to variable name.

== is called comparison operator. Double equal (==) is used to compare operands on both sides (left and right side). Double equal will ignore the type of operands.

$x = 5;
$y = "5"
$x==$y; //It will give true

=== is also called comparison operator, But it will check the type of the variable along with value.

$x = 5;
$y = "5";
$x === $y; //It will give false. Because types of $x and $y are not same.

I hope it clears. If you need any help add in the comment.

How to add empty directory to git repository?

Whenever we are creating a directory in git folder git shows us nothing to commit, it is by the design of git we can not commit empty directories. So to commit empty directory to git repository follow the steps below

  1. Create an empty directory
$ mkdir test

2. Then create an empty directory

$ touch test/.gitkeep

3. Add your directory with this command

$ git add test

4. Now you can commit your empty directory to git repository.

$ git commit -m "Empty directory added to git repository"

How to log query in laravel

To log a query in laravel there are multiple methods available. I am going to share one of the method log query in laravel.

Why we have required to log queries? Sometimes we are not getting correct results when we are running our application. Sometime not able to figure out issue. We have requirement to check the query what is running in the backend. So we need query which can be get by logging in laravel. To log query in laravel follow the below steps.

  1. Open the AppServiceProvider.php file you can found app/Providers/AppServiceProvider.php on the path.
  2. Add the following code to log query in laravel
        \DB::listen(function($sql) {
            \Log::info('SQL', [$sql->sql]);
            \Log::info('Query parameters', [$sql->bindings]);
            \Log::info('Query execution time', [$sql->time]);
        });

The above code will log queries executed on the page, their parameters and the execution time as well in the log file. All the queries in the laravel logged by above code in the log file. You can add condition as per your choice or requirement. If you want to log only specific query you can add this code to your desired place to log the query.

For more information to log query in laravel you can refer to https://laravel.com/docs/8.x/database#listening-for-query-events

One other option I would like to recommend to use telescope. https://laravel.com/docs/8.x/telescope#introduction

How to check php version in command line?

If you want to know the PHP version you are running through the command line, you need to just type in the command the following line.

php -v

It will give you the current php version of your system which is currently using and output of PHP version like this (not exactly, depends on your version)

PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Go Programming Language (Definition)

Go is an open source, procedural, compiled and statically typed programming language. It was designed and developed at Google by Rob pike, Ken Thompson and Robert Griesemer. It is syntactically similar to C, also provides some additional features memory safety, garbage collection, dynamic types. Some people called it golang also.

What is Accessor in Laravel?

As we know laravel accessor is very userful feature. It is used for creating the virtual attribute on the object which you want to access it as database column name.

So for example if you want to show full name and in your database table (users) has first_name and last_name columns, you can write:

public function getFullNameAttribute()
{
  return $this->first_name . " " . $this->last_name;
}

Then you can call $user->full_name and it will return the first and last name collectively. Naming convention for accessor is a snake_case attribute, so getFullNameAttribute function would be accessible through $user->user_name.

prefix must be added as get and suffix would be Attribute