laravelËÑË÷ɾ³ý
laravelËÑË÷ɾ³ýÊÇÒ»ÖַǾ³£¼ûµÄ¿ª·¢ÐèÇó£¬ÌØÊâÊÇÔÚ¿ª·¢ÄÚÈÝÖÎÀíϵͳ»òµç×ÓÉÌÎñÍøÕ¾µÈÐèÒª¶ÔÊý¾Ý¾ÙÐÐcrud²Ù×÷µÄÓ¦ÓÃÖС£laravel×÷Ϊһ¸öÊ¢ÐеÄphp¿ò¼Ü£¬ÌṩÁ˸»ºñµÄ¹¤¾ßºÍapi£¬Ê¹µÃÕâ¸öÀú³Ì±äµÃºÜÊǼòÆÓ¡£
Ò»¡¢LaravelËÑË÷
ÔÚLaravelÖÐʵÏÖËÑË÷¹¦Ð§Í¨³£ÐèÒªÒÔÏ°취:
×°ÖÃLaravel Scout
Laravel ScoutÊÊÓÃÓÚ¶ÔËÑË÷Ч¹û¾ÙÐзÖÀàºÍÓÅ»¯µÄÓ¦Óã¬Scout±ÈÕÕÔÉúµÄËÑË÷ÅÌÎÊËÙÂʸü¿ì£¬ÅÌÎÊ×é¼þ¸üÈÝÒ×±»Öع¹¡£Laravel ScoutʹÓÃElasticsearch»òAlgoliaÀ´Ö´ÐÐËÑË÷£¬ÕâЩËÑË÷ÒýÇæÊǺÜÊÇÊ¢Ðеģ¬²¢ÓµÓÐÆÕ±éµÄÉçÇøÖ§³Ö¡£
Êý¾Ý¿âǨáã
ÔÚLaravelÓ¦ÓÃÖн¨ÉèModel¡¢ControllerºÍÊý¾Ý¿âǨáãÎļþµÄ»ù´¡ÉÏ£¬ÎÒÃÇÐèÒª¾ÙÐÐËÑË÷Ïà¹ØµÄÊý¾Ý¿âǨáã²Ù×÷¡£
ÀýÈ磬ÔÚÓû§Êý¾Ý¿âÖÐÎÒÃÇ¿ÉÒÔÌí¼ÓÒ»¸ö¡°searchable¡±ÁУº
Schema::table('users', function (Blueprint $table) { $table->boolean('searchable')->default(false); });
µÇ¼ºó¸´ÖÆ
ÕâÀïÎÒÃÇ¿ÉÒÔ¸øsearchableÉèÖÃĬÈÏÖµfalse£¬¼´Óû§²»Ï£Íû±»ËÑË÷¡£Ò²¿ÉÒÔÉèÖÃÔÚÓû§ÐÅÏ¢±à¼ÍêÖ®ºó£¬½«searchableÉèÖÃΪtrue£¬Óû§¾Í¿ÉÒÔ±»ËÑË÷ÁË¡£
ʹÓÃScout±ê¼ÇÄ£×Ó
ʹÓÃLaravel Scoutʱ£¬ÎÒÃÇÐèÒªÔÚ¶ÔÓ¦µÄModelÖÐʹÓÃSearchable trait¡£
ÀýÈ磺
namespace App;
use IlluminateDatabaseEloquentModel;
use LaravelScoutSearchable;
class User extends Model
{
use Searchable; // ...
µÇ¼ºó¸´ÖÆ
}
Õâ¸öÀú³ÌÍê³Éºó£¬Ä£×ÓµÄsave()¡¢delete()¡¢restore()ºÍforceDelete()ÒªÌå»á×Ô¶¯ÓëËÑË÷ÒýÇæͬ²½¡£
ʹÓÃAlgolia»òElasticSearch
ÔÚAlgolia»òElasticSearchÖн¨ÉèË÷ÒýºÍÉèÖÃËÑË÷Ч¹ûÃûÌá£ÎªÁËÓÅ»¯ËÑË÷Ч¹û£¬ÎÒÃÇ¿ÉÒÔÔÚLaravelÖÐʹÓÃScoutµÄSearchable trait¡£
ÀýÈ磬ÔÚAlgoliaÖУº
namespace App;
use LaravelScoutSearchable;
use AlgoliaScoutExtendedSearchableAggregator;
class Note extends Model
{
use Searchable, Aggregator; public function toSearchableArray() { $array = $this->toArray(); // ÇëÇó×¢Èëʱ£¬ÎÒÃÇÐèÒª¸üÐÂÅÌÎÊÉý¼¶ºÍÏòÁ¿·ÖÅÉ $array = $this->seoInject Boosts($array, [ 'title' => 1000, 'content' => 100, 'body' => 10, ]); return $array; }
µÇ¼ºó¸´ÖÆ
}
¶þ¡¢LaravelËÑË÷ɾ³ý
ÔÚLaravelµÄCRUDÓ¦ÓÃÖУ¬É¾³ý²Ù×÷ÊǺÜÊÇÆÕ±éµÄ¡£ÔÚËÑË÷Ч¹ûÖоÙÐÐɾ³ý²Ù×÷ÊÇÒ»¸öÏà¶ÔÖØ´óµÄʹÃü£¬ÓÉÓÚÄúÐèÒª½«É¾³ýµÄÊý¾Ý×Ô¶¯´ÓËÑË÷ÒýÇæÖÐɾ³ý¡£ÕâÒâζ×Å£¬ÎÒÃÇÐèÒª¶ÔÊý¾Ý¿â¾ÙÐвÙ×÷¡£
ÒÔÏÂÊÇʵÏÖËÑË÷²¢É¾³ýÊý¾ÝµÄ´óÖÂÁ÷³Ì£º
1.ÔÚËÑË÷Ч¹ûÖÐѡժҪɾ³ýµÄÊý¾Ý¡£
2.½«ÕâЩÊý¾Ý±ê¼ÇΪ¡°É¾³ý¡±£¬¸üÐÂÊý¾Ý¿â£¬²¢´ÓËÑË÷ÒýÇæÖÐɾ³ýÕâЩÊý¾Ý¡£
namespace AppHttpControllers;
use AppNote;
use IlluminateHttpRequest;
class NoteController extends Controller
{
/** * ɾ³ýÒ»ÌõÌõ¼ÇÐÞÊηûÐÅÏ¢ÇëÇó * * @param int $id * @return IlluminateHttpResponse */ public function destroy($id) { $note = Note::find($id); if ($note) { // ´ÓÊý¾Ý¿âÖÐɾ³ý $note->delete(); // ´ÓËÑË÷ÒýÇæÖÐɾ³ý $note->unsearchable(); } return redirect() ->route('notes.index') ->with('success', 'Note deleted successfully.'); }
µÇ¼ºó¸´ÖÆ
}
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇ´ÓÊý¾Ý¿âÖÐɾ³ýÁËÌõ¼Ç²¢´ÓËÑË÷ÒýÇæÖÐɾ³ý¡£½ÓÏÂÀ´£¬ÎÒÃÇ»áÌÖÂÛÔõÑùÔÚËÑË÷Ч¹ûÖÐʵÏÖÅúÁ¿É¾³ý²Ù×÷¡£
Èý¡¢ÊµÏÖÅúÁ¿É¾³ý²Ù×÷
ÔÚ½«¶à¸öЧ¹ûɾ³ý֮ǰ£¬ÎÒÃÇÐèÒªÔÚÇ°¶Ë¿ª·¢Ò»¸ö¶àÑ¡¿òºÍɾ³ý°´Å¥¡£È»ºó£¬ÎÒÃÇÐèÒªÔÚ¿ØÖÆÆ÷ÖÐʵÏÖÅúÁ¿É¾³ý²Ù×÷¡£Õⲿ·Ö´úÂë°üÀ¨£º
»ñÈ¡ËùÓÐҪɾ³ýµÄÌõ¼Ç¡£
ÔÚÊý¾Ý¿âÖбê¼ÇÕâЩÌõ¼ÇΪ¡°É¾³ý¡±¡£
´ÓËÑË÷ÒýÇæÖÐɾ³ýÕâЩÌõ¼Ç¡£
Öض¨Ïòµ½ÔÀ´µÄ·ÓÉ¡£
ÀýÈ磬ÔÚNoteController.phpÎļþÖУ¬ÊµÏÖÅúÁ¿É¾³ý²Ù×÷£º
/** * ɾ³ý¶àÌõÌõ¼Ç±¬·¢ÇëÇó * * @param Request $request * @return IlluminateHttpResponse */ public function multiDestroy(Request $request) { $notesId = $request->ids; foreach ($notesId as $noteId) { $note = Note::find($noteId); if ($note) { $note->delete(); $note->unsearchable(); } } return redirect() ->route('notes.index') ->with('success', 'Notes were deleted successfully.'); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃDZéÀúÁËËùÓÐҪɾ³ýµÄÌõ¼Ç¹¤¾ß£¬²¢ÔÚÊý¾Ý¿âºÍËÑË÷ÒýÇæÖн«ËüÃDZê¼ÇΪɾ³ý״̬¡£Õâ¸öÀú³ÌºÍµ¥É¾ºÜÀàËÆ¡£
ËÄ¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùÔÚLaravelÓ¦ÓóÌÐòÖÐʵÏÖËÑË÷ɾ³ý£¬º¸ÇÁËLaravel¿ò¼ÜºÍScoutÄ£¿éµÄ»ù±¾Ó÷¨ºÍ¿ØÖÆÆ÷´¦ÀíÒªÁì¡£¿ÉÒÔͨ¹ýÕâ¸öʾÀýÔÚ×Ô¼ºµÄLaravelÓ¦ÓóÌÐòÖÐʵÏÖÏàͬµÄ¹¦Ð§£¬ÎªÄú¿ª·¢Ô½·¢Àû±ã¡£
ÒÔÉϾÍÊÇlaravelËÑË÷ɾ³ýµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡