Laravel 5.2 - Upload Files to Database

2019-07-26 12:03发布

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

1条回答
闹够了就滚
2楼-- · 2019-07-26 12:15

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.

查看更多
登录 后发表回答