Laravel's Articles

Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller architectural pattern and based on Symfony.

Getting Started with Laravel in CodeLobster IDE
Laravel is probably one of the most promoted PHP frameworks at the moment. It has a huge team and an excellent knowledge base - many articles, video tutorials and detailed documentation. Professional programmers use it widely in their projects, so beginners also need to learn Laravel as soon as possible. Codelobster helps you to make a local Laravel installation quickly and effortlessly. There is no need to use the command line or download and run VirtualBox and Homestead virtual machine, which is larger than 2 GB. For the work we need our IDE and the latest version of XAMPP, which in all respects is suitable for the correct operation of the newest version of the framework. This article uses XAMPP 7.2.12, and we recommend that you install it too. Launch CodeLobster and go to the main menu "Tools" -> "Preferences" -> "PHP". Specify the path to the executable file "php.exe", in our system, for example, it looks like this - "D:/xampp-7.2.12/php/php.exe". Check "php.ini" file and make sure you have enabled all the necessary PHP-extensions such as php_bz2, php_curl, php_fileinfo, php_gd2, php_gettext, php_mbstring, php_exif, php_mysqli, php_pdo_mysql, php_pdo_sqlite, php_openssl and php_ftp. Now everything is ready, let's run MySQL and Apache servers and start the main work. Installing Laravel in CodeLobster IDE Go to the main menu "Project" -> "Create Project". In the dialog box that appears, select the type of the created project "Create Empty Laravel Project". Specify the project name, which we will place in the "htdocs" folder in the directory with the installed XAMPP. Enable the option "Create project in a new folder" and click "OK" to start the wizard to install the framework. We have to complete a few steps and enter the elementary settings. To connect the system to an existing database, select the "Use Database" checkbox and enter DB name, username and password. In the next dialog box, enter the server name and port. If MySQL is running on the local computer, then nothing needs to be changed at this stage. All data entered by us will be automatically saved in the "config/database.php" file, and later it can be changed manually simply by opening this file in the editor. At the next dialog, we have got an ability to choose additional official packages for the installation: Cashier - it provides an interface for working with Stripe and Braintree services for organizing an online payment system on the site. Envoy - a package for organizing the execution of various tasks on a remote server, this may be the execution of PHP code or the launch of macros with several sequential commands.  Horizon - it provides a control panel and an API for accessing the Redis queue service, this ensures faster operation of WEB applications. Passport - it provides advanced features for user authentication using the API, fully implements the OAuth2 authorization system. Scout - it implements the synchronization of your search index with the records in the model for working with the database and provides the ability to perform full-text search. Socialite - it allows OAuth authentication, so you can use the login with Google, Facebook, Twitter, LinkedIn, GitHub, GitLab and Bitbucket. Such tools will help in the work on large-scale projects, if you are just starting to learn the framework, you can skip this step for now. Click "Finish" and wait a little while CodeLobster downloads the current version of the framework and unpacks it into the designated directory. When the wizard has finished its work, we will check the correctness of the installation - open the address "http://localhost/laravel/public/" in your browser, it is in this folder the public files are stored. As you can see, the Laravel logo appeared, there are no error messages, which means everything is well. We can study the structure of the new project on the "Project" tab, and also edit the main settings of our WEB-application. If you are using a database connection, go to the file explorer in the left pane of the IDE and open the ".env" file in the editor, this file stores the environment variables. Find the line "DB_CONNECTION=mysql" and enter the parameters for connecting to MySQL. In our example, this fragment looks like this: DB_CONNECTION=mysql DB_HOST= DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=rootpassword Testing the Work of the Framework Laravel includes an object-relational mapping (ORM) system, it is called Eloquent and is an implementation of the Active Record design pattern (AR). The rules for naming classes, tables and their fields are used in order to unify ways of connecting objects and their corresponding tables and rows in the DB. In the database that we have connected to the current installation, there is a ready-made table on which we can practice. The name of the table is "countries", it contains the names of countries and phone codes - these are the "name" and "phonecode" fields. To use Eloquent, you need to create your own class that extends the "Model" class. Create a file "Country.php" in the "app" folder and add the following lines to it: <?php namespace App; use Illuminate\Database\Eloquent\Model; class Country extends Model {} According to the naming rules, a class called "Country" (in the singular) will represent entries in the table "countries". This is quite enough for now, the rest of the work for us will be done by the framework. To take advantage of the model for the data access, let's create a router - we will link the URL "http://localhost/laravel/public/countries" to the function of extracting records from the table. Routers for your WEB applications are stored in the "routes" folder, open the "web.php" file in the editor and add a few lines of your own code to it:    Route::get('/countries', function () {     $countries = App\Country::all();     $html = '<ul>'; foreach ($countries as $country) {   $html .= '<li>' . $country->name . '</li>'; } return $html .= '</ul>'; }); The IDE understands the structure of the Laravel project very well and provides hints and dynamic help on all the functions and classes included in the framework. It will be much easier for you to use all its advantages if you use autocomplete while typing by pressing the Ctrl + Space key combination to get the list of methods that are allowed in this context. When we execute our code, an array with information about countries will be extracted, it can now be passed to the view for formatting and display. In our educational example, we simply select the names of the countries and create an HTML list, and it will be displayed using the default view. Open the address "http://localhost/laravel/public/countries" in the browser and see an accurate list with the names of all countries from our database. Let's summarize In this article, we have learned how to quickly create a new project and install Laravel using the wizard in CodeLobster IDE. This is a universal approach - it is suitable for any operating system and does not require the use of the command line or the installation of additional software. The principle of operation of all MVC and ORM frameworks is the same. After reading this article, you have learned the basic steps to get started with Laravel, with this knowledge you will be able to easily and simply master other PHP libraries.
How to Send Mail Notification in Laravel
In this article, we will share with you how to create a notification in laravel 6 and how to send it to users. laravel provides much helpful functionality by default. sometimes we need to send some information by notification to system users we can do it by laravel Notification class and functions(). Here, we will create one simple notification to our laravel application send it to a user by mail or also store some related information into the databases. Where to use Laravel Notification? Many laravel beginners has one question were to use laravel notification in our laravel application. so, we i will give you some real field examples for laravel notification functionality. like when we work with web-based ERP and send users some invoices as well as store some related data into the database then notification it will be very helpful. laravel provide to developer many ways to send notification by notification class 1.) mail, 2.) store into a database, 3.) by broadcast (use for real-time notification by pusher and Redis), 4.) slack All Steps Step 1: Create Laravel Application Step 2: Create Migration Step 3: Create Notification Class Step 4: Create Route Step 5: Create Controller Step - 1 : Create Laravel Application In the first step, we need to create one new fresh laravel 6 application in our local system help by running the following command in your terminal. composer create-project --prefer-dist laravel/laravel NotificationDemo Step - 2 : Create Migration In step two we need to create notifictions table by the following command into the terminal on the project root directory. php artisan notifications:table After the create notifications table migration then run the laravel migration by the following command. php artisan migrate Step - 3 : Create Notification Class In the third step, we need to create the notification class by running the following command in the terminal. php artisan make:notification DemoNotification After, run the above command in the terminal then notification file created at aap/Notifications/DemoNotification.php <?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; class DemoNotification extends Notification { use Queueable; private $details; /** * Create a new notification instance. * * @return void */ public function __construct($details) { $this->details = $details; } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return ['mail','database']; } /** * Get the mail representation of the notification. * * @param mixed $notifiable * @return \Illuminate\Notifications\Messages\MailMessage */ public function toMail($notifiable) { return (new MailMessage) ->greeting($this->details['greeting']) ->line($this->details['body']) ->action($this->details['actionText'], $this->details['actionURL']) ->line($this->details['thanks']); } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toDatabase($notifiable) { return [ 'order_id' => $this->details['order_id'] ]; } } Step - 4 : Create Route In this step, we need to create a route to send notifications to the user. open file routes/web.php file and create the following route. Route::get('send', 'NotificationController@send'); Step - 5 : Create Controller Now, we need to create app/Http/Controllers/NotificationController.php file by run the following command in the terminal. php artisan make:controller NotificationController Now, open the app/Http/Controllers/NotificationController.php and write the following code into it. <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; use Notification; use App\Notifications\DemoNotification; class NotificationController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } public function send(Request $request) { $user = User::first(); $details = [ 'greeting' => 'Hi Artisan', 'body' => 'This is my first notification from HackTheStuff', 'thanks' => 'Thank you for using HackTheStuff article!', 'actionText' => 'View My Site', 'actionURL' => url(''), 'order_id' => 'Order-20190000151' ]; Notification::send($user, new DemoNotification($details)); dd('Your notification send seuccessfully!'); } } You can also send a notification the following way. $user->notify(new MyFirstNotification($details)); And, you can get send notification by following command. dd($user->notifications); Conclusion You can see send notification in laravel is very easy to use. You can check I hope you will like this article.
Increase Session Timeout Limit in Laravel
Today, I will show you how to increase session timeout in laravel 7.  You will learn you set session lifetime in laravel 7.  We will avail you to give an example of laravel 7 increase session timeout.    if you increase session juncture in laravel 7 then you can use bellow two solutions.  Two solutions in first solution in utilizing. env file and second solution in config file used.    you can simply use increased session lifetime in laravel.    rudimentally, you can not set a lifetime session for aeonian but you can set in minutes for session expiration juncture.  so I will set 1-year juncture for session expire 60 * 24 * 365 = 525600 Here i will show how to increase lifetime from env file and configuration file. so let's see both example as bellow: Example 1 : .env File You can easy to define value in minutes in your env file as bellow. .env SESSION_LIFETIME=525600 then after open your config/session.php and change the following into it. use Illuminate\Support\Str; return [ ..... 'lifetime' => env('SESSION_LIFETIME', 120), ..... ] Example 2: Using Config File config/session.php use Illuminate\Support\Str; return [ ..... 'lifetime' => 1 * (60 * 24 * 365), ..... ] i hope you like this small article.
Laravel Eloquent Queries with Eager Loading
When you make relationship in Laravel eloquent relationship, there are two ways you can access eloquent relationships data. In a lazy loading, when you access the property, then relationships data loaded. For example, Post model is belongs to User model. So when you want to get all users data with posts records. Here is Post model. <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Post extends Model {     /**      * Get the users that write article      */     public function user()     {         return $this->belongsTo(User::class);     } } Now we get data in controller. use App\Models\Post; $posts = Post::all(); foreach ($posts as $post) {     echo $post->user->name; } In the above example, all posts data will loop and retrieve user for the post. What if there are hundreds of posts? All will loop and retrive user for the post in all loop. In eager loading, all the users are also retrived with post records. $posts = Post::with('user')->get(); foreach ($posts as $post) {     echo $post->user->name; } This way, eager loading is better than lazy loading to retrive data in one time. Now lets look mode example for eager loading. If you have multiple relationships with single model, you can retrive all relationships records with eager loading. $posts = Post::with(['user', 'category'])->get(); Nested Eager Loading If you have nested Eager Loading, you may retrive as below: $posts = Post::with('user.address')->get(); If you only want to retrive specific columns in Eager Loading, pass id and any other foreign key columns which you want to retrive with records. $posts = Post::with('user:id,email,phone,post_id')->get(); I hope it will help you.
Laravel 8 Create CRUD in Livewire with Bootstrap Model Validation
In this blog, I would relish to apportion with you how perform crud opeartion with bootstrap modal livewire in laravel application.I will show you a consummate step by step Laravel Livewire CRUD operation with modal. Livewire is a full-stack framework for Laravel that makes building dynamic interfaces simple, without leaving the comfort of Laravel.Livewire relies solely on AJAX requests to do all its server communicaton. Here I will give full example for crud opertaion livewire bootstrap modal in laravel,So Lets follow the bellow step. Step 1 : Install Laravel App In First step, We need to get fresh laravel version application using bellow command. So Let's open terminal and run bellow command. composer create-project --prefer-dist laravel/laravel blog Step 2 : Setup Database Configuration After successfully install laravel app thenafter configure databse setup. We will open ".env" file and change the database name, username and password in the env file. DB_CONNECTION=mysql DB_HOST= DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name DB_USERNAME=Enter_Your_Database_Username DB_PASSWORD=Enter_Your_Database_Password Step 3 : Install Livewire In this step, You will simply install livewire to our laravel application using bellow command: composer require livewire/livewire Step 4 : Create Component Now, You can create livewire component using bellow command, So Let's run bellow command to create user form component: php artisan make:livewire users  
Laravel 8 Authentication using Livewire Jetstream
Today, let's optically discern the post of laravel 8 jetstream auth utilizing livewire. today you can visually perceive laravel 8 auth with livewire jetstream. we will avail you to give an example of laravel 8 auth with livewire tutorial. I learn simply step by step laravel 8 authentication livewire example. So, let's follow a few steps to engender an example of authentication laravel 8 livewire jetstream. A few days ago laravel 8 relinquished and they provide lots of incipient updates. laravel 7 was utilizing laravel/ui for auth scaffolding and now laravel 8 introduce jetstream for authenticate, registration, email verification, two-factor authentication, session management, API support, and team management, today, Laravel 8 jetstream designed by Tailwind CSS and they provide auth utilizing livewire and Inertia this tutorial. today, I will show you how to integrate auth in laravel 8. you can engender laravel auth with jetstream facilely  step by step. so let's optically discern follow bellow step and get it laravel 8 authentications with jetstream utilizing livewire. Laravel Livewire is a library that makes it simple projct build modern, reactive, dynamic interfaces utilizing Laravel Application. Livewire provides a indite to your ajax with laravel blade, validation, etc. you can utilize a javascript framework. so you can visually perceive bellow step to engender auth utilizing laravel 8 livewire. Install Laravel 8: here, we need to install laravel 8 application using composer command. composer create-project --prefer-dist laravel/laravel blog Install Jetstream: Now, in this step, we need to use composer command to install jetstream, so let's run bellow command and install the bellow library. composer require laravel/jetstream Create Auth With Livewire: now, we need to create authentication using the bellow command. you can create basic login, register, and email verification. if you want to create team management then you have to pass an additional parameter. you can see bellow commands: php artisan jetstream:install livewire OR php artisan jetstream:install livewire --teams Now, let's node js package: npm install let's run package: npm run dev now, we need to run migration command to create database table: php artisan migrate Now, you can run and check. they installed all views, actions, and all in your laravel 8 application. Laravel 8 Jetstream Features Laravel 8 Jetstream provides new all feature are configurable. you can see there is a configuration file fortify.php and jetstream.php file where you can enable and disable option for that feature: config/fortify.php .... 'features' => [ Features::registration(), Features::resetPasswords(), Features::emailVerification(), Features::updateProfileInformation(), Features::updatePasswords(), Features::twoFactorAuthentication(), ], ... config/jetstream.php .... 'features' => [ Features::profilePhotos(), Features::api(), Features::teams(), ], ... i hope you like this article.
How to Compare current password with hash password in Laravel
In this article we will share with you how to check your current password string with laravel's hash generated password help of hash::check() function. we know laravel store password in hash formate so password not seen in readable formate. so, how to check any string with that hash converted laravel's password string? Many times you need to compare any password string with laravel's olds password hash string. this is needed when we built password change functionality in an application. before changes password, we want to check the user put the string in the input box that is matched with his/her old password string? You can be done it help of Hash::check() function in your laravel application. Example public function changePassword(Request $request) { $input = $request->all(); $user = User::find(auth()->user()->id); // Check password string with hash string.. if(!Hash::check($input['current_password'], $user->password)) { dd('Return error with current passowrd is not match.'); }else{ dd('Write here your update password code'); } } As you can see, how to check your current password string with your old one laravel's hash password in your laravel application help of Hash::check() function. We hope that small tutorials help everyone. if you have known more ways to check the current password with an old password in laravel or any issues or questions related to these tutorials so please comment below.
How to check request is ajax or not in Laravel
In this tutorials we will share with you how to check request is ajax or not in laravel. some time we work with laravel API then it is help more. because help of this you can manage you API ajax request and web request in same controller so, you can manage you code very easy way. You can check ajax request help of ajax() function in laravel. Laravel Request class has many method to read HTTP request from the currenct request. ajax() one of them Example - 1 public function index(Request $request) { if($request->ajax()){ return response()->json(['status'=>'Ajax request']); } return response()->json(['status'=>'Http request']); } Example - 2 public function index() { if($request->ajax()){ return response()->json(['status'=>'Ajax request']); } return response()->json(['status'=>'Http request']); } We hope these small tutorials help everyone. if you like this article then please comment below. Thanks..