php Laravel 错误:方法 Illuminate\View\View::__toString() 不能抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26534016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Error: Method Illuminate\View\View::__toString() must not throw an exception
提问by cbloss793
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. :)
查看下面的答案和情况。:)
采纳答案by cbloss793
Situation 1: Trying to print out a value in an array.
情况 1:试图打印出数组中的值。
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.
答案 1:尝试打印出数组。你确定是数组?当它是一个对象而不是一个数组时,我收到了这个错误。尝试做一个 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] => [email protected]
[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.
当您尝试直接在视图中访问 post_images 数组时,它会引发错误。不管。什么。你。做。
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.
答案 2:检查所有调用 View 的地方。这里发生的事情是我试图在我没有提供 post_images 数组的区域中的其他地方访问相同的视图。花了 FOREVER 才弄明白。
I hope this helps someone else. :) I just know the error I kept getting didn't help me anywhere.
我希望这对其他人有帮助。:) 我只知道我不断收到的错误对我没有任何帮助。
回答by Mārti?? Briedis
There is a very simple solution: don't cast View object to a string.
有一个非常简单的解决方案:不要将 View 对象转换为字符串。
Don't: echo View::make('..');
or echo view('..');
不要:echo View::make('..');
或echo view('..');
Do: echo View::make('..')->render();
or echo view('..')->render();
做:echo View::make('..')->render();
或echo view('..')->render();
For PHP version <7.4By 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.
对于PHP <7.4 版本,通过强制转换视图,它会__toString()
自动使用方法,不会抛出异常。如果render()
手动调用,异常处理正常。如果视图中有错误,就会出现这种情况——laravel 会抛出异常。
It's fixed in PHP >=7.4you 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. Read more about this "feature" here: https://bugs.php.net/bug.php?id=53648
对于 PHP 版本 <7.4:这实际上是 PHP 限制,而不是 Laravel。在此处阅读有关此“功能”的更多信息:https: //bugs.php.net/bug.php?id=53648
回答by Solar
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.
当我的情况下的对象$expression = new Expression();
与参数变量相同时,我遇到了这样的错误,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.
一切正常,希望你觉得这有帮助。
回答by alexanderesmar
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;