当我试图将图像插入数据库中时,它只是向我展示路径,例如http://127.0.0.0.1:8000/storegation/storage/images'

发布于 2025-02-05 19:37:54 字数 4022 浏览 0 评论 0 原文

我是Laravel 8的新手,我正在尝试使用图像插入数据,因此,当我尝试插入数据时,它只显示了插入的数据和图像路径。我该如何解决?

file restaurantcontroller.php

<?php
    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Models\Restaurant;

    class RestaurantController extends Controller
    {
        function add(Request $request)
        {
            $resto = new Restaurant;
            $resto->name = $request->input('name');
            $resto->email = $request->input('email');
            $resto->address = $request->input('address');
            $resto->create_at = $request->input('create_at');
            $resto->uploaded_at = $request->input('uploaded_at');
            $resto->image = $request->input('image');
            // $resto->image = $request->file('image')->store('images');

            if($request->hasFile('image'))
            {
                $image = $request->file('image');
                $image_name = $image->getClientOriginalName();
                $image->move(public_path('/images'), $image_name);

                $image_path = "/images/" . $image_name;
            }

            $resto->save();
            $request->session()->flash('status', 'Client entered successfully');
            return redirect('list');
        }
    }

file add.blade.blade.php

@extends('layout')
@section('content')

<div class="col-sm-6">
    <form method="POST" action="" enctype="multipart/form-data">
        @csrf

        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" name="name" class="form-control" id="name" placeholder="Enter Your Name">
        </div>

        <div class="form-group">
            <label for="email">Email</label>
            <input type="email" name="email" class="form-control" id="email"
                   placeholder="[email protected]">
        </div>

        <div class="form-group">
            <label for="address">Address</label>
            <textarea class="form-control" name="address" id="address" rows="3"></textarea>
        </div>

        <div class="form-group">
            <label for="create_at">Create At Date</label>
            <input type="date" name="create_at" class="form-control" id="create_at">
        </div>

        <div class="form-group">
            <label for="uploaded_at">Uploaded At Date</label>
            <input type="date" name="uploaded_at" class="form-control" id="uploaded_at">
        </div>

         <div class="form-group">
            <label for="image">Image</label>
            <input type="file" name="image" class="form-control" id="image">
        </div>

        <button type="Submit" clas="btn btn-primary">Submit</button>

    </form>
</div>
@stop

file web.php

Route::get('/', [RestaurantController::class, 'index']);
Route::view('/add', 'add');
Route::post('/add', [RestaurantController::class, 'add'])->name('add');

i &lt; img src = http://127.0.0.0.1:8000/storeapt Images Alt =“ Image” height =“ 50” width =“ 50”&gt; ,我想要的是 http://127.0.0.0.1:8000/storage/images/`my_image_name`

它告诉我

{“ _ token”:“ exic1hv4sx3qrz6zcqjjjjjnfil6bjblw938hfrkg8j“,“ name”:“ test”,“ email”,“ [email&nbsp; procepted] “ ”,“地址”:“测试地址”,“ create_at”:“ create_at”:“ 2022-05-30”,“ 2022-05-30”,“ “:{}}

作为输出

I'm new to Laravel 8 and I'm trying to insert the data with an image, so when I try to insert the data it just shows me the inserted data and image path only. How can I solve this?

File RestaurantController.php

<?php
    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Models\Restaurant;

    class RestaurantController extends Controller
    {
        function add(Request $request)
        {
            $resto = new Restaurant;
            $resto->name = $request->input('name');
            $resto->email = $request->input('email');
            $resto->address = $request->input('address');
            $resto->create_at = $request->input('create_at');
            $resto->uploaded_at = $request->input('uploaded_at');
            $resto->image = $request->input('image');
            // $resto->image = $request->file('image')->store('images');

            if($request->hasFile('image'))
            {
                $image = $request->file('image');
                $image_name = $image->getClientOriginalName();
                $image->move(public_path('/images'), $image_name);

                $image_path = "/images/" . $image_name;
            }

            $resto->save();
            $request->session()->flash('status', 'Client entered successfully');
            return redirect('list');
        }
    }

File add.blade.php

@extends('layout')
@section('content')

<div class="col-sm-6">
    <form method="POST" action="" enctype="multipart/form-data">
        @csrf

        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" name="name" class="form-control" id="name" placeholder="Enter Your Name">
        </div>

        <div class="form-group">
            <label for="email">Email</label>
            <input type="email" name="email" class="form-control" id="email"
                   placeholder="[email protected]">
        </div>

        <div class="form-group">
            <label for="address">Address</label>
            <textarea class="form-control" name="address" id="address" rows="3"></textarea>
        </div>

        <div class="form-group">
            <label for="create_at">Create At Date</label>
            <input type="date" name="create_at" class="form-control" id="create_at">
        </div>

        <div class="form-group">
            <label for="uploaded_at">Uploaded At Date</label>
            <input type="date" name="uploaded_at" class="form-control" id="uploaded_at">
        </div>

         <div class="form-group">
            <label for="image">Image</label>
            <input type="file" name="image" class="form-control" id="image">
        </div>

        <button type="Submit" clas="btn btn-primary">Submit</button>

    </form>
</div>
@stop

File web.php

Route::get('/', [RestaurantController::class, 'index']);
Route::view('/add', 'add');
Route::post('/add', [RestaurantController::class, 'add'])->name('add');

I got <img src=http://127.0.0.1:8000/storage/images alt="image" height="50" width="50" > and what I want is http://127.0.0.1:8000/storage/images/`my_image_name`

When I perform return $request->all();, it shows me

{"_token":"ExiC1hv4sX3qrz6ZcQJJNfIL6bjblw938hfRkG8J","name":"test","email":"[email protected]","address":"testing address","create_at":"2022-05-30","uploaded_at":"2022-06-01","image":{}}

as a output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀中猫帐中妖 2025-02-12 19:37:55

您可以简单地使用 Store()方法。

...

if ($request->hasFile('image')) {
  $image = $request->image->store('image'); // See store()
}

$restro->image = $image;
$restro->save();

在刀片模板内部,您可以以这样的方式检索存储的文件:

<img src="{{ asset('storage/' . $restro->image) }}" />

确保已在公共文件夹中创建了存储文件夹的符号链接。如果没有,可以通过此工匠命令来创建它:

php artisan storage:link

You can simply use the store() method.

...

if ($request->hasFile('image')) {
  $image = $request->image->store('image'); // See store()
}

$restro->image = $image;
$restro->save();

Inside the blade template, you can retrieve the stored file like this:

<img src="{{ asset('storage/' . $restro->image) }}" />

Make sure you have created the symbolic link of your storage folder in your public folder. If you haven't, you can create it by this artisan command:

php artisan storage:link
我早已燃尽 2025-02-12 19:37:54

您可以使用此代码:

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

    $file = $request->file('image');
    $fileName = $file->getClientOriginalName();
    $destinationPath = public_path() . '/images';
    $file->move($destinationPath, $fileName);
}

You can use this code:

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

    $file = $request->file('image');
    $fileName = $file->getClientOriginalName();
    $destinationPath = public_path() . '/images';
    $file->move($destinationPath, $fileName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文