Laravel 刀片模板,URL::to 中的 foreach 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16506894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:26:03  来源:igfitidea点击:

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

templateslaravellaravel-4blade

提问by Sk446

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.

我正在使用刀片,我有一个表格,带有 foreach 循环,显示每个帖子以及编辑/删除按钮。

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

我想要做的是使用刀片的 URL::to 链接到编辑和删除页面,以确保一致的链接。

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

我尝试使用的代码(记住这是在 foreach 循环内,因此 $post->id var)是这样的:

<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?

有没有办法解决这个问题?

回答by vlad

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

希望这可以帮助。弗拉德

回答by Holger Weis

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:

vlad 已经为您的问题提供了正确答案,但请注意,您也可以通过以下方式直接链接到您的控制器操作URL::action

<a href="{{ URL::action('Admin\PostsController@edit', $post->id) }}">Edit</a>

回答by Abdulmajeed

I think this will work

我认为这会奏效

<a href="{{ url('test/'.$post->id.'/view') }}"></a>

回答by Christian

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

{{ }}等于<?php echo ;?>

if you put single '

如果你把单 '

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

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

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

但是如果你把 double ' (")-> <?php "$hello" ;?>= Hello World (只是一个例子)

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

你需要写一些类似的东西 {{ URL::to("admin/posts/edit/$post->id") }}

回答by Suresh Bala

Another way

其它的办法

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

回答by CraicerHyman

Had an issue with this in Laravel 5 so thought Id pop it in even if the question is old. Resolved my issue using

在 Laravel 5 中遇到了这个问题,所以即使问题很老,我也想把它弹出来。使用解决了我的问题

{{ URL::to('/box').'/'.$box->id }}

{{ URL::to('/box').'/'.$box->id }}

or
{{ url('/box').'/'.$box->id }}

或者
{{ url('/box').'/'.$box->id }}

回答by mnv

Also you may use route()helper to generate url by name of route. For example, definition of route:

您也可以使用route()helper 按路由名称生成 url。例如路由的定义:

Route::get('/test/mypage/{id}', 'MyController@myAction')->name('my_route_name');

Code in your view:

您认为的代码:

<a href="{{ route('my_route_name', $row['id']) }}">{{ $row['name'] }}</a>