Laravel

Laravel: How to Pass Search Conditions as Parameters to Pagination on the List Page

In this article, we will introduce how to pass search conditions as parameters to pagination (Paginator) links on a Laravel search results list page and keep the search conditions while paginating.
(Laravel version 6)

Three Table Structures

The following three tables are prepared. From the listpages table, the users who registered the product are stored in the users table (child). The todofuken_id registered in the users table matches the id in the todofukens table (grandchild), and displays the prefecture name (prefecture).

listpages table (Parent)

Name Description
id Primary key
product_name Product name
user_id Product registration user ID = id of users table
※ You must use the name “user_id”, which is created by removing the “s” from the users table name (“user”) and appending an underscore “_” to the primary key “id” of the users table. By doing so, you can retrieve data where the id matches in the users table.

users table (Child)

Name Description
id Primary key
name User name
email Email address
todofuken_id Prefecture ID = id of todofukens table
※ You must use the name “todofuken_id”, which is created by removing the “s” from the todofukens table name (“todofuken”) and appending an underscore “_” to the primary key “id” of the todofukens table. By doing so, you can retrieve data where the id matches in the todofukens table.

todofukens table (Grandchild)

Name Description
id Primary key
prefecture Prefecture name

Model Description: Passing Search Conditions as Parameters to Pagination in Laravel

※ Write code to retrieve data from the Listpage model, User model, and Todofuken model.

class Listpage extends Model
{
	public function user()
	{
		//Retrieve data from User model
	    return $this->belongsTo('App\User');
	}
	public function todofukens()
	{
		//Retrieve data from Todofuken model
	    return $this->belongsTo('App\Todofuken');
	}
}

Controller Description: Passing Search Conditions as Parameters to Pagination in Laravel

※ Retrieve id and name from the users table where user_id in the listpages table matches, retrieve id and prefecture from the todofukens table where todofuken_id in the users table matches. As a search condition, perform a LIKE search on product_name in the listpages table.

namespace App\Http\Controllers;
use Illuminate\Http\Request;

use App\User;   //Use User model
use App\Todofuken;   //Use Todofuken model
use App\Listpage;   //Use Listpage model


    public function listdata(Request $request)
    {
        $sort = 'listpages.id';
        $order = 'desc';

        $sql = '1=1';

        $product_name = "";
        if(!empty($request->product_name)){
            $product_name = $request->product_name;
            $sql .= " AND listpages.product_name like '%".$request->product_name."%'";
        }

	//Retrieve id and name from users table where user_id in listpages table matches. Retrieve id and prefecture from todofukens table where todofuken_id in users table matches. As a search condition, perform a LIKE search on product_name in listpages table.
        $listpages = Listpage::with(['user:id,name','user.todofuken:id,prefecture'])
        ->join('users','users.id','=','listpages.user_id')
        ->select('listpages.*','users.id as uid')
        ->whereRaw($sql)
        ->orderBy($sort, $order)->paginate(20);

	//Pass the search condition product_name from listpages table as param to the view.
        return view('listpages', [
            'listpages' => $listpages,
            'product_name' => $product_name,
            'param' => [
                'product_name' => $product_name,
            ],
        ]);
    }

View (List Page) Description: Passing Search Conditions as Parameters to Pagination in Laravel

※ If you only display pagination it will be {{ listpage->links() }}, but if you want to pass (keep) search conditions (product name: product_name) as parameters to pagination, you can use {{ listpage->appends($param)->links() }}. By passing the search condition as a parameter ($param) to appends(), pagination will be performed while maintaining the search condition.

		<form enctype="multipart/form-data" action="{{ url('listpages') }}" method="GET">
	            	@csrf
			<label for="product_name">Product Name</label>
			<input type="text" name="product_name" value="{{ old('product_name', $product_name) }}">
			<br>
			<button type="submit">Search</button>
        	</form>

                <div align="center">
                    {{ $listpage->appends($param)->links()}}
                </div>

                <table>
                    <thead>
                        <th>Product Name</th>
                        <th>Registered User</th>
                        <th>Prefecture</th>
                    </thead>
                    <tbody>
                        @foreach ($listpages as $listpage)
                            <tr>
                                <!-- Product Name -->
                                <td class="table-text">
                                    <div>{{ $listpage->product_name }}</div>
                                </td>
                                <!-- Registered User -->
                                <td class="table-text">
                                    <div>{{ $listpage->user->name }}</div>
                                </td>
                                <!-- Prefecture -->
                                <td class="table-text">
                                    <div>{{ $listpage->user->todofuken->prefecture }}</div>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>

Route Description

※ Change the page URL as needed.

Route::get('/listpages', 'ListpagesController@listdata');

 
※ Please use at your own risk if reusing.