laravel search multiple tables Example

Today, We want to share with you laravel search multiple tables Example.In this post we will show you laravel select from multiple tables, hear for get data from multiple tables in laravel 5.7 we will give you demo and example for implement.In this post, we will learn about fetch data from two tables in laravel 5.7 eloquent with an example.

laravel search multiple tables Example

There are the Following The simple About laravel search multiple tables Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 5.7 eloquent search multiple tables, so the some laravel search join tables for this example is following below.

Searching multiple tables with one query with Laravel

The Sample data is stored in tables as such:

Teachers Categories
teacherid_ studentid_
teacher_name student_name
teacher_student * student_slug

The Laravel Problem

Here’s some Laravel basic Relationship context to the issue. We have two models (‘Teacher’, and ‘Student’). They are joined Laravel Queries on a one-to-many relationship:


class Teacher {

  public function student() {
    return $this->belongsTo('Student', 'teacher_student');


class Student {

  public function Teacher() {
    return $this->hasMany('Teacher', 'student_id');

Laravel 5.7 Search multiple tables for a search term

$teachers = Teacher::whereHas('student', function($query) use($term) {
    $query->where('student_name', 'like', '%'.$term.'%');
})->orWhere('teacher_name','LIKE','%'.$term.'%')->orderBy($order, 'asc')->get();

we search for all teachers that have a related student.This is using the ‘student()’ function that defined earlier in the Teacher Model.

Teacher::whereHas('student', function($query) use($term) {
    $query->where('student_name', 'like', '%'.$term.'%');

If this MySQL query returns no results, after that we fall back to the simple Call the second mysql query, where we query the teacher directly, here some data checking if the teacher name is like the search query

Also Read This ๐Ÿ‘‰   Laravel 6 Joins Example Tutorial


Angular 6 CRUD Operations Application Tutorials

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel search multiple tables Example.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.