Database yang akan kita gunakan adalah dborang dari tutorial Membuat Pagination di Laravel 5.

Membuat Route

edit file app/Http/routes.php tambahkan dibagian paling bawah

Route::get('cari','CariController@index');
Route::get('hasil', 'CariController@getName');

Membuat Model

buat model baru dengan nama Orang.php

<?php namespace App;
 
use Illuminate\Database\Eloquent\Model;
 
class Orang extends Model {
 
	protected $table = 'dborang';
 
}

Membuat Controller

buat file controller dengan nama CariController.php

<?php namespace App\Http\Controllers;
 
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Cari;
use Illuminate\Http\Request;
use Input;
 
class CariController extends Controller {
 
	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
 
	return view('search');
 
	}
	public function getName()
	{
		$input = Input::get('cari');
		$name = \App\Orang::where('Name', 'LIKE', '%'.$input.'%')->get();
		return view('result', compact('name'));
	}
}

Query yang dijalankan diatas sama dengan select * from `dborang` where `Name` LIKE '%$input%'.

Membuat View

Untuk view disini dibagi menjadi 2, pertama form pencarian yang di beri nama search.blade.php

<!DOCTYPE html>
<html lang="">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Pencarian dengan Laravel 5 &raquo; Jaranguda.com</title>
		<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
	</head>
	<body>
		<div class="container">
			<h3>Pencarian</h3>
			<hr>
			<form action="{{ url()}}/hasil" action="GET">
				<div class="form-group">
					<label for="cari">data yang dicari</label>
					<input type="text" class="form-control" id="cari" name="cari" placeholder="cari">
				</div>
				<input class="btn btn-primary" type="submit" value="Update data">
			</form>
		</div>
	</body>
</html>

dan view untuk menampilkan hasil pencarian result.blade.php

<!DOCTYPE html>
<html lang="">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>Pencarian dengan Laravel 5 &raquo; Jaranguda.com</title>
		<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
	</head>
	<body>
 
	<div class="container">
	<h3>Hasil Pencarian</h3>
	<hr>
 
		@if (count($name) > 0)
			@foreach ($name as $data)
				{{ $data->Name }} => {{ $data->Country }} <br>
			@endforeach
		@else 
		Data tidak ditemukan.
		@endif
 
	</div>
 
	</body>
</html>

Screenshot aplikasi

script searching laravel

Leave a comment

Your email address will not be published. Required fields are marked *