In a laravel project I have this migration:
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::dropIfExists('posts');
Schema::create('posts',function (Blueprint $table){
// More columns will be added in next migrations.
$table->id();
$table->string('name');
$table->fulltext(['posts']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};
And this Model:
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use AppModelPost;
class Post extends Model
{
use HasFactory;
protected $table="posts";
}
And I search as fulltext in a controller:
class PostController extends Controller
{
public function search()
{
$searchterm = $request->get('searchterm');
$qb = Post::query();
if(!empty($searchterm)){
$request->whereFullText(['name'],$searchterm);
}
$page = $request->get('page')??1;
$limit = $request->get('limit')??20;
$results = $request->offset(($page - 1) * $limit)
->simplePaginate($limit);
return new JsonResponse($results,200);
}
}
But how upon my controller can I return the results order by relevancy? I want the closest matching to be the first ones.