php – Uploading picture to database in laravel-ThrowExceptions

Exception or error:

I have created a form where a user can add news, and i want them to be able to upload a picture as well. I have tried too read what other users create a upload form, but cant understand it.
The picture that gets uploaded needs to store the picture path and all the other information in the database that i have created.
Reason for this is that i want to later display the news and picture that the user created, so it can be viewed.
So i need some help, since i don’t quite understand how to do this and I am a beginner in laravel. I don’t want to use any .js or other scripts since i want to learn these basics first.

My database:

class NyhetsmodulTable extends Migration {


    public function up()
    {
        Schema::create('news', function($table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('author');
            $table->string('message');
            $table->boolean('active');
            $table->timestamps();
            $table->string('picture_path');
        });
    }

My create.plade.php file (not sure if my file form is right):

{{ Form::open(array('route' => 'adminpanel.newsmodule.store', 'files' => true)) }}
<ul>
    <li>
        {{ Form:: label ('title', 'Title: ' )}}
        {{ Form:: text ('title')}}
    </li>
    <li>
        {{ Form:: label ('author', 'Author: ' )}}
        {{ Form:: text ('author')}}
    </li>
    <li>
        {{ Form:: label ('message', 'News: ' )}}
        {{ Form:: textarea ('message') }}
    </li>
    <li>
        {{ Form::file('image')}}
    </li>
    <li>

    </li>
    <li>
        {{ Form::submit('Submit') }}
    </li>
</ul>

My controller, that i dont understand how to set up:

public function uploadFile()
{

}

My route:

Route::post('adminpanel/newsmodule/create',
    [
        'uses' => 'NyhetsController@uploadFile',
        'as' => 'adminpanel.newsmodule.upload'
    ]
);
How to solve:

Form open tag should be

{{ Form::open(array('url' => 'adminpanel.newsmodule.create', 'files' => true)) }}

in first line,
there is no need to redeclare form inside form
files should be set true, so it can send files

and to Store image,

$news = News::find(1)
$news->image = Input::file('image');
$news->save();

since i thougt u just save path,

$pathToFile = '/foo/bar/baz.jpg
Image::make(Input::file('image')->save($pathToFile);
$news->picture_path = $pathToFile;
$news->save();

Leave a Reply

Your email address will not be published. Required fields are marked *