1

So I created a form that can post images like this, and tried to process the images as I examined view

<form  action="{{ $action }}" enctype="multipart/form-data" method="post">
 <input type="file" name="imagefile" accept="image/*">
 <input type="text" name="content">
 <button tyoe="submit">OK</button>
</form>

Processing with the controller took out the image and saved the place while watching other people's blog

$post_data = $request->except('imagefile');
$imagefile = $request->file('imagefile');
$temp_path = $imagefile->store('public/temp');

$a = new Test;
$a->fill($request->all())->save();

I thought it was going well, but there was an error

Cannot insert value NULL into 'imagefile'

I checked after that but the cause is not understood.

7
  • "Form" must have enctype="multipart/form-data", like this : <form method="post" action="example.cgi" enctype="multipart/form-data">
    – Ngoc Nam
    Nov 29, 2019 at 2:34
  • @NgocNam I'm sorry, the actual form has it but I forgot to write it
    – su3158
    Nov 29, 2019 at 2:38
  • Maybe the problem is $a->fill($request->all())->save();, because imagefile was set to object "a" ,imagefile field/column be null, you try to debug, set $a->imagefile be an empty string before save (insert) it.
    – Ngoc Nam
    Nov 29, 2019 at 2:44
  • @NgocNam It might work if you assign it before saving
    – su3158
    Nov 29, 2019 at 2:46
  • So, the problem is inserting. Because imagefile is an object, you must find path of image then set it into imagefile field of object $a, then insert into DB, otherwise, you must change imagefile type to store an object.
    – Ngoc Nam
    Nov 29, 2019 at 2:48

3 Answers 3

2

Try this it May help You..

$post_data = $request->except('imagefile');
$imagefile = $request->file('imagefile');
$temp_path = $imagefile->store('public/temp');
$filename = $request->file('image');
$post_data->imagefile = $filename;

$a = new Test;
$a->fill($post_data)->save();

or

$a = new Test;
$a->imagefile = $filename; // here imagefile equal to your db fild
$a->save();
4
  • please explain it. Nov 29, 2019 at 6:22
  • I don't know the cause yet, but it seems to be long
    – su3158
    Nov 29, 2019 at 6:23
  • This error Save () gives The identity column 'id' cannot be updated.
    – su3158
    Nov 29, 2019 at 6:29
  • Thank you very much. The image problem is solved An error with data save () is another matter
    – su3158
    Nov 29, 2019 at 6:37
1

You need to assign file name to the values array before save. Also don't use $request->all(); this contains image object too & we don't need it when saving. We only need the file name. In below code i have assign the file name into $post_data array before save it.

$post_data = $request->except('imagefile');
$imagefile = $request->file('imagefile');
$temp_path = $imagefile->store('public/temp');

$filename = $request->file('image')->hashName();
$post_data->imagefile = $filename;

$a = new Test;
$a->fill($post_data)->save();
2
  • Does everything in the request fall on post_date?
    – su3158
    Nov 29, 2019 at 3:36
  • post_data contains everything except imagefile object.
    – LahiruTM
    Nov 29, 2019 at 4:22
0

Please try below line to get all parameters regarding your image.

$imagefile = $_FILES;

print the $imagafile array before proceeding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.