Ain's avatar
Level 1

Store image

How do we store an image into the database? Because in the tutorial I follow, it only upload it

https://www.webslesson.info/2018/02/image-file-upload-in-laravel-with-validation.html

index.blade.php

<td>
    <html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    </head>
    <body>
    <br />
    @if (count($errors) > 0)
        <div class="alert alert-danger">
            Upload Validation Error<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    @if ($message = Session::get('success'))
        <div class="alert alert-success alert-block">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <strong>{{ $message }}</strong>
        </div>
        <img src="/images/{{ Session::get('path') }}" width="300" />
    @endif
    <form method="post" action="{{url('/masterStockist')}}" enctype="multipart/form-data">
        {{ csrf_field() }}
        <table class="table">
            <p width="30"><input type="file" name="select_file" /></p>
            <p width="30%" align="left"><input type="submit" name="upload" class="btn btn-primary" value="Upload"><span class="text-muted col-md-2">jpg, png, jpeg</span></p>
        </table>
    </form>
    </body>
    </html>
</td>

UploadPictureController.php

function upload(Request $request)
    {
        $this->validate($request, [
            'select_file'  => 'image|max:2048'
        ]);

        $image = $request->file('select_file');

        $new_name = rand() . '.' . $image->getClientOriginalExtension();

        $image->move(public_path('images'), $new_name);
        return redirect()->back()->with('success', 'Image Uploaded Successfully')->with('path', $new_name);
    }

web.php

Route::post('/masterStockist', 'UploadPictureController@upload');

Migration

$table->string('image')->nullable();
0 likes
7 replies
jlrdw's avatar

That is code to store an image just make sure the path is correct.

jlrdw's avatar

You need to store the filename in a database field. Like myimage.jpg.

I use the asset helper to display images.

The actual file is stored in the file system you're just using image tag to display the image.

Put another way you still have to save a database record in conjunction with the upload, that tutorial must be just how to upload.

Ain's avatar
Level 1

@jlrdw

I need to store the image to a specific user, by using "restock_user_id". Do you perhaps know how to do it?

$this->validate($request, [
'select_file'  => 'image|max:2048'
]);

$image = $request->file('select_file');

$new_name = rand() . '.' . $image->getClientOriginalExtension();

$image->move(public_path('images'), $new_name);

Restock::create([
'image'=>$new_name,
]);

return redirect()->back()->with('success', 'Image Uploaded Successfully')->with('path', $new_name);

Because after the user fill in others detail -> submit ->index : only then the upload image button appears. Because the total only appear once the user click submit.

Receipt: user need to transfer the money after know the total

mware's avatar

@ain If the specific user is the person filling out the form, then you should be able to get the user's id from the auth method.


Restock::create([
    'image' => $new_name,
    'restock_user_id' => auth()->id() //or auth()->user()->id
]);

That should accomplish what you're looking for.

Ain's avatar
Level 1

thankyou @mware . I have another question. Do you know how to save the image field only? Because it been asking for all the data for each column down below.

    protected $fillable = [
    'restock_user_id',
    'address',
    'phone_no',
    'date',
    'total',
    'status_id',
    'tracking_no',
    'image'

The error https://imgur.com/a/7KIskE2

jlrdw's avatar

If you are storing images for users you should have a child table for the images with user_id as a foreign key.

It's hard to know exactly what you need because it's your database.

Some people even have a separate folder for each uses images, it depends on how secure these images need to be.

image the same name you gave the actual file, but that name also has to be stored in the database.

Please or to participate in this conversation.