How to add a full-text search using Scout and Algolia in Laravel 6?

How to add a full-text search using Scout and Algolia in Laravel 6?

In this Laravel 6 tutorial explains how to add a full-text search using Scout and Algolia in Laravel 6. Full Text Search using Scout in Laravel 6. Full Text Search using Scout and Algolia in Laravel 6. We will use Scout and Algolia to perform the search.

In this example, I will show you how to add full text search using scout in your Laravel 6 application. Here we will use scout ans algolia to perform full text search.

You just need to create an account in algolia.com and create one index where your data will store.

Follow below step to add full text search in your application :

Install Laravel 6

You will create laravel 6 fresh application.

composer create-project --prefer-dist laravel/laravel ScoutFullTextSearch
Install Scout Package

Now here we will run below command to install scout package.

composer require laravel/scout

config/app.php

Add ScoutServiceProvider to providers array in app.php file like as below :

'providers' => [

    ....

    Laravel\Scout\ScoutServiceProvider::class,

]

Publish Assets

Let's go ahead and publish the assets provided by the Scout library using the following command.

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Install Algolia

Now we have to install algolia package by running below command.

composer require algolia/algoliasearch-client-php
Package Configuration

Now we have to create new account in algolia.com. So if you haven't account on algolia.com site then click here and create new account : algolia.com.

From above screenshot you can get app id and secret from dashboard.

config/scout.php

Add id and secret in scout.php file like as below .

'algolia' => [

    'id' => env('ALGOLIA_APP_ID', '*******'),

    'secret' => env('ALGOLIA_SECRET', '******************'),

],

.env

Also put id and secret in .env file .

ALGOLIA_APP_ID=*********

ALGOLIA_SECRET=***********************

routes/web.php

Add product route in web.php file.

Route::get('product', ['as'=>'product.index','uses'=>'[email protected]']);
Route::post('product-add', ['as'=>'product.add','uses'=>'[email protected]']);
Create Controller

Run below command to make product controller

php artisan make:controller ProductController

app\Http\Controllers\ProductController.php

update productcontroller.php like as below :

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\Product;


class ProductController extends Controller

{

    /**

     * Get the index name for the model.

     *

     * @return string

    */

    public function index(Request $request)

    {

        if($request->has('search')){

            $products = Product::search($request->search)->paginate(5);

        }else{

            $products = Product::paginate(5);

        }

        return view('product-index',compact('products'));

    }



    /**

     * Get the index name for the model.

     *

     * @return string

    */

    public function create(Request $request)

    {

        $this->validate($request,[

            'name'=>'required',

            'quantity'=>'required'

        ]);


        $products = Product::create($request->all());


        return back();

    }

}
Create Model

Run below command to make product model

php artisan make:model Product

app\Product.php

update product.php like as below :

<?php


namespace App;


use Illuminate\Database\Eloquent\Model;

use Laravel\Scout\Searchable;


class Product extends Model

{

    use Searchable;


    public $fillable = [

        'name','quantity'

    ];


    /**

     * Get the index name for the model.

     *

     * @return string

    */

    public function searchableAs()

    {

        return 'product';

    }

}
Create Blade File

Put below code in your product blade file .

product-index.blade.php

<!DOCTYPE html>

<html>

<head>

    <title>Laravel 6 Scout Search</title>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

</head>

<body>

<div class="container">

    <div class="row">

        <div class="col-md-12">

            <h2 class="mt-5">Laravel 6 Scout Search</h2><br/>

            <form method="POST" action="{{ route('product.add') }}" autocomplete="off">

                <input type="hidden" name="_token" value="{{ csrf_token() }}">

                <div class="row">

                    <div class="col-md-4">

                        <div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">

                            <input type="text" id="name" name="name" class="form-control" placeholder="Enter Name" value="{{ old('name') }}">

                            <span class="text-danger">{{ $errors->first('name') }}</span>

                        </div>

                    </div>

                    <div class="col-md-4">

                        <div class="form-group {{ $errors->has('quantity') ? 'has-error' : '' }}">

                            <input type="text" id="quantity" name="quantity" class="form-control" placeholder="Enter Quantity" value="{{ old('quantity') }}">

                            <span class="text-danger">{{ $errors->first('quantity') }}</span>

                        </div>

                    </div>

                    <div class="col-md-4">

                        <div class="form-group">

                            <button class="btn btn-primary rounded-0">Create New Product</button>

                        </div>

                    </div>

                </div>

            </form>

        </div>

    </div>

    <div class="row mt-3">

        <div class="col-md-12">

            <h5><strong>Product</strong></h5><hr>

            <form method="GET" action="{{ route('product.index') }}">

                <div class="row">

                    <div class="col-md-6">

                        <div class="form-group">

                            <input type="text" name="search" class="form-control" placeholder="Search" value="{{ Request::get('search') }}">

                        </div>

                    </div>

                    <div class="col-md-6">

                        <div class="form-group">

                            <button class="btn btn-primary rounded-0">Search</button>

                        </div>

                    </div>

                </div>

            </form>

            <table class="table table-bordered">

                <thead>

                    <th width="5%">Id</th>

                    <th>Name</th>

                    <th>Quantity</th>

                </thead>

                <tbody>

                    @if($products->count())

                        @foreach($products as $key => $value)

                            <tr>

                                <td>{{ ++$key }}</td>

                                <td>{{ $value->name }}</td>

                                <td>{{ $value->quantity }}</td>

                            </tr>

                        @endforeach

                    @else

                        <tr>

                            <td colspan="4">There are no data.</td>

                        </tr>

                    @endif

                </tbody>

            </table>

            {{ $products->links() }}

        </div>

    </div>

</div>

</body>

</html>

It will help you....

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

In Laravel 6 tutorial, we learn how to use PHP artisan command interface (CLI) to clear the cache from Laravel 6.8 application. How To Clear Cache in Laravel 6.8 Application using Artisan Command Line Interface (CLI)? How to clear route cache using php artisan command? How to easily clear cache in Laravel application? How to clear config cache in PHP Laravel via artisan command? How to clear Laravel view cache? How to Reoptimized class in Laravel via artisan CLI?

Today in this tutorial, we are going to learn how to clear route cache, laravel application cache, config cache, view cache and reoptimized class in a Laravel 6.8 application using artisan command-line interface.

I’m pretty sure many of you may have found yourself gotten into the situation where you do not see changes in the view after making the changes in the app.

Laravel application serves the cached data so caching problem occurs due to the robust cache mechanism of Laravel.

But, if you are still facing this issue, then you do not have to worry further. Let me do the honour of introducing you some of the best artisan commands to remove the cache from your Laravel app via PHP artisan command line interface.

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

Table of Contents

  • Clear Route Cache in Laravel
  • Clear Laravel Application Cache
  • Clear Config Cache via PHP Artisan
  • Clear Laravel View Cache
  • Reoptimized Class
Clear Route Cache in Laravel

Laravel caching system also takes routes in consideration, to remove route cache in Laravel use the given below command:

php artisan route:cache
Clear Application Cache in Laravel

Run the following command to clear application cache:

php artisan cache:clear
Clear Config Cache in Laravel

Run the following command to clear config cache:

php artisan config:cache
Clear View Cache in Laravel

Run the following command to clean your view cache:

php artisan view:clear
Reoptimize Class

Run the below command to reoptimize the class loader:

php artisan optimize

Conclusion

We have completed this Laravel 6 tutorial, In this tutorial we learned how to use php artisan command to clear the cache from your Laravel application. We have answered the following questions in this article.

  • How to clear route cache using php artisan command?
  • How to easily clear cache in Laravel application?
  • How to clear config cache in PHP Laravel via artisan command?
  • How to clear Laravel view cache?
  • How to Reoptimized class in Laravel via artisan CLI?

Now, it’s your time to let me know what do you think about this laravel 6 article. Go forth and try these super awesome artisan commands and let me know how these commands are helping you.

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather. Laravel Weather is a good package which we can use to get weather data. It's a wrapper around Open Weather Map API (Current weather). A wrapper around Open Weather Map API (Current weather)

🌤️ A wrapper around Open Weather Map API (Current weather)

Installation

You can install the package via composer:

source-shell
composer require gnahotelsolutions/laravel-weather
Usage
text-html-php
$weather = new Weather();

// Checking weather by city name
$currentWeatherInGirona = $weather->get('girona,es');

// You can use the city id, this will get you unambiguous results
$currentWeatherInGirona = $weather->find('3121456');

Units

By default the package uses metric for Celsius temperature results, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inUnits('imperial')->get('girona,es');

Language

By default the package uses es for the description translation, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inLanguage('en')->get('girona');

Guzzle Client Instance

If you need to use another instance of Guzzle, to modify headers for example:

text-html-php
$weather = new Weather();

$guzzle = $this->getSpecialGuzzleClient();

$currentWeatherInGirona = $weather->using($guzzle)->get('girona');

Testing

source-shell
composer test

How to Use Sweet Alert in Laravel?

How to Use Sweet Alert in Laravel?

This Laravel tutorial explains how to use Sweet Alert in Laravel. Laravel SweetAlert is a Laravel package by Rashid Ali that integrates the popularJavaScript SweetAlert 2 library with Laravel. Sweet Alert is a Laravel wrapper around SweetAlert JavaScript library for showing alert or notification message.

This Laravel tutorial explains how to use Sweet Alert in Laravel. Laravel SweetAlert is a Laravel package by Rashid Ali that integrates the popularJavaScript SweetAlert 2 library with Laravel. Sweet Alert is a Laravel wrapper around SweetAlert JavaScript library for showing alert or notification message.

This is a great package created by rashid, and this laravel package is very famous because it has tonnes of features you can choose from.