Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

488
Visualizações
laravel PHP Fatal error: Cannot redeclare for function

I have the following function :

function get_user_browser() {
  $u_agent = $_SERVER['HTTP_USER_AGENT'];
  $ub = 'unknown';
  if (preg_match('/Trident\/7.0; rv:11.0/', $u_agent)) {
    $ub = "ie ie-11";
  }
  elseif(preg_match('/MSIE/i', $u_agent)) {
    $ub = "ie";
  }
  elseif(preg_match('/Chrome/i', $u_agent)) {
    $ub = "chrome";
  }
  elseif(preg_match('/Firefox/i', $u_agent)) {
    $ub = "firefox";
  }
  elseif(preg_match('/Safari/i', $u_agent)) {
    $ub = "safari";
  }
  elseif(preg_match('/Opera/i', $u_agent)) {
    $ub = "opera";
  }
  return 'browser-'.$ub;
}

I have this function declare in two blades and I got this error :

FPHP Fatal error: Cannot redeclare get_user_browser() (previously declared in /home/storage/framework/views/0800146a0e35b205e9a66bb2f00ffb2f:7) in /home/storage/framework/views/0800146a0e35b205e9a66bb2f00ffb2f on line 7

How can I fix it?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Here a smart simple trick

add a file called helpers.php into your app folder

copy the function you wrote inside that file

add to composer.json under "autoload" the block:

 "files": [
  "app/helpers.php"
]

then run a composer update and you'll get the function available on all of your codebase

In this file you can add any helper functions you like, and as has already been told you should always avoid to write php code inside views

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda