對不起,如果問題是新手。我是 php 和 laravel 的新手,仍在嘗試通過教程學習。
我試圖將資料庫中的“否”傳遞給 url,這樣當我點擊 Daftar 時,url 就會顯示
http://127.0.0.1:8000/search/{否}
網頁設計
我確實嘗試將它放在我的 href 標簽中,但沒有得到我想要的結果
這是我的代碼
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>
搜索控制器.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');
}
}
}
網頁.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');
謝謝
uj5u.com熱心網友回復:
您有多種方法可以做到這一點。在我看來,最簡單的方法是
<a href="search/ {{ $nama->No] }}">DAFTAR</a>.
事實上,你已經做了什么。僅使用 Bladesyntax。你的例子中有一個小錯誤。即,您的雙引號。 <a href="search/".$nama[No]>DAFTAR</a>應該:
<a href="search/<?php echo $nama[No] ?>">DAFTAR</a>或者更好<a href="search/ {{ $nama->No] }}">DAFTAR</a>。
為了完整性。最優雅的方式是使用組件。
uj5u.com熱心網友回復:
嘗試這個
<td>
<a href="{{ route('web.search', ['No'=>*given_variable_here*]) }}">DAFTAR</a>
</td>
在上面的given_variable_here輸入變數。
此外,您沒有準備好接受web.php. 這可以像這樣更正:
Route::get('/search/{No}',[searchController::class, 'search'])->name('web.search');
最后,我不太確定將No要使用的 'N' 大寫。如果您遇到問題,請先將它們以小寫形式放置。如果您使用 VS Code,請確保添加擴展名Laravel Extra Intellisense,Laravel Blade Snippets和Laravel Snippets. 他們是一個很大的幫助。如果這有幫助,請告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372552.html
上一篇:洗掉Laravel中的渲染視圖
