Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Have you seen this lovely error while working in Laravel?

Method IlluminateViewView::__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. :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
185 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...