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
Then in your index.blade.php
And your form action should be accordingly
This should work, haven't tested though. Let me know if otherwise.