sorry if the question is kind of newbie. I am new to php and laravel, still trying to learn through tutorial.
I am trying to pass the 'No' in my database to the url, so that the url when I clicked on Daftar, it will show
http://127.0.0.1:8000/search/{No}
I did try to put it this way in my href tag but did not manage to get the result I want
here is my code
search.blade.php
@if(isset($namelist))
<table class="table table-hover">
<thread>
<tr>
<th>No</th>
<th>Nama</th>
<th>ID</th>
<th>Tindakan</th>
</tr>
</thread>
<tbody>
@if(count($namelist) > 0)
@foreach($namelist as $nama)
<tr>
<td>{{ $nama->No }}</td>
<td>{{ $nama->Name }}</td>
<td>{{ $nama->ID }}</td>
<td>
<a href="search/".$nama[No]>DAFTAR</a>
</td>
</tr>
@endforeach
@else
<tr><td>Tiada rekod ditemui, sila daftar secara manual di kaunter pendaftaran</td></tr>
@endif
</tbody>
</table>
@endif
</div>
</div>
</div>
</body>
</html>
searchController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class searchController extends Controller
{
function search(request $request){
if(isset($_GET['query'])){
$search_text = $_GET['query'];
$namelist = DB::table('namelist')-> where ('ID','LIKE','%'.$search_text.'%')->paginate(100);
return view('search',['namelist'=>$namelist]);
}
elseif(isset($_GET['query'])){
$search_text1 = $_GET['query'];
$namelist = DB::table('namelist')-> where ('No','LIKE','%'.$search_text1.'%')->paginate(100);
return view('search',['namelist'=>$namelist1]);
}
else{
return view('search');
}
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\searchController;
use App\Http\Controllers\daftar;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
route::get('/search',[searchController::class, 'search'])->name('web.search');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Thank you
CodePudding user response:
You have multiple ways to do that. In my opinion, the simplest way would be
<a href="search/ {{ $nama->No] }}">DAFTAR</a>
.
Actually, what you have already done. Only with the Bladesyntax. And there is a small mistake in your example. Namely, your double quotes. <a href="search/".$nama[No]>DAFTAR</a>
should be:
<a href="search/<?php echo $nama[No] ?>">DAFTAR</a>
or better <a href="search/ {{ $nama->No] }}">DAFTAR</a>
.
For the sake of completeness. the most elegant way would be to work with components.
CodePudding user response:
Try this
<td>
<a href="{{ route('web.search', ['No'=>*given_variable_here*]) }}">DAFTAR</a>
</td>
Enter the variable at given_variable_here above.
Also, you did not prepare the route to accept the passed variable in your web.php
. This can be corrected like this:
Route::get('/search/{No}',[searchController::class, 'search'])->name('web.search');
Lastly, I'm not too sure about capitalizing the 'N' in the No
you want to use. Should you have problems, start by placing these in lowercase. And if you're using VS Code make sure to add the extensions Laravel Extra Intellisense
, Laravel Blade Snippets
and Laravel Snippets
. They are a great help. Let me know if this helps.