Ain's avatar
Level 1

Upload image

I want user to upload image only after they submit the other details. Because after they submit, only then the total will be calculated.

below is the sequences :

  1. https://imgur.com/a/0XWIJHG

  2. https://imgur.com/a/rmt2jzr

  3. (after user click "Click here") https://imgur.com/a/a4y4KFX

The question is, during uploading the image, does it considered as create or edit?

0 likes
7 replies
Tangente's avatar

It would depend on how you structured your database. Personally I would do this. Each record has an image, so we have one to one relationship.

Image would be part of the record, and it would be nullable. That way, you can add all record information, save, then when you go to upload the image, all you would do is update instead of create.

If you were to do many-to-many relationships though, like a record may have one or many uploads, in that case you would do 'create' each time you save the upload, and associate the record id to each upload entry.

1 like
Ain's avatar
Level 1

hi @tangente ,

the relationship is one-to-many https://imgur.com/a/51JGZGS. Do i have to do the 'create' like many-to-many relationship?

If yes, would you mind to explain how do i associate the record id to each upload entry?

Tangente's avatar

From that picture, it looks like the image is part of restocks table. so, one restock has exactly one image from that image (one to one). Well, a user may have one or more restocks (one to many) but a restock has one image. therefore

  1. Save user record (create) for example:
$user = User::create(['name'=>$request->get('name'),  'address'=>$rquest->get('address')]);

now you have the user saved.

  1. save restock (without image, therefore make image column nullable) (create)

example:

$restock = new Resocks();
$restock->address=$request->get('address');
$restock->phone=$request->get('phone');
.
.
.
$restock->restock_user_id=$user->id;//because you saved the user
$restock->save();

then you go to upload section. This is where you do the edit, because you are going to add image location after you upload

example:


$restock = findOfFail($request->get('restock'));
 if(isset($request->get('image))
        {


            $image=$$request->get('image');

            $extension = $image->getClientOriginalExtension();
            $storage = '/app/Pictures/';
            $fileNameNoStorage= str_random(100).$image->getFilename() . '.' . $extension;
            $fileName = $storage.$fileNameNoStorage;
            $image->move(storage_path() . $storage, $fileName);

            $restock->image=$fileNameNoStorage;
        $restock->save();

        }

That's how I would do that

Ain's avatar
Level 1

@tangente im getting this error, do you perhaps know why and how to solve it?

Error https://imgur.com/a/3jmnQQg

When I do the dd($request) https://imgur.com/a/BY1IRPJ

UploadPictureController.php

    public function store(Request $request)
    {
        $input = $request->all();

        if($file = $request->file('file')){

            $name = $file->getClientOriginalName();

            $file->move('images', $name);

            $input ['image'] = $name;

        }
        Restock::create($input);
        
    }
Sinnbeck's avatar

Your "error" image is just an image of the code you posted?

Tangente's avatar

@ain , what error are you getting? Need to see the error so we can debug from there

Please or to participate in this conversation.