Using the official expansion pack Scout in Laravel 5.3 + Algolia full-text search in the tutorial examples
Today, I'll give you the next demonstrates how to implement full-text search using Scout + Algolia in Laravel 5.3 in.
Laravel 5.3 provides us with many new features, but also introduced a lot of new expansion pack, and today we will use Laravel 5.3 provides model-based full-text search of the Scout expansion pack. If you want to implement full text search function in Laravel, then the quickest way is to use this expansion pack.
In this tutorial, I will be hands-on and step by step instructions to operate, leading to a better understanding of its principles and implementation, so even easier to use in their own applications.
First of all, give us a preview release:
Next, we like to give you a step by step Paodingjieniu re-set its realization.
Step 1: Install Laravel 5.3 application
If you have not installed Laravel 5.3, then of course, is to install it, after all, can not make bricks without straw, thanks to the Composer, the installation process is extremely simple, just execute the following command in the web root directory can be:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install Expansion Pack
We must first of its profits. In this step, we need to install two expansion packs:
1) Laravel / Scout
2) algolia / algoliasearch-Client-PHP
First, we have to install the Scout by Composer, run the following command to install in the project root directory:
composer require laravel/scout
After a successful installation, open the config / app.php registration service provider:
'providers' => [
....
LaravelScoutScoutServiceProvider::class,
]
Next, we have to publish the configuration file with the following command:
php artisan vendor:publish --provider="LaravelScoutScoutServiceProvider"
After the run is completed, you will find a scout.php new file in the config directory, the file is Scout configuration file.
Well, then we have to install algolia Expansion Pack:
composer require algolia/algoliasearch-client-php
The third step: Expansion Pack Configuration
After installing Algolia extended package, configure Algol id and secret. This requires us to have an account in order to obtain the information on Algolia.com corresponding, if not, then to register a.
After registration and login id to obtain secret information through this application link, web interface and location information as follows: set id and secret information to the above .env configuration file:
ALGOLIA_APP_ID=paste app id
ALGOLIA_SECRET=paste app secret
Step Four: Create a data table and model
After doing the above preparations, we need to provide Artisan command by Laravel create a migration table for the items:
php artisan make:migration create_items_table
After running this command, you will see the migration of newly generated files in the database / migrations directory, edit the file reads as follows:
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop("items");
}
}
After creating items table, but also to create the corresponding model class for the table. We create Item.php file in the app directory and edit its contents as follows:
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use LaravelScoutSearchable;
class Item extends Model
{
use Searchable;
public $fillable = ['title'];
/**
* 获取模型的索引名称
*
* @return string
*/
public function searchableAs()
{
return 'items_index';
}
}
Step five: add a new route
In this step we need to add new items to its list of routes. We add the following route in routes / web.php file:
Route::get('items-lists', ['as'=>'items-lists','uses'=>'ItemSearchController@index']);
Route::post('create-item', ['as'=>'create-item','uses'=>'ItemSearchController@create']);
Step Six: Create Controller
After the preceding operations, then we create routing controller defined app / Http / Controllers / ItemSearchController.php, a controller for managing a list of items and a new item and return a response. Therefore, we edit the file controller reads as follows:
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use AppItem;
class ItemSearchController extends Controller
{
/**
* items列表
*/
public function index(Request $request)
{
if($request->has('titlesearch')){
$items = Item::search($request->titlesearch)
->paginate(6);
}else{
$items = Item::paginate(6);
}
return view('item-search'大专栏 indiemaple's blog">,compact('items'));
}
/**
* 创建新的item
*/
public function create(Request $request)
{
$this->validate($request,['title'=>'required']);
$items = Item::create($request->all());
return back();
}
}
Step Seven: Create a view
The final step, let's create a view file resources / views / item-search.blade.php, edit view file code is as follows:
<!DOCTYPE html>
<html>
<head>
<title>Laravel 5.3 - laravel scout algolia search example</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Laravel Full Text Search using Scout and algolia</h2><br/>
<form method="POST" action="" autocomplete="off">
@if(count($errors))
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.
<br/>
<ul>
@foreach($errors->all() as $error)
<li></li>
@endforeach
</ul>
</div>
@endif
<input type="hidden" name="_token" value="">
<div class="row">
<div class="col-md-6">
<div class="form-group ">
<input type="text" id="title" name="title" class="form-control" placeholder="Enter Title" value="">
<span class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<button class="btn btn-success">Create New Item</button>
</div>
</div>
</div>
</form>
<div class="panel panel-primary">
<div class="panel-heading">Item management</div>
<div class="panel-body">
<form method="GET" action="">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" name="titlesearch" class="form-control" placeholder="Enter Title For Search" value="">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<button class="btn btn-success">Search</button>
</div>
</div>
</div>
</form>
<table class="table table-bordered">
<thead>
<th>Id</th>
<th>Title</th>
<th>Creation Date</th>
<th>Updated Date</th>
</thead>
<tbody>
@if($items->count())
@foreach($items as $key => $item)
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
@endforeach
@else
<tr>
<td colspan="4">There are no data.</td>
</tr>
@endif
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
OK, now we can see the effect of the code above, first start PHP server built in the project root directory:
php artisan serve
Then access items list page in the browser:
http://localhost:8000/items-lists
If you have added items to record the data table, then you can run the following command to add data to the index:
php artisan scout:import "AppItem"
This can be a search operation in the page.
For more Scout expansion pack usage details, refer to the corresponding Chinese document: http: //laravelacademy.org/post/6277.html
Disclaimer: This article is translated from finishing itsolutionstuff.com