salep
salep

Reputation: 1380

Laravel return variable in blade template

I have a route like below.

Route::get('profile/{username}', 'ProfileController@index');

ProfileController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ProfileController extends Controller
{
    function index() {
        view('profile.index');
    }
}

profile/index.blade.php

{username}

But it doesn't echo the username when I go to /profile/salep, what's missing here?

If I change my ProfileController to this (below), it works but PhpStorm says "Unreachable stamement" for the view.

class ProfileController extends Controller
{
    function index($username) {
        return $username;
        view('profile.index');
    }
}

I didn't use the structure below (took it from the documentation) because I need to pass my variable to a view rather than returning, so I need to both return and pass it to a view, I guess.

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});

Upvotes: 0

Views: 5696

Answers (2)

Alex Crawford
Alex Crawford

Reputation: 21

You were nearly there with the second attempt.

Try this:

class ProfileController extends Controller
{
    function index($username) {
        return view('profile.index')->with('username', $username);
    }
}

Upvotes: 1

salep
salep

Reputation: 1380

I solved it.

routes.php

Route::get('profile/{username}', 'ProfileController@index');

ProfileController.php

class ProfileController extends Controller
{
    function index($username) {
        return view('profile.index')->with('username', $username);
    }
}

Upvotes: 0

Related Questions