1

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

2 Answers2

1

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.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • Thanks for answering, the code work, but do you have any idea why the output url show http://127.0.0.1:8000/search/%201 when the actual number is 1 and not 201? Thanks again – Amir Shouqqie Dec 03 '21 at 08:04
  • Do you have any idea how I can recall this variable passed in the url to the next page that it will redirect to? – Amir Shouqqie Dec 03 '21 at 08:46
  • @AmirShouqqie Here, too, there are several possibilities. You can: 1) pass the varaible as a query parameter, 2) store it in the cookie or localStorage at the client (JS Solution). With the query parameter, you then have access with the Laravel request Object (Request $request). – Maik Lowrey Dec 03 '21 at 08:52
0

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.

DerickMasai
  • 93
  • 1
  • 10