Laravel 5.2 - 将文件上传到数据库

时间:2016-12-09 00:49:24

标签: php database laravel file-upload laravel-5.2

这些就是我所拥有的:

名为lamanInformasi的数据库表,其中包含以下字段:idjudulisicreated_atupdated_at

这就是我想要的:

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

这些是我的代码:

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>

当我运行它时,我有这个错误:

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

我在ParameterBag line 89-91

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

这些是我的问题:

如何修复该错误?我是否正确编写了上传文件的代码?因为我尝试过类似的代码,但它不起作用。感谢

1 个答案:

答案 0 :(得分:0)

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

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

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

然后在 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>

你的表格行动应该相应

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

这应该可行,但尚未测试过。如果不是,请告诉我。