简体   繁体   English

Laravel 错误:方法 Illuminate\\View\\View::__toString() 不能抛出异常

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

Have you seen this lovely error while working in Laravel?您在 Laravel 中工作时是否看到过这个可爱的错误?

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

I have seen it and it's incredibly annoying.我已经看过了,而且非常烦人。 I have found out two reasons why this error gets thrown.我发现了引发此错误的两个原因。 I just want to help people not take hours and hours of time.我只是想帮助人们不花几个小时的时间。

View answers & situations below.查看下面的答案和情况。 :) :)

There is a very simple solution: don't cast View object to a string.有一个非常简单的解决方案:不要将 View 对象转换为字符串。

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

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

For PHP version <7.4 By casting view, it uses __toString() method automatically, which cannot throw an exception. PHP 7.4 以下版本通过强制转换视图,自动使用__toString()方法,不会抛出异常。 If you call render() manually, exceptions are handled normally.如果您手动调用render() ,异常会正常处理。 This is the case if there is an error in the view - laravel throws an exception.如果视图中有错误,就会出现这种情况——laravel 会抛出异常。

It's fixed in PHP >=7.4 you should not encounter this issue: https://wiki.php.net/rfc/tostring_exceptions .它已在 PHP >=7.4 中修复,您不应遇到此问题: https : //wiki.php.net/rfc/tostring_exceptions

For PHP version <7.4: This actually is a PHP limitation, not Laravels.对于 PHP 版本 <7.4:这实际上是 PHP 限制,而不是 Laravel。 Read more about this "feature" here: https://bugs.php.net/bug.php?id=53648在此处阅读有关此“功能”的更多信息: https : //bugs.php.net/bug.php?id=53648

Situation 1: Trying to print out a value in an array.情况 1:试图打印出数组中的值。

Answer 1: Try printing out the array.答案 1:尝试打印出数组。 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.尝试做一个 print_r 并看看你得到了什么。

Situation 2: You have this associated array like this:情况 2:您有这样的关联数组:

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.当您尝试直接在视图中访问 post_images 数组时,它会引发错误。 No. Matter.不管。 What.什么。 You.你。 Do.做。

Answer 2: Check in all the places where you are calling the View.答案 2:检查所有调用 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.这里发生的事情是我试图在我没有提供 post_images 数组的区域中的其他地方访问相同的视图。 Took FOREVER to figure out.花了 FOREVER 才弄明白。

I hope this helps someone else.我希望这对其他人有帮助。 :) I just know the error I kept getting didn't help me anywhere. :) 我只知道我不断收到的错误对我没有任何帮助。

I encountered error like this when an object in my case $expression = new Expression();在我的情况下,当一个对象$expression = new Expression();时,我遇到了这样的错误$expression = new Expression(); is the same as the parameter variable submitExpression($intent, $bot_id, **$expression**){ check below code for more details.与参数变量submitExpression($intent, $bot_id, **$expression**){请查看下面的代码以获取更多详细信息。

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.一切正常,希望你觉得这有帮助。

My problem was finding out where exactly View::__toString() was being called in my code, so that I could fix it with using render() (as the other answers suggest).我的问题是找出在我的代码中调用View::__toString()确切位置,以便我可以使用render()修复它(正如其他答案所建议的那样)。

To find it, temporarily edit vendor/laravel/framework/src/Illuminate/View/View.php , adding logging of the current stack trace:要找到它,请临时编辑vendor/laravel/framework/src/Illuminate/View/View.php ,添加当前堆栈跟踪的日志记录:

public function __toString()
{
    // Next line added temporarily to debug.
    logger("This causes the '__toString() must not throw an exception' problem: " 
        . (new \Exception())->getTraceAsString());
    return $this->render();
}

a similar error is:类似的错误是:

FatalErrorException in FooController.php line 0: Method App\\Models\\Foo::__toString() must not throw an exception FatalErrorException in FooController.php line 0: Method App\\Models\\Foo::__toString() 不能抛出异常

and it was just a bad assignment: $foo.= new Foo;这只是一个糟糕的分配: $foo.= new Foo;

instead of: $foo = new Foo;而不是: $foo = new Foo;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel错误:方法Illuminate \ View \ View :: __ toString()不得抛出异常 - Laravel Error : Method Illuminate\View\View::__toString() must not throw an exception 方法Illuminate \\ View \\ View :: __ toString()不得在Laravel中引发异常 - Method Illuminate\View\View::__toString() must not throw an exception in Laravel laravel方法Illuminate \\ View \\ View :: __ toString()不得引发异常 - laravel Method Illuminate\View\View::__toString() must not throw an exception 在Laravel的视图内部加载视图时,方法Illuminate \\ View \\ View :: __ toString()不得抛出异常 - Method Illuminate\View\View::__toString() must not throw an exception when loading views inside view in Laravel 方法Illuminate \\\\ View \\\\ View :: __ toString()不得在unix中引发异常 - Method Illuminate\\View\\View::__toString() must not throw an exception in unix paginator-&gt; links()结果方法Illuminate \\ View \\ View :: __ toString()不能抛出异常 - paginator->links() results in Method Illuminate\View\View::__toString() must not throw an exception 方法 Laravel\\Passport\\Bridge\\AccessToken::__toString() 不能抛出异常 - Method Laravel\\Passport\\Bridge\\AccessToken::__toString() must not throw an exception PracticeController.php 第 0 行中的 FatalErrorException:Method Illuminate\\Database\\Eloquent\\Collection::__toString() 不能抛出异常** - FatalErrorException in PracticeController.php line 0: Method Illuminate\Database\Eloquent\Collection::__toString() must not throw an exception** 方法App \\ Follower :: __ toString()不得引发异常:错误 - Method App\Follower::__toString() must not throw an exception: Error 错误:方法__toString()不能抛出异常symfony2 - Error: method __toString() must not throw an exception symfony2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM