3

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 }}
2
  • 1
    Try the first option, but with a closing ) before }} Commented Aug 2, 2017 at 18:18
  • Thanks @AlivetoDie silly mistake lets to big confusion your answer really helped me lot. Commented Aug 2, 2017 at 18:24

2 Answers 2

5

You can try these:-

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

Or:-

{{ url("/posts/$post->id/comments") }} // double quotes
Sign up to request clarification or add additional context in comments.

Comments

4

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]) }}

1 Comment

Mr. Shukshin your answer makes me think deep. Its really elegant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.