Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

480
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!