You try to put an array to a single variable
Probably here
''' 'image' => $this->images, '''
getting this error when im trying to upload a images ErrorException Array to string conversion
protected $rules = [
'category' => 'required|integer|exists:categories,id',
'title' => 'required|min:4',
'body' => 'required|min:4',
];
public function createPost()
{
if (auth()->check()) {
$this->validate(
[
'images.*' => 'image|max:1024', // 1MB Max
]
);
$random = str_pad(mt_rand(1,999999),6,'0',STR_PAD_LEFT);
$post = Post::create([
'user_id' => auth()->user()->id,
'title' => $this->title,
'category_id' => $this->category,
'status_id' => $this->status,
'body' => $this->body,
'post_number' => $random,
'slug' => Str::slug($this->title) ?? null,
'image' => $this->images,
]);
foreach ($this->images as $key => $image) {
$this->images[$key] = $image->storeAs('posts');
}
$this->images = json_encode($this->images);
session()->flash("message", "Featured image successfully uploaded");
@ene If 2 (or 3, or ...) you can define 2 (3, ...) fields in your table post (image1, image2, ...)
If you don't want to fix a limit, you have to use a second table "post_images"
Read this https://laravel.com/docs/8.x/eloquent-relationships#one-to-many
Please or to participate in this conversation.