Skip to content
Advertisement

Laravel how to properly pass data to href

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}

Webpage designed

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 AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;
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 IlluminateSupportFacadesRoute;
use AppHttpControllerssearchController;
use AppHttpControllersdaftar;
/*
|--------------------------------------------------------------------------
| 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', [AppHttpControllersHomeController::class, 'index'])->name('home');

Thank you

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement