Chron's avatar
Level 6

How can I make precognition to work on file uploads

'avatars' => 'nullable|array',
'avatars.*' => 'image|mimes:jpg,png|max:1024',

I need help. Whenever I add file(s), it just sends 204 No Content

It works perfectly fine with other input types.

import { useForm } from 'laravel-precognition-vue-inertia';

const form = useForm('post', route('account.store'), {
	people: [],
    avatars: [],
  });

I tried adding a header { headers: { 'Content-Type': 'multipart/form-data' } } like in axios but it didn't work. Tried adding forceFormData: true like in inertia but it didn't work either.

0 likes
10 replies
Tray2's avatar

Have you added the correct enc type to the form?

 enctype="multipart/form-data"
Chron's avatar
Level 6

@Tray2

<script>
import { useForm } from 'laravel-precognition-vue-inertia';

const form = useForm('post', route('account.store'), {
	//...
    avatars: [],
});

const submit = () => form.submit({
	preserveScroll: true,
	forceFormData: true,
	headers: {
		'Content-Type': 'multipart/form-data'
	},
})
</script>
<template>
<form @submit.prevent="submit" method="POST" enctype="multipart/form-data">
	//...

	<input type="file" name="avatars" @change="(e) => { form.avatars = e.target.files; form.validateFiles(); form.validate('avatars') }" multiple />

	<button type="submit">Submit<button>
</form>
</template>
use Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests;
use App\Http\Controllers\AccountController;

Route::controller(AccountController::class)->group(function() {
		Route::name('account.')->group(function() {
				Route::middleware([HandlePrecognitiveRequests::class])->group(function() {
						Route::post('/accounts', 'store')->name('store');
				});
		});
});

I think that's all of it. 🤔

Chron's avatar
Level 6

@Tray2

FormReq

return [
	// ...
	'avatars' => 'nullable|array',
	'avatars.*' => 'image|mimes:jpg,png|max:1024',
];

Controller

public function store(StoreAccountRequest $request) {
	//... creation

	return response()->json(201);
}
Tray2's avatar

@Chron What do you get if you

dd($request->validated());

In your controller?

Chron's avatar
Level 6

@Tray2

It's like using without Precognition, it just dumps the data

Chron's avatar
Level 6

@Tray2

array:4 [â–¼ // app/Http/Controllers/AccountController.php:49
  //...
  "avatars" => array:1 [â–¼
    0 => Illuminate\Http\UploadedFile {#378 â–¶}
  ]
]

Please or to participate in this conversation.