[英]how to echo strings and variable in laravel 5.1 blade
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. 问题是只显示
http://localhost:8000/students
,而不是变量的值。 So what is the solution? 那么解决方案是什么?
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
As it seems you're passing some parameter in the 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
Then it will print 14
然后它将打印
14
Hope this helps you 希望这对你有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.