Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel blade templates, foreach variable inside URL::to?

I'm creating a basic list view of posts, and need a link to the 'edit' page.

I'm using blade, and what I have is a table, with a foreach loop, showing each post along with edit/delete buttons.

What I wanted to do is use blade's URL::to for the links to the edit and delete pages, to ensure consistant links.

The code I've tried using (remember this is inside a foreach loop hence the $post->id var) is this:

<a href="{{ URL::to('admin/posts/edit/$post->id') }}" class="btn btn-mini btn-primary">Edit Post</a>

However this does not work. I've also tried

<a href="{{ URL::to('admin/posts/edit/<?php echo $post->id; ?>') }}" class="btn btn-mini btn-primary">Edit Post</a>

which also doesnt work.

I dont get any error, the link literally ends up being:

http://domain.dev/admin/posts/$post->id

Is there any way of working around this?

like image 536
Sk446 Avatar asked May 12 '13 11:05

Sk446


4 Answers

I think the problem is that you are using php variable ($post) within a string with a single '. In this case it just outputs the name of the variable. Try this:

<a href="{{ URL::to('admin/posts/edit/' . $post->id) }}" class="btn btn-mini btn-primary">Edit Post</a>

Hope this helps. Vlad

like image 141
vlad Avatar answered Oct 13 '22 19:10

vlad


vlad has already given the right answer to your question but be aware that you can also directly link to your controller action via URL::action:

<a href="{{ URL::action('Admin\PostsController@edit', $post->id) }}">Edit</a>
like image 44
Holger Weis Avatar answered Oct 13 '22 18:10

Holger Weis


The {{ }} are equal to <?php echo ;?>

if you put single '

<?php echo '$hello' ?> = $hello

but if you put double ' (") -> <?php "$hello" ;?> = Hello World (just one example)

You need to write something like {{ URL::to("admin/posts/edit/$post->id") }}

like image 1
Christian Avatar answered Oct 13 '22 18:10

Christian


I think this will work

<a href="{{ url('test/'.$post->id.'/view') }}"></a>
like image 1
Abdulmajeed Avatar answered Oct 13 '22 19:10

Abdulmajeed