Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 830
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 5
Kenil Vasani
Asked: December 18, 20202020-12-18T21:38:26+00:00 2020-12-18T21:38:26+00:00In: Laravel, php

Laravel 5.4 Array to string conversion exception

  • 5

I am trying to break down a string into an array and then print the values on the screen. Here is the string that i am trying to break:

“Cog|Condor”

“|” using this to split it. Here is how I am doing it:

<?= $arrays = explode('|', $b->brand); foreach($arrays as $array){echo $array;}  ?> 

But i keep getting this exception:

    2/2) ErrorException
Array to string conversion (View: D:\Code\PHP\Code\CrownBillingSystem\resources\views\pages\print.blade.php)
in 6e7ee4930110d4a26a3e31e0ddfe8b87849a1319.php (line 93)
at CompilerEngine->handleViewException(object(ErrorException), 1)
in PhpEngine.php (line 44)
at PhpEngine-

I cannot figure out what is wrong here.

laravelphpstring
  • 1 1 Answer
  • 11 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-18T21:36:30+00:00Added an answer on December 18, 2020 at 9:36 pm

      While the other answers are not incorrect, Blade has been designed to eradicate the use of PHP tags. Blade functions allow you to do everything.

      The error being produced here is that <?= is shorthand for <php echo. So your code will render as echo $arrays in pseudo code terms which is where the PHP is breaking because you cannot echo an array.

      To better your code in this instance, you should be manipulating as much of the data as possible in the controller which is also mentioned here in the blade documentation.

      Might I suggest modifying your code, to produce the same result, but using blade.

      @php 
          $arrays = explode('|', $b->brand); 
      @endphp
      
      @foreach($arrays as $array)
          {{ $array }}
      @endforeach
      

      The above snippet will produce the same results as intended.

      An even better way of doing it, and to further your understanding would be to return the view from the controller, and pass in $arrays predefined. Something like this:

      public function echoArrays()
      {
          $b = Object::find(1); //or however you get $b
          $arrays = explode('|', $b->brand); 
          return view('view1', compact('arrays');
      }
      

      The above will allow you to use the code snippet 2 up, but without the @php [email protected] tags, and just use the @foreach() ... @endforeach

      • 3
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      Replicating claims as headers is deprecated and will removed from ...

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com