Searching and saving sequence of profile data - crud of profiles

Step 01: Adding following changes in list.blade.php under resources/view/profiles for search form

// search form 
<div class="row">
	<div class="col-md-4">
		<h2 class="">All Profiles</h2>
	</div>
	<div class="col-md-4">
		<div class="">
			<form method="GET" action="{{ route('profiles.index' ) }}">
				<input type="text" name="search_global" class="form-control" placeholder="Filter by name" value="{{ request('search_global', '') }}">
			</form>
		  </div>                    
	</div>
	<div class="col-md-4 text-end pt-1">
		<a class="btn btn-sm btn-primary" href="{{ route('products.create') }}"> Add Profile</a>
	</div>             
</div>

Step 02: Then, back in ProfileController following changes in index() method for searching

// searching 
$query = Profile::query();
$keyword = $request->query( 'search_global' ) ?? '';       

// eloquent conditional clause
$query->when( $keyword , function ($query) use ($keyword) {
	$query->where( 'name', 'like' , '%'.$keyword.'%' );
}); 

So, updated full index() method will be like below

public function index( Request $request ) {
	$meta_title = 'All Profiles';    
	
	$query = Profile::query();
	$keyword = $request->query( 'search_global' ) ?? '';       
	
	// eloquent conditional clause
	$query->when( $keyword , function ($query) use ($keyword) {
		$query->where( 'name', 'like' , '%'.$keyword.'%' );
	});  
	
	$profiles = $query->orderBy( 'sequence', 'desc' )->paginate( 10 )->withQueryString();   
	return view( 'profiles.list', compact( 'meta_title','profiles' ) );
} 

Step 03: Saving sequence of profile data by adding following method. Please note this is response method of ajax call.

function saveOrder ( Profile $profile, Request $request  ) { 
	$profile->sequence = $request->post( 'order' );
	$profile->save();
	return response()->json($profile->sequence,200);
}

Related Posts


Building mini ecommerce in Laravel

Listing rows of users - crud

Adding user data - crud of users

Editing user data - crud of users

Deleting user data - crud of users

Listing rows of products - crud

Listing rows of profiles - crud

Listing rows of orders - crud

Listing rows of order items - crud