-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic Live Search functionality with AJAX.
User can now have suggestions from a search box (input element). To Do: Have ability to search multiple types of data. Currently, only student data could be retrieved. Maybe tinker a little with the JS function and HTML elements to achive that.
- Loading branch information
1 parent
8804cd1
commit c167fbc
Showing
23 changed files
with
31,493 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\LiveSearch; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Classroom; | ||
use App\Models\ClassroomStudent; | ||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class LiveSearchController extends Controller | ||
{ | ||
//Note: Improve the API by adding authentication | ||
public function search(Request $request){ | ||
if(isset($request->data) & isset($request->searchFor)){ | ||
/** | ||
* Token fetched from request header. The token is store in the HTML meta tag then passed to the | ||
* request header. This token is fetched from the | ||
*/ | ||
$headerToken = $request->header('API-TOKEN'); | ||
/** | ||
* Check for API token in database | ||
*/ | ||
$tokenCount = User::where('api_token', $headerToken)->count(); | ||
/** | ||
* Check if there's specified token in database. | ||
*/ | ||
if($tokenCount > 0){ | ||
if(!empty($request->data)){ | ||
if($request->searchFor == 'user'){ | ||
$response = User::where('username', 'LIKE', "%{$request->data}%") | ||
->orWhere('fullname', 'LIKE', "%{$request->data}%") | ||
->orWhere('email', 'LIKE', "%{$request->data}%") | ||
->select('username', 'fullname', 'email')->get(); | ||
header('Content-Type: application/json'); | ||
return response()->json($response); | ||
} | ||
if($request->searchFor == 'student'){ | ||
// Only user with the student role is retrieved | ||
$response = User::select('username', 'fullname', 'email') | ||
->where('role', 'student') | ||
->where('username', 'LIKE', "%{$request->data}%") | ||
->where('fullname', 'LIKE', "%{$request->data}%") | ||
->where('email', 'LIKE', "%{$request->data}%")->get(); | ||
header('Content-Type: application/json'); | ||
return response()->json($response); | ||
} | ||
if($request->searchFor == 'classroom'){ | ||
$response = Classroom::where('id', 'LIKE', "%{$request->data}%") | ||
->orWhere('programs_code', 'LIKE', "%{$request->data}%") | ||
->orWhere('admission_year', 'LIKE', "%{$request->data}%") | ||
->orWhere('study_year', 'LIKE', "%{$request->data}%") | ||
->orWhere('study_levels_code', 'LIKE', "%{$request->data}%") | ||
->select('id', 'programs_code', 'admission_year', 'study_year', 'study_levels_code')->get(); | ||
header('Content-Type: application/json'); | ||
return response()->json($response); | ||
} | ||
}else{ | ||
$error = ['Error: Query is empty!']; | ||
return response()->json($error); | ||
} | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
database/migrations/2020_12_23_105817_adds_api_token_to_users_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class AddsApiTokenToUsersTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->string('api_token', 60)->unique()->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->dropColumn(['api_token']); | ||
}); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.