简体   繁体   English

Laravel:如何在 {{ }} 内实现 if 语句

[英]Laravel: How to implement if statement within {{ }}

I'm having to pass search filters in links in my laravel app on various occasions.在各种情况下,我不得不在我的 laravel 应用程序的链接中传递搜索过滤器。 The following statement was working fine in laravel 4, but now I'm converting it to laravel 8 and its giving me error now.以下语句在 laravel 4 中运行良好,但现在我将其转换为 laravel 8 并且它现在给我错误。

{{ link_to_route('workprocess.edit', 'Edit Work Process', array($workprocess->id, json_encode($searchFilters)), array('class' => 'btn btn-sm btn-warning')) }}

Error错误

Missing required parameters for [Route: workprocess.edit] [URI: workprocess/edit/{workprocess}/{search?}]. (View: D:\xampp\htdocs\compliance.local\resources\views\home.blade.php)

Reason $searchFilters is empty sometimes since the same template is being used for searching as well.原因$searchFilters 有时是空的,因为同样的模板也被用于搜索。 $searchFilters = array('fromdate' => '', 'todate' => '', 'name' => '', 'type' => '');

I want to put an if statement inside {{ }} for json_encode($searchFilters) so that I don't have to write code for two links as there are quite a few other such occurances.我想在 {{ }} 中为json_encode($searchFilters)放置一个 if 语句,这样我就不必为两个链接编写代码,因为还有很多其他此类事件。

This would be my solution;这将是我的解决方案;

Where $searchFilters is set: $searchFilters 设置的位置:

if (empty($searchfilters)) {
    $searchfilters = 1;
};

and add a ternary inside json_encode method:并在json_encode方法中添加一个三元组:

json_encode(empty($searchFilters)?= 1: $searchFilters; '');

You can add an if statement using shorthand for example:您可以使用简写形式添加一个 if 语句,例如:

{{ $var === "hello" ? "Hi" : "Goodbye" }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM