How i search data in different tables in laravel?

how to fetch data from multiple table in laravel
fetch data from two tables in laravel eloquent
how to display data from two tables in laravel
eloquent search multiple tables
laravel search multiple models
laravel: select from multiple tables
laravel multiple table relationship
laravel search query with multiple conditions

I am new to laravel i am trying to search data in my tables inside database.

This is my route code

Route::get('/search','StudentController@search');
Route::get('/search','ProfessorController@search');

This is the search function code for professor

  public function search(Request $request)
    {   
        $search = $request->get('search');
        $professors = DB::table('professors')->where('name', 'like','%'.$search.'%')->paginate(4);
        return view('professor.index', ['professors' => $professors]);  
    }

and this is the search function code for students

 public function search(Request $request)
    {   
        $search = $request->get('search');
        $students = DB::table('students')->where('lastname', 'like','%'.$search.'%')->paginate(4);
        return view('student.index', ['students' => $students]);  
    }

This is the view code for professor

        <form action="/search" method="GET">
                   <div class="input-group">
                      <input type="search" name="search" class="form-control">
                        <span class="input-group-prepend">
                        <button type="submit" class="btn btn-primary bouton">Search</button>
                        </span>
                   </div>
       </form>

and this is for students

 <form action="/search" method="GET">
             <div class="input-group">
               <input type="search" name="search" class="form-control">
               <span class="input-group-prepend">
           <button type="submit" class="btn btn-primary bouton">Search</button>
               </span>
            </div>
 </form>

Now! the problem that i am having is that when i try to search data in my students view it takes me to the professors view and when i switch places in route it always execute the lats route !!

I really how that my issue is clear and i would be very happy if someone helps me !

If you want dynamic result use this approach so that you don't need to create new routes and method every time. Just pass the extra parameter.

Change your routes to

Route::get('/search/{model?}','StudentController@search');

and forms to

<form action="/search/students" method="GET">
<form action="/search/professors" method="GET">

and in controller

$result = DB::table($request->model)->where('name', 'like','%'.$search.'%')->paginate(4);

How to display the data from different tables in Laravel, With laravel you have many options. for simple and medium queries, setup the relationships and use the ->with() method to grab data from multiple models. Now you can use the car table as well. How can I create live search in Laravel? For retrieve data from MySQL database using laravel framework first we have to create a table in data base. After create a table in the MySQL database you need to insert record or data on it.If you want to know how to insert data in laravel framework please visit the link : Insert data in Laravel .

They are the same to Laravel. How would it know which one do you want. Slight modification would do.

Route::get('/search-students','StudentController@search');
Route::get('/search-professors','ProfessorController@search');

Search multiple tables for a search term, Hi everyone, I have searched and can't find the answer anywhere. I have three tables (user, tracks, album). I want to search for a single  Edit: My use of "single query" wasn't the right word to describe my problem. I'm aware that Eloquent runs query for each related tables and then joins them as result and this is okay. My problem is retrieving data from different tables with eager loading, then merging result as single Collection.

Your route should be diffrent with each other routes.

For student

Route::get('student/search','StudentController@search')->name('student.search');

<form action="{{ route('student.search')}}" method="GET">
</form>

For professor

Route::get('professor/search','ProfessorController@search')->name('professor.search');
<form action="{{ route('professor.search')}}" method="GET">
</form>

How can I search multiple tables in laravel 5, Forum > How can I search multiple tables in laravel 5. idesignedme posted 4 years ago. Database Eloquent. I am able to search any column in one table at a  Retrieve data from database using Laravel, Ajax - Learn Retrieve data from database using Laravel and Ajax with complete source code and demo.

You just need if statement like this:

 public function index()
 {
    $search = request()->query('search');

    if ($search) {
        $posts = Post::where('title', 'LIKE', '%'.$search.'%')->simplePaginate(1);
    } else {
        $posts = Post::simplePaginate(3);
    }
    return view('welcome')
        ->with('categories', Category::all())
        ->with('tags', Tag::all())
        ->with('posts', $posts);
    }

so, in your code it could be like this:

 public function search(Request $request)
{   
    $search = $request->get('search');
    if($search){
    $professors = DB::table('professors')->where('name', 
    'like','%'.$search.'%')->paginate(4);
    } else { 
    $professors = Professor::all(); 
    }
     return view('professor.index', ['professors' => 
     $professors]); 


}

New Package Laravel-Searchable: Easily Search in Multiple Models , Also, seeded some data for both tables, used make:auth to generate a simple Bootstrap template, and ended up with this list of companies:. Search functionality implementation in Laravel. A simple laravel app to demonstrate how to implement a search feature to your laravel applications.

Database: Query Builder - Laravel, If you just need to retrieve a single row from the database table, you may use the first method. You may combine these methods with other clauses: Laravel Join 3 Tables: Joining more than two tables is very common in any framework, we often need to join 3 or more then three table in Laravel.Here in this tutorial we are going to explain how you can join three tables or more than three tables in Laravel.

Query Builder - Laravel, The database query builder provides a convenient, fluent interface to creating and DB::table('users')->chunk(100, function($users) { foreach ($users as $user) { // } }); Using Where Null To Find Records With Unset Values Instead of comparing two columns, these methods will compare the column against a value: If you have ever had to tell a teammate to manually add a column to their local database schema, you've faced the problem that database migrations solve. The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel's supported database systems. Generating Migrations

[Eloquent] How to retrieve data from multiple tables with different , My problem is retrieving data from different tables with eager loading, then merging Laravel Eloquent when() method is crazy, say goodbye to if-else. no idea how to make video live? i already tried search some keyword but still have no  But if we want to required search filter for only one specific table column with select box then we should have to implement custom search filter of data in our Datatable. Below you can find step by step process for how to make custom search filter in Laravel 5.8 Datatable by using yajra datatables package and Ajax jquery.

Comments
  • thanks for your time and suggestion works for me
  • that works like a treat mate thanks a lot brother