Posts

How to pass CSRF token with ajax request?

In between head, tag put and in Ajax, we have to add $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });

How to get data between two dates in Laravel?

In Laravel, we can use whereBetween() function to get data between two dates. Example Users::whereBetween('created_at', [$firstDate, $secondDate])->get();

How to turn off CSRF protection for a particular route in Laravel?

We can add that particular URL or Route in $except variable. It is present in the app\Http\Middleware\VerifyCsrfToken.php file. Example class VerifyCsrfToken extends BaseVerifier { protected $except = [ 'Pass here your URL', ]; }

How do you make and use Middleware in Laravel?

It acts as a middleman between a request and a response. Middleware is a type of filtering mechanism used in Laravel application. We can create middelware with php artisan make:middleware UsersMiddleware Here "UsersMiddleware" is the name of Middleware. After this command a "UsersMiddleware.php" file created in app/Http/Middleware directory. After that we have to register that middleware in kernel.php (available in app/Http directory) file in "$routeMiddleware" variable. 'Users' => \App\Http\Middleware\UsersMiddleware::class, Now we can call "Users" middleware where we need it like controller or route file. We can use it in controller file like this. public function __construct() { $this->middleware('Users'); } In route file we can use like this. Route::group(['middleware' => 'Users'], function () { Route::get('/', 'HomeController@index'); });

How to use Stored Procedure in Laravel?

How to create a Stored Procedure To create a Stored Procedure you can execute given code in your MySQL query builder directly or use phpmyadmin for this. DROP PROCEDURE IF EXISTS `get_subcategory_by_catid`; delimiter ;; CREATE PROCEDURE `get_subcategory_by_catid` (IN idx int) BEGIN SELECT id, parent_id, title, slug, created_at FROM category WHERE parent_id = idx AND status = 1 ORDER BY title; END ;; delimiter ; After this, you can use this created procedure in your code in Laravel. How to use stored procedure in Laravel $getSubCategories = DB::select( 'CALL get_subcategory_by_catid('.$item->category_id.')' );

How to upload files in laravel?

We have to call Facades in our controller file with this : use Illuminate\Support\Facades\Storage; Example if($request->hasFile(file_name')) { $file = Storage::putFile('YOUR FOLDER PATH', $request->file('file_name')); }

How to use multiple databases in Laravel?

To use multiple databases in Laravel, follow these steps carefully. Ensure these settings in the .env file Add these following lines of code in the config/database.php file to clearly define the relationship between the two databases Execute the query with particular database. 1. Ensure these settings in the .env file DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=your_db_name DB_USERNAME=bestinterviewquestion DB_PASSWORD=admin@123 DB_CONNECTION_SECOND=mysql DB_HOST_SECOND=localhost DB_PORT_SECOND=3306 DB_DATABASE_SECOND=your_db_name2 DB_USERNAME_SECOND=bestinterviewquestion DB_PASSWORD_SECOND=admin@12345 2. Add these following lines of code in the config/database.php file to clearly define the relationship between the two databases 'mysql' => [ 'driver' => env('DB_CONNECTION'), 'host' => env('DB_HOST'), 'port' => env('DB_PORT'), 'database' => env('DB_DATAB...