Laravel检查var是否不存在

时间:2019-02-16 13:45:30

标签: php laravel

我有条件:

package com.myPackage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Bean;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Controller
public class WebController extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/page1").setViewName("page1");
        registry.addViewController("/page2").setViewName("page2");
        registry.addViewController("/page3").setViewName("page3");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/403").setViewName("error/403");
    }

    @GetMapping("/page1")
    public String page1(HttpSession session) {
        return "page1";
    }

    @GetMapping("/page2")
    public String page2(HttpSession session) {
        return "page2";
    }

    @GetMapping("/page3")
    public String page3(HttpSession session) {
        return "page3";
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/403")
    public String error403() {
        return "error/403";
    }

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("templates/");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    private String getCredentials() {
        String credential = null;

        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        credential = userDetails.getUsername().toString();

        return credential;
    }

}

{{ setting('notifications.email.enabled') ? 'checked' : '' }} 中可以是值:setting('notifications.email.enabled')1,并且值可以不存在。如何检查0是否不存在(在表中),如果不存在,则如何检查setting('notifications.email.enabled')。现在我发现'checked'不存在。

2 个答案:

答案 0 :(得分:1)

尝试一下:

{{ setting('notifications.email.enabled', true) ? 'checked' : '' }}

通过在第二个参数中设置默认值

答案 1 :(得分:0)

将其与0进行比较,否则返回checked,如果未设置或为1,则将返回选中状态。

{{ (setting('notifications.email.enabled') === 0) ? '' : 'checked' }}