Searcing is essential for every table or blog post or any content you display to the user. Searching makes it easy to find the particular records from the database table that match the keyword string.
Let me take the best example to help you understand laravel searching better.
1. First create a new laravel project.
composer create-project laravel/laravel
cd laravel
composer install
2. Database Configuration
After successfully installing the laravel Application, Go to your project .env file and set up database credentials and move next step :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="your database name here"
DB_USERNAME="database username here"
DB_PASSWORD="database password here"
3. Creating blog project models
Here we are taking a blog post example that having multiple tags & category so you can search from them.
php artisan make:model Post -m
php artisan make:model PostCategory -m
php artisan make:model PostTag -m
php artisan make:model PostAttribute -m
4. Migrateing the tables into database
You will find these migration files in database > migration folder. and change their content as written below.
create_posts_table.php
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title',255)->nullable();
$table->string('slug',255)->unique()->nullable();
$table->text('content')->nullable();
$table->boolean('status')->default(false);
$table->timestamps();
});
}
create_post_categories_table.php
public function up()
{
Schema::create('post_categories', function (Blueprint $table) {
$table->id();
$table->string('name',255)->nullable();
$table->timestamps();
});
}
create_post_tags_table.php
public function up()
{
Schema::create('post_tags', function (Blueprint $table) {
$table->id();
$table->string('name',255)->nullable();
$table->timestamps();
});
}
create_post_attributes_table.php
public function up()
{
Schema::create('post_attributes', function (Blueprint $table) {
$table->id();
$table->enum('type',['category','tag'])->default('category');
$table->bigInteger('table_id')->unsigned();
$table->bigInteger('post_id')->unsigned();
$table->timestamps();
});
}
Migrate these tables by running the following command
php artisan migrate
5. Make relationship for your post model
Go to the app/model/post.php and add the following code into your file
public function categories():HasManyThrough
{
return $this->hasManyThrough(PostCategory::class,PostAttribute::class,'post_id','id','id','table_id')->where('type','category');
}
public function tags():HasManyThrough
{
return $this->hasManyThrough(PostTag::class,PostAttribute::class,'post_id','id','id','table_id')->where('type','tag');
}
6. Searching keyword in multiple relationships query
Make a controller file IndexController.php
php artisan make:controller IndexController
Add the following search function into app/http/controllers/IndexController.php
IndexController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class IndexController extends Controller
{
public $search;
public function Search(Request $request)
{
if($request->has('seach'))
{
$this->search = $request->get('seach');
$posts = Post::where('status',1)
->where(function ($q){
$q->orWhere('title','LIKE',"%{$this->search}%")
->orWhere(function ($qry){
$qry->whereHas('categories',function ($qc){
$qc->where('post_categories.name',$this->search);
});
})
->orWhere(function ($qry){
$qry->whereHas('tags',function ($qt){
$qt->where('post_tags.name',$this->search);
});
});
})->paginate(10);
$posts->append('seach');
}
else
{
$posts = Post::where('status',1)->paginate(10);
}
return view('welcome',compact('posts'));
}
}