Laravel Error: Method Illuminate\View\View::__toString() must not throw an exception

前端 未结 4 1218
忘了有多久
忘了有多久 2020-12-03 04:13

Have you seen this lovely error while working in Laravel?

Method Illuminate\\View\\View::__toString() must not throw an exception

I have s

相关标签:
4条回答
  • 2020-12-03 04:54

    Situation 1: Trying to print out a value in an array.

    Answer 1: Try printing out the array. Are you sure it's an array? I've gotten this error when it was an object instead of an array. Try doing a print_r and seeing what you get.

    Situation 2: You have this associated array like this:

    Array
        (
            [post_id] => 65
            [post_text] => Multiple Images!
            [created_at] => 2014-10-23 09:16:46
            [updated_on] => 
            [post_category] => stdClass Object
                (
                    [category_label] => Help Wanted
                    [category_code] => help_wanted
                )
    
            [employee_full_name] => Sam Jones
            [employee_pic] => /images/employee-image-placeholder.png
            [employee_email] => jon@gmail.com
            [post_images] => Array
                (
                    [0] => stdClass Object
                        (
                            [image_path] => 9452photo_2.JPG
                        )
    
                    [1] => stdClass Object
                        (
                            [image_path] => 8031photo_3.JPG
                        )
    
                )
    
        )
    

    When you try to access post_images array directly within a View, it throws an error. No. Matter. What. You. Do.

    Answer 2: Check in all the places where you are calling the View. What happened here is that I was trying to access the same view somewhere else in an area where I wasn't giving the post_images array. Took FOREVER to figure out.

    I hope this helps someone else. :) I just know the error I kept getting didn't help me anywhere.

    0 讨论(0)
  • 2020-12-03 05:00

    I encountered error like this when an object in my case $expression = new Expression(); is the same as the parameter variable submitExpression($intent, $bot_id, **$expression**){ check below code for more details.

    private function submitExpression($b_id, $expression){
       $expression = new Expression();
       $expression->b_id = $b_id;
       $expression->expression = $expression;
       $expression->save();
    
    }
    

    so I changed the above code to something like

    private function submitExpression($b_id, $statement){      
       $expression = new Expression();
       $expression->b_id = $b_id;
       $expression->expression = $statement;
       $expression->save(); 
    }
    

    and everything was working fine, hope you find this helpful.

    0 讨论(0)
  • 2020-12-03 05:04

    a similar error is:

    FatalErrorException in FooController.php line 0: Method App\Models\Foo::__toString() must not throw an exception

    and it was just a bad assignment: $foo.= new Foo;

    instead of: $foo = new Foo;

    0 讨论(0)
  • 2020-12-03 05:07

    There is a very simple solution: don't cast View object to a string.

    Don't: echo View::make('..'); or echo view('..');

    Do: echo View::make('..')->render(); or echo view('..')->render();

    For PHP version <7.4 By casting view, it uses __toString() method automatically, which cannot throw an exception. If you call render() manually, exceptions are handled normally. This is the case if there is an error in the view - laravel throws an exception.

    It's fixed in PHP >=7.4 you should not encounter this issue: https://wiki.php.net/rfc/tostring_exceptions.

    For PHP version <7.4: This actually is a PHP limitation, not Laravels. Read more about this "feature" here: https://bugs.php.net/bug.php?id=53648

    0 讨论(0)
提交回复
热议问题