Steven Maguire
I've been building web software since 2004.
select * from table where column like '%keyword%';
SELECT * FROM table WHERE
(
column_one LIKE '%keyword%'
OR column_two LIKE '%keyword%'
);
laravel.com/docs/5.3/scout
$ composer create-project --prefer-dist laravel/laravel mascots
...
$ cd mascots
New Laravel Project
$ php artisan make:model Mascot --migration
Model created successfully.
Created Migration: 2016_09_25_181801_create_mascots_table
New Laravel Model
<?php
//...
public function up()
{
Schema::create('mascots', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('image_url');
$table->string('domain');
$table->text('description');
$table->integer('popularity');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('mascots');
}
Define Migration
database/migrations/YYYY_MM_DD_HHMMSS_create_mascots_table.php
$ php artisan migrate
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrated: 2016_09_25_181801_create_mascots_table
Run Migration
<?php
//...
Route::resource('mascots', 'MascotController');
Create Resource Route
routes/api.php
$ php artisan make:controller MascotController --resource
Controller created successfully.
Create Resource Controller
laravel.com/docs/5.3/scout
<?php
//...
'providers' => [
//...
Laravel\Scout\ScoutServiceProvider::class,
//...
];
Include Scout Service Provider
config/app.php
$ composer require laravel/scout
Include Scout Package
<?php
namespace App;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
class Mascot extends Model
{
use Searchable;
}
Add Searchable Trait to Model
app/Mascot.php
$ php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Publish Scout Config
<?php
return [
//...
'driver' => env('SCOUT_DRIVER', 'algolia'),
//...
'queue' => false,
//...
'algolia' => [
'id' => env('ALGOLIA_APP_ID', ''),
'secret' => env('ALGOLIA_SECRET', ''),
],
];
Update Configuration
config/scout.php
$ composer require algolia/algoliasearch-client-php
Include Algolia Package
ALGOLIA_APP_ID=XXXXXXXXXX
ALGOLIA_SECRET=XXXXXXXXXX
Include Algolia API Keys
.env
$ php artisan scout:import "App\Mascot"
<?php
$mascot = new App\Mascot;
// ...
$mascot->save();
<?php
// Adding via Eloquent query...
App\Mascot::where('popularity', '>', 10)->searchable();
// You may also add records via relationships...
$actor->mascots()->searchable();
// You may also add records via collections...
$mascots->searchable();
<?php
$mascot = App\Mascot::find(1);
// ...
$mascot->save();
<?php
$mascot = App\Mascot::find(1);
// ...
$mascot->delete();
<?php
// Removing via Eloquent query...
App\Mascot::where('popularity', '>', 10)->unsearchable();
// You may also remove via relationships...
$actor->mascots()->unsearchable();
// You may also remove via collections...
$mascots->unsearchable();
<?php
App\Mascot::withoutSyncingToSearch(function () {
// Perform model actions without fear
// of syncing data with third-party
// search service
});
<?php
$mascots = App\Mascot::search('cereal')->get();
Performs search against third-party search service, then queries database for all models associated with results; returns Collection.
local.INFO: select * from `mascots` where `id` in ('2', '1')
<?php
$mascots = App\Mascot::search('cereal')
->where('popularity', 10)
->get();
Currently, these clauses only support basic numeric equality checks, and are primarily useful for scoping search queries by a tenant ID. Since a search index is not a relational database, more advanced "where" clauses are not currently supported.
<?php
$mascots = App\Mascot::search('cereal')->paginate();
// Specific per page
$mascots = App\Mascot::search('cereal')->paginate(10);
$ composer require laravel/passport
...
$ php artisan migrate
Migrated: 2016_06_01_000001_create_oauth_auth_codes_table
Migrated: 2016_06_01_000002_create_oauth_access_tokens_table
Migrated: 2016_06_01_000003_create_oauth_refresh_tokens_table
Migrated: 2016_06_01_000004_create_oauth_clients_table
Migrated: 2016_06_01_000005_create_oauth_personal_access_clients_table
$ php artisan passport:install
Encryption keys generated successfully.
Personal access client created successfully.
Password grant client created successfully.
Include Passport
laravel.com/docs/5.3/passport
<?php
return [
//...
'queue' => true,
//...
];
config/scout.php
laravel.com/docs/5.3/scout#custom-engines
@stevenmaguire
on twitter
stevenmaguire@gmail.com
on electronic mail
stevenmaguire
on github
By Steven Maguire
Deck originally created for a presentation to a gathering of the Chicago Laravel Meetup group - https://vcr.bz/laravel-scout