How to detect a mobile device with PHP?

Get user agent of user and redirect user to mobile version.

How to get server key for FCM notification

Login https://console.firebase.google.com
Click the settings icon next to your project name at the top left of the new Firebase Console
Click Project settings
Click on the Cloud Messaging tab
The key is right under Server Key
If your key is not correct it will return Error 401.

Please refer attached Image

 

 

How to reset all fields of a form through JavaScript or jQuery?

In java script form.reset() is a DOM element method (not one on the jQuery object), so you need:
If you want to you use jQuery follow below code

how Use Laravel Auth middleware

 

If you want set any middleware in larave you need to change kernel.php file which is located in app/http folder
In Kernel.php – you can set middleware like this:

/**
* The application’s route middleware.
*
* @var array
*/
protected $routeMiddleware = [
‘auth’ => ‘App\Http\Middleware\Authenticate’,
‘auth.basic’ => ‘Illuminate\Auth\Middleware\AuthenticateWithBasicAuth’,
‘guest’ => ‘App\Http\Middleware\RedirectIfAuthenticated’,

‘admin’ => \App\Http\Middleware\Admin::class,
‘guestadmin’ => ‘App\Http\Middleware\RedirectIfAuthenticated’,
];

If you want to create Admin middleware you need to create Admin.php in middleware folder which in located in app folder.
Create admin and copy code below:-
return redirect()->route(‘admin.login’);
}

return $next($request);
}

}

Define ROutes in route file
/*
|————————————————————————–
| Application Routes for BackEnd
|————————————————————————–
*/
Route::group([‘namespace’ => ‘BackEnd’, ‘prefix’ => ‘admin’], function() {
#Access only guest users
Route::group([‘middleware’ => [‘adminguest’]], function(){
#Signin
Route::get(‘/login’, [
‘as’ => ‘admin.login’,
‘uses’ => ‘AdminController@getSignin’
]);
Route::post(‘/login’, [
‘as’ => ‘admin.login’,
‘uses’ => ‘AdminController@postSignin’
]);
Route::post(‘/logout’, [
‘as’ => ‘admin.logout’,
‘uses’ => ‘AdminController@getLogout’
]);
});

#Access only login users
Route::group([‘middleware’ => [‘admin’]], function(){
#index
Route::get(‘/’, [
‘as’ => ‘admin.index’, function () {
$title = “Dashboard”;
return view(‘BackEnd.dashboard’, compact(‘title’));
}
]);

});

});

Now you are able to acess http://example.com/admin

Write your login code in Admin Controller

Get the size of an image in php

<?php

$img = get_headers(“YourimageURL.jpg”, 1);
print_r ($img);

?>
// OUTPUT It returns araay of image detail
Array
(
[0] => HTTP/1.1 200 OK
[Content-Type] => image/jpeg
[Content-Length] => 27781
[Connection] => close
[Cache-Control] => max-age=5184000
[X-XSS-Protection] => 1; mode=block
[X-Content-Type-Options] => nosniff
[X-Nginx-Cache-Status] => HIT
[X-Server-Powered-By] => Engintron
[Pragma] => public
[Vary] => Accept-Encoding
[Accept-Ranges] => bytes
)

Remove public from URL in Laravel

To remove public from URL in laravel you need to follow below steps.

Rename the server.php in the your Laravel root folder to index.php
Move all folder from public to root.
copy the .htaccess file from /public directory to your Laravel’s project root folder.

Note:- This is not a safe way. A safer way is place Laravel’s files outside your web server root and make the Laravel’s public directory your web server root. Route your website to public folder in place of public_html. You can do this with help of your server administrator.

What is self keyword in PHP

In PHP, you use the self keyword to access static properties and methods.

Below is the example to expalin self

Refer this code:

class ParentClass {
function test() {
self::demo();

}

function demo() {
echo ‘Example of self !’;
}
}

class ChildClass extends ParentClass {
function demo() {
echo ‘Return chlid class’;
}
}

$obj = new ChildClass();
$obj->test();

In this example, self::demo() will always output ‘Example of self !’.
Now we can see that self refers to the class in which it is called.
So, you should use self only when $this is not available, or when you don’t want to allow descendant classes to overwrite the current method.

If we use $this in test method it will return “Return chlid class”;

Show queries in laravel

 

Laravel provides a database query builder. To see the query use toSql() in laravel.

 

$result = DB::table(‘users’)
->where(‘username’, ‘like’, ‘%admin%’)
->toSql() // to see the query which is executed
//  ->get()->toArray();  to get results
dd($result)

Laravel update record

Use Query Builder to update records, This is a sample:

Or Use another method to load DB

generate random number in php

To generate 6 digit random number code in php.

Following function will help you to generate random number.

<?php
function _generate_code(){
$character_set_array = array();
$character_set_array[] = array(‘count’ => 3, ‘characters’ => ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’);
$character_set_array[] = array(‘count’ => 3, ‘characters’ => ‘0123456789’);
$temp_array = array();
foreach ($character_set_array as $character_set) {
for ($i = 0; $i < $character_set[‘count’]; $i++) {
$temp_array[] = $character_set[‘characters’][rand(0, strlen($character_set[‘characters’]) – 1)];
}
}
shuffle($temp_array);
return implode(”, $temp_array);
}

echo _generate_code();