我试图使用@if& @else在控制器内部,例如:
public function deleteCharacter ($char_delete_id)
{
@if (Auth::user()->id == $delCharacterUserID)
$deleteSuccess = 1;
return redirect('/characters')->with('deleteSuccess', $deleteSuccess);
@else
$deleteSuccess = 0;
return redirect('/characters')->with('deleteSuccess', $deleteSuccess);
@endif
这会引发错误:
CharacterController.php第40行中的FatalErrorException:语法错误, 意外的'如果' (T_IF)
答案 0 :(得分:7)
在控制器中,只需使用normal PHP syntax,而不是blade syntax。所以用以下代码替换你的代码:
public function deleteCharacter ($char_delete_id)
{
if (Auth::user()->id == $delCharacterUserID) {
$deleteSuccess = 1;
} else {
$deleteSuccess = 0;
}
return redirect('/characters')->with('deleteSuccess', $deleteSuccess);
}
它应该工作!控制器是一个PHP文件,所以在其中编写PHP。
答案 1 :(得分:1)
优化您的代码
#define MAGIC_MACRO_ELEM(r, data, i, elem) \
BOOST_PP_COMMA_IF(i) BOOST_PP_STRINGIZE(elem)
#define MAGIC_MACRO(...) \
BOOST_PP_SEQ_FOR_EACH_I(MAGIC_MACRO_ELEM, ~, BOOST_PP_TUPLE_TO_SEQ((__VA_ARGS__)))