1

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

4
  • The way array_key_exists handles null, float, boolean, and 'integer-representing string' keys is inconsistent in itself and, in the case of bool and float, with the way these are converted when used as array offset. Commented Dec 9, 2016 at 2:18
  • @FullStack Sorry, I don't understand. What should I do? Commented Dec 9, 2016 at 2:22
  • look at this page php.net/manual/en/function.array-key-exists.php#90687 Commented Dec 9, 2016 at 2:23
  • @FullStack I still don't understand what's wrong with my ParameterBag Commented Dec 9, 2016 at 2:26

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I made a mistake. I should have put the code inside index function in LamanInformasiController to store function, so I move it. It works fine, until I select a file and click Submit, I have this error NotFoundHttpException in RouteCollection.php line 161
I have this in routes.php Route::resource('/upload8','LamanInformasiController');
If you are using Route::resource then you should follow the REST convention and handle the storing of uploaded file in the store method of your LamanInformasiController and change the form action
Can you give the right code? I don't know which part I have to change
The form action goes to wrong URL. I access the form with this URL: http://localhost/wfpProject/public/upload8. When I click submit, it brings me to this URL: http://localhost/upload8
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.