'strpos(): Argument #1 ($haystack) must be of type string, array given
I'm newbie in Laravel, and follow a free course in Laracast. This is link of my course my_cource (episode 11 at 2:49s).
Summary of my problem: I'm trying to make my code more declarative with what I wan't to do (more specific is make a class name: Post
, and a method: find()
inside this class). This file declare Post class:
Model/Post.php:
<?php
namespace App\Models;
class Post
{
public static function find($slug)
{
//don't write any thing in this function,
//i expected an blank page (not an error page like my bug)
}
}
And this file direct url when user click:
routes/web.php:
<?php
use Illuminate\Support\Facades\Route;
use App\Models\Post;
Route::get('/', function () {
return view('posts');
});
Route::get('posts/{post}', function ($slug) {
//find post with slug and pass it intos posts view (through $post variable)
$post = Post::find($slug);
return view([
'post' => $post
]);
})
->where('post', '[A-z_-]+');
This file return Html page, it's view of user (what user see in browser):
views/post.blade.php:
<body>
<article>
<?= $post ?>
</article>
</body>
</html>
This is what I expect: Expect result
But this is the real I received: strpos(): Argument #1 ($haystack) must be of type string, array given
Error say: strpos(): Argument #1 ($haystack) must be of type string, array given
Thanks so much.
Solution 1:[1]
In the return, method view should have a string, then maybe followed by the array data
Route::get('posts/{post}', function ($slug) {
$post = Post::find($slug);
return view('post', [
'post' => $post
]);
})
->where('post', '[A-z_-]+');
Solution 2:[2]
I've already fixed it ;) Thanks. The issue was
php8.0-mbstring & php8.0-xml
just install them :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Pri Nce |