Frid Ric
Frid Ric

Reputation: 69

How to pass a variable from a link to another view in laravel

When a I click to a link, I want to pass $row->id to another view.

Here it is my link:

foreach($artisti as $row) {
                $artists .= '
                    <a href="{{ url(\'result/artista/\'.$row->id.\'/\') }}" class="dropdown-item">'.$row->nome.'</a>
                    ';
            }

Here it is my controller:

public function result($id)
{
    return view('result')->with('id', $id);
}

Here it is my web.php:

Route::get('/result/artista/{id}', 'LiveSearch@result')->name('artista');

Here it is the view I want to send the variable:

@extends('layouts.dashboard')

@section('content')

<h1>{{$id}}</h1>

@endsection

Upvotes: 1

Views: 516

Answers (2)

Akhtar Munir
Akhtar Munir

Reputation: 1769

Make your link in this way. Check that and let me know

foreach($artisti as $row) {
            $artists .= '
                <a href="'.{{ route('artista',$row->id) }}.'" class="dropdown-item">'.$row->nome.'</a>
                ';
        }

If above doesn't work then do this way

foreach($artisti as $row) {
            $artists .= '
                <a href="'.route('artista', ['id' => $row->id]).'" class="dropdown-item">'.$row->nome.'</a>
                ';
        }

Upvotes: 1

void
void

Reputation: 913

try this in your blade

{{ url('result/artista/'.$row->id) }}

Route::get('/result/artista/{id}', 'LiveSearch@result')

Edit1: For your named route only

route('artista', ['id' => $row->id]);

in controller : try print_r($id)

Upvotes: 1

Related Questions