Laravel如何将图像上传到数据库并在视图中显示

时间:2019-07-13 13:09:50

标签: php laravel

所以我正在制作一个由三个字段组成的表单,标题,缩略图和textarea(带有texteditor),似乎上传的图像很好,因为我注意到我在{我提交表单后进入{1}}文件夹,但是当我检查数据库时,缩略图字段未显示/public/image文件夹中的文件名,例如20190713125534.jpg,而是

/public/image

我很困惑,我认为它根本不会上传图像,但是正如我之前解释的那样,所以我的问题是如何更改带有文件名的缩略图字段的值以及如何在我的图像中显示图像查看?

这是我的Blogcontroller.php

C:\xampp\tmp\phpC18A.tmp

这是我对表单的看法

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Blog;
use Validator,Redirect,Response,File;

class BlogController extends Controller
{
    public function index()
    {
        $blogs = Blog::get();
        return view('post.post_textarea',[
            'blogs' => $blogs
        ]);
    }

    public function store(Request $request)
    {
        Blog::create([
            'name' => $request->name,
            'message' => $request->message,
            'thumbnail' => $request->thumbnail,
       ]);
       if ($files = $request->file('thumbnail')) {
           $destinationPath = 'public/image/'; // upload path
           $profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension();
           $files->move($destinationPath, $profileImage);
           $insert['thumbnail'] = "$profileImage";
        }
        return redirect()->back();
    }

    public function getFullPost($blog_id) {
    $blogs = Blog::where('id', '=', $blog_id)->get();
    return view('post.read')->with(compact('blogs'));
    }
}

这是我的视图,用于显示数据库中的数据

<form enctype="multipart/form-data" method="POST">
                        {{csrf_field()}}
                        <div class="form-group">
                            <h4 style="color: black;" >Judul</h4>
                            <br>
                            <input type="text" class="form-control" name="name">
                        </div> 
                        <div class="form-group">
                            <h4 style="color: black;" >Thumbnail</h4>
                            <br>
                            <input type="file" name="thumbnail">
                        </div>                     
                        <div class="form-group">
                            <h4 style="color: black;" >Isi</h4>
                            <br>
                                <textarea class="form-control" name="message" id="" rows="10"></textarea>
                        </div>
                        <div class="form-group">
                            <button class="pull-right site-btn" type="submit" >Upload<img src="../public/asset/img/icons/double-arrow.png" alt="#"/></button>
                        </div>                     
                    </form>

2 个答案:

答案 0 :(得分:1)

尝试将其添加到控制器中

$blog = new Blog;
$blog->name = $request->name;
$blog->message = $request->message;
if($request->hasFile('thumbnail')) {

            $file = Input::file('thumbnail');
            //getting timestamp
            $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
            $name = $timestamp. '-' .$file->getClientOriginalName();
            $file->move(public_path().'/images/', $name);
            $blog->thumbnail = url('/images/' . $name);

        }
$blog->save();

return back();

比您认为的

@foreach ($blogs as $blog)
     <div class="blog-item">
          <div class="blog-thumb">
               <img src="{{ $blog->thumbnail }}" alt="">  ->this is where i was supposed to fetch the image
          </div>

     <div class="blog-text text-box text-white">                            
           <div class="top-meta">{{ Carbon\Carbon::parse($blog->created_at)->format('d-m-Y')  }} /  di <a href="">Rakitan</a></div>
            <h3>{{ $blog->name }}</h3>
            <p>{!! \Illuminate\Support\Str::words($blog->message, 30, '...') !!}</p>
            <a href="{{ route('post.read', ['blog_id' => $blog->id]) }}" class="read-more">Lanjutkan Baca  <img src="asset/img/icons/double-arrow.png" alt="#"/></a>

           </div>
        </div>
@endforeach

答案 1 :(得分:0)

我认为您的代码有问题,在您的存储功能中,您没有正确保存它。请查看下面的代码以将缩略图的链接保存到数据库中。

    <?php
       namespace App\Http\Controllers;
       use Illuminate\Http\Request;
       use App\Http\Requests;
       use App\Http\Controllers\Controller;
       use App\Blog;    

       class BlogController extends Controller{

        //some your function goes here      
        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
          public function store(Request $request)
          {
                $blog = new Blog;
                $blog->name = $request->input('name');              
                $blog->message = $request->input('message');

                $file = $request->file('your_thumbnail');
                //make sure yo have image folder inside your public
                $destination_path = 'image/';
                $profileImage = date("Ymd").".".$file->getClientOriginalName();
                $file->move($destination_path, $profileImage);
                //save the link of thumbnail into myslq database        
                $blog->thumbnail = $destination_path . $profileImage;
                $blog->save();
                return redirect()->back();
           }
    }