laravel 如何在laravel 5.1刀片中回显字符串和变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33666015/
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
how to echo strings and variable in laravel 5.1 blade
提问by Sajjad
I am using this code inside my PHP
file:
我在我的PHP
文件中使用此代码:
university.blade.php
{{ url( $universities->universityName.'/students') }}" >
The problem is that displays just http://localhost:8000/students
, not the value of variable. So what is the solution?
问题是只显示http://localhost:8000/students
,而不是变量的值。那么解决方案是什么?
采纳答案by Emeka Mbah
Probably $universities->universityName
is empty
大概$universities->universityName
是空的
Lets assume your controller looks like this:
假设您的控制器如下所示:
<?php namespace App\Http\Controllers
use App\University; // I assume this is your model
class UniversityController extends Controller{
public function index()
{
$universities = Univeristy::all();
return view('universities', compact('universities'));
}
}
Then later in your universities.blade.php
然后在你的 universities.blade.php
<a href="{!! url("{$univerisity->universityName}/students") !!}">Students</a>
or you can check if $universities->universityName
is empty, if it doesn't print the URL means its empty.
或者你可以检查是否$universities->universityName
为空,如果它不打印 URL 意味着它是空的。
@if(!$universities || !$universities->universityName)
<a href="{!! url("{$univerisity->universityName}/students") !!}">Students</a>
@endif
回答by Sulthan Allaudeen
As it seems you're passing some parameter in the url.
看来您正在 url 中传递一些参数。
I assume that you want to print those parameter in your view
我假设您想在视图中打印这些参数
Here i wrote an example route, controller with view for your understanding.
在这里,我写了一个示例路线,带有视图的控制器以供您理解。
Step 1 :Write a route method
第 1 步:编写路由方法
Route::get('get/{id}', 'publicController@get');
Step 2 :Write a function inside your controller
第 2 步:在控制器中编写一个函数
public function get($id)
{
return view('get')->with('id', $id);
}
Now you're returning the parameter that you passed to the view
现在您要返回传递给视图的参数
Step 3 :Show in your View
第 3 步:在您的视图中显示
Inside your view you can simply echo it using
在您的视图中,您可以简单地使用
{{ $id }}
So If you have this in your url
所以如果你的网址中有这个
http://localhost/yourproject/get/14
http://localhost/yourproject/get/14
Then it will print 14
然后它会打印 14
Hope this helps you
希望这对你有帮助