Laravel从数据库中保存并显示图像

时间:2015-11-03 12:31:59

标签: php sql laravel

Laravel问题: 如何在数据库中存储和显示图像。
可以帮我解决一下吗?

这是我的代码:

商店代码:

public function store(FormRequest $request)
    {

        $slug = uniqid();
        $item = new Item(array(
            'title' => $request->get('title'),
            'image' => $request->get('image'),
            'content' => $request->get('content'),

        ));

        $item->save();

    }

显示代码:

    @foreach($items as $item)
            <tr>
                   <td>{!! $item->id !!} </td>
                   <td>{!! $item->title !!}</td>
                   <td><img src="{{ asset($item->image) }}" /></td>

             </tr>
@endforeach

2 个答案:

答案 0 :(得分:0)

您可以使用data-url将base64编码的内容添加到src中,如维基百科中所述:https://en.wikipedia.org/wiki/Data_URI_scheme

您还可以添加提供图像的路线,例如id。

答案 1 :(得分:0)

   // Upload image coding   
// Get Form Image
             $image = $request->file('image');
            $slug = str_slug($request->name);
            if (isset($image))
            {
                $currentDate = Carbon::now()->toDateString();
                $imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
                if (!file_exists('storage/uploads/post'))
                {
                    mkdir('storage/uploads/post',0777,true);
                }
                $image->move('storage/uploads/post',$imagename);
            }else{
                $imagename = "default.png";
            }

//getting coding


 public function index(){

    return view('admin.post.index',compact('posts'));
    }

// show code

<div class="body">
  <img class="img-responsive img-thumbnail" src="{{ asset('storage/uploads/post/'.$post->image) }}" >
    </div>