Laravel 5.2 - 将文件上传到数据库 [英] Laravel 5.2 - Upload Files to Database

查看:134
本文介绍了Laravel 5.2 - 将文件上传到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是我拥有的:



数据库表名为 lamanInformasi 具有以下字段: id judul isi code> created_at , updated_at



我想:



用户可以上传多个文档或图像文件,这些文件将存储到数据库。文件名将保存到 isi 字段,文件本身将保存到名为 propic 的文件夹。用户还可以在网站上显示数据库中的所有数据。



这些是我的代码:



create.blade.php

 < form action =lamanInformasiController @ index method =postenctype =multipart / form-data> 
< input type =filename =image>< br />
< input type =submitname =submitvalue =Submit>
< / form>

lamanInformasiController.php



  public function index(Request $ request)
{
$ file = new file;
if(Input :: hasFile('image'))
{
$ destinationPath = public_path()。'/ propic /';
$ name = Input :: file('image') - > getClientOriginalName();
$ extension = Input :: file('image') - > getClientOriginalExtension();

$ file = Input :: file('image') - > move($ destinationPath,$ name。。。
}
$ file - > isi = $ request-> get($ file);
$ file - >保存();

$ lamanInformasi = LamanInformasi :: all();
return view('upload.index',compact('lamanInformasi'));
}

index.blade.php

 < table class =table table-striped table-borderedborder =1px solid black> 
< thead>
< tr>
< td> ID< / td>
< td> Judul< / td>
< td> Isi< / td>
< td>创建于< / td>
< td>更新于< / td>
< / tr>
< / thead>
< tbody>
@foreach($$ lamanInformasi as $ key => $ value)
< tr>
< td> {{$ value-> id}}< / td>
< td> {{$ value-> judul}}< / td>
< td> {{$ value-> isi}}< / td>
< td> {{$ value-> created_at}}< / td>
< td> {{$ value-> updated_at}}< / td>
< / tr>
@endforeach
< / tbody>
< / table>

运行时出现此错误

  ParameterBag.php第90行中的ErrorException:
array_key_exists():第一个参数应为字符串或整数

我在中有这个参数行89-91

  public function get($ key,$ default = null)
{
return array_key_exists($ key,$ this->参数)? $ this-> parameters [$ key]:$ default;
}

这是我的问题:

如何解决这个错误?我是否使代码正确上传文件?因为我已经尝试过类似的代码,它不工作。感谢

解决方案

有几件事你需要照顾。尝试下面的代码



LamanInformasiController.php - 控制器名称通常大写。

  class LamanInformasiController extends Controller 
{
/ **
* @var LamanInformasi - 包括上面的use语句的模型。
* /
protected $ model;

/ **
*实例化控制器时注入(模型)LamanInformasi。
* @param LamanInformasi $ model
* /
public function __construct(LamanInformasi $ model)
{
$ this-> model = $ model;
}

public function index()
{
$ lamanInformasi = $ this-> model-> all();
return view('upload.index',compact('lamanInformasi'));
}


public function store(Request $ request)
{
if(Input :: hasFile('image'))
{
$ destinationPath = public_path()。'/ propic /';
$ name = Input :: file('image') - > getClientOriginalName();
$ extension = Input :: file('image') - > getClientOriginalExtension();
$ fileName = $ name。'。'。$ extension;

//将文件存储在$ destinationPath
$ file = Input :: file('image') - > move($ destinationPath,$ fileName);

//在数据库中保存相应的记录
$ this-> model-> create(['isi'=> $ fileName]);

//返回成功消息
}
//返回失败消息
}

} //不要忘记包含使用语句输入或写入\Input

然后在 index.blade.php

 < table class =table table-striped table-borderedborder =1px solid black> 
< thead>
< tr>
< td> ID< / td>
< td> Judul< / td>
< td> Isi< / td>
< td>创建于< / td>
< td>更新于< / td>
< / tr>
< / thead>
< tbody>
@foreach($ lamanInformasi as $ file)
< tr>
< td> {{$ file-> id}}< / td>
< td> {{$ file-> judul}}< / td>
< td> {{$ file-> isi}}< / td>
< td> {{$ file-> created_at}}< / td>
< td> {{$ file-> updated_at}}< / td>
< / tr>
@endforeach
< / tbody>



因此

 < form action =/ upload8method =postenctype =multipart / form-data> 
< input type =filename =image>< br />
< input type =submitname =submitvalue =Submit>





t测试。如果不是这样,请告诉我。


These are what I have :

Database table named lamanInformasi, which has these fields: id, judul, isi, created_at, updated_at.

This is what I want :

User can upload multiple document or image files, and the files will be stored to database. The file names will be saved to isi field, and the files itself will be saved to a folder named propic. User also can show all the data from database on the website.

These are my codes :

create.blade.php

<form action="lamanInformasiController@index" method="post" enctype="multipart/form-data">
    <input type="file" name="image"><br />
    <input type="submit" name="submit" value="Submit">
</form>

lamanInformasiController.php

public function index(Request $request)
{
    $file = new file;
    if (Input::hasFile('image'))
    {
        $destinationPath = public_path().'/propic/';
        $name = Input::file('image')->getClientOriginalName();
        $extension = Input::file('image')->getClientOriginalExtension();

        $file = Input::file('image')->move($destinationPath, $name . "." . $extension);
    }
    $file -> isi = $request->get($file);
    $file -> save();

    $lamanInformasi = LamanInformasi::all();
    return view('upload.index', compact('lamanInformasi'));
}

index.blade.php

<table class="table table-striped table-bordered" border= "1px solid black">
    <thead>
        <tr>
            <td>ID</td>
            <td>Judul</td>
            <td>Isi</td>
            <td>Created At</td>
            <td>Updated At</td>
        </tr>
    </thead>
    <tbody>
        @foreach($$lamanInformasi as $key => $value)
        <tr>
            <td>{{$value->id}}</td>
            <td>{{$value->judul}}</td>
            <td>{{$value->isi}}</td>
            <td>{{$value->created_at}}</td>
            <td>{{$value->updated_at}}</td>
         </tr>
         @endforeach
    </tbody>
</table>

When I run it, I have this error :

ErrorException in ParameterBag.php line 90:
array_key_exists(): The first argument should be either a string or an integer

I have this in ParameterBag line 89-91

public function get($key, $default = null)
{
    return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
}

These are my questions :

How to fix that error? Did I make the code right to upload files? Because I have tried similar code, and it's not working. Thanks

解决方案

There are a couple of things which you need to take care of. Try the code as below

LamanInformasiController.php - controller names are generally capitalized

class LamanInformasiController extends Controller
{
    /**
     * @var LamanInformasi - include the use statement above for the model.
     */
    protected $model;

    /**
     * Inject (model)LamanInformasi while instantiating the controller.
     * @param LamanInformasi $model
     */
    public function __construct(LamanInformasi $model)
    {
        $this->model = $model;
    }

    public function index()
    { 
        $lamanInformasi = $this->model->all();
        return view('upload.index', compact('lamanInformasi'));
    }


    public function store(Request $request)
    {
        if (Input::hasFile('image'))
        {
            $destinationPath = public_path().'/propic/';
            $name = Input::file('image')->getClientOriginalName();
            $extension = Input::file('image')->getClientOriginalExtension();
            $fileName = $name.'.'.$extension;

            //store the file in the $destinationPath
            $file = Input::file('image')->move($destinationPath, $fileName);

            //save a corresponding record in the database
            $this->model->create(['isi'=> $fileName]);

            //return success message
        } 
        //return failure message
    }

}   //don't forget to include the use statement for Input or write \Input

Then in your index.blade.php

<table class="table table-striped table-bordered" border= "1px solid black">
<thead>
    <tr>
        <td>ID</td>
        <td>Judul</td>
        <td>Isi</td>
        <td>Created At</td>
        <td>Updated At</td>
    </tr>
</thead>
<tbody>
    @foreach($lamanInformasi as $file)
    <tr>
        <td>{{$file->id}}</td>
        <td>{{$file->judul}}</td>
        <td>{{$file->isi}}</td>
        <td>{{$file->created_at}}</td>
        <td>{{$file->updated_at}}</td>
     </tr>
     @endforeach
</tbody>

And your form action should be accordingly

<form action="/upload8" method="post" enctype="multipart/form-data">
<input type="file" name="image"><br />
<input type="submit" name="submit" value="Submit">

This should work, haven't tested though. Let me know if otherwise.

这篇关于Laravel 5.2 - 将文件上传到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆