gvgvgvijayan
gvgvgvijayan

Reputation: 2506

String interpolation in laravel

I tried all of this but can't predict interpolation read all points in official documentation but not yet found elegant solution-

{{ url('/posts/' . $post->id . '/comments' }}

{{ url('/posts/$post->id/comments') }}

Finally this one worked but I expecting something elegant than this

@php
  $url = url('/posts/' . $post->id. '/comments');
@endphp

{{ $url }}

Upvotes: 3

Views: 10045

Answers (2)

shukshin.ivan
shukshin.ivan

Reputation: 11340

The most elegant way is to use route names.

# routes/web.php
Route::get('/posts/{post}/comments', 'PostsController@comments')
        ->where('post', '[0-9]+')
        ->name('post-comments');

Then

# PostsController.php
use App\Post
...
public function comments(Post $post, Request $request) {
    // use $post object
}

And in view

{{ route('post-comments', ['post' => $post]) }}

Upvotes: 4

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You can try these:-

{{ url('/posts/' . $post->id . '/comments') }} // ) missing

Or:-

{{ url("/posts/$post->id/comments") }} // double quotes

Upvotes: 5

Related Questions