blackpearl's avatar

Upload Image

Hello, i have a little problem, I create a simple blog systems where i create a news but i want to add image yet. I tried to do it but doesn't work.

Can someone help me ?

Its my post controller

public function store(PostFormRequest $request)
    {   
        $post= new Post(array(
            'title' => $request->get('title'),
            'content' => $request->get('content'),
            'slug' => Str::slug($request->get('title'),'-'),
        ));

        $post->save();
        $post->categories()->sync($request->get('categories'));

        return redirect('/admin/posts/create')->with('status', 'The post has been created');
    }

It's my create view

@extends('master')
@section('title', 'Create a Post')
@section('content')


    <div class="container col-md-8 col-md-offset-2">
        <div class="well well bs-component">

            <form class="form-horizontal" method="post" files="true">

                @foreach( $errors->all() as $error)
                    <p class="alert alert-danger">{{ $error }}</p>
                @endforeach

                @if(session('status'))
                    <div class="alert alert-success">{{ session('status')}}</div>
                @endif

                <input type="hidden" name="_token" value="{!! csrf_token() !!}">

                <fieldset>
                    <legend>Create a new Post</legend>
                    <div class="form-group">
                        <label for="title" class="col-lg-2 control-label">Title</label>
                        <div class="col-lg-10">
                            <input type="title" class="form-control" id="title" placeholder="Title" name="title">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="content" class="col-lg-2 control-label">Content</label>
                        <div class="col-lg-10">
                            <textarea class="form-control" row="3" id="content" name="content"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="select" class="col-lg-2 control-label">Categories</label>
                        <div class="col-lg-10">
                            <select class="form-control" id="categories" name="categories[]" multiple>
                                @foreach ($categories as $category)
                                    <option value="{!! $category->id !!}">{!! $category->name !!}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                    <label for="image" class="col-lg-2 control-label">Image</label>
                    <div class="col-lg-10">
                    {!! Form::file('image',['class' => 'form-control'])!!}
                    </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-10 col-lg-offset-2">
                            <button type="reset" class="btn btn-default">Cancel</button>
                            <button type="submit" class="btn btn-primary">Submit</button>
                        </div>
                    </div>
                </fieldset>
            </form>
        </div>
    </div>
      <script>
      $(function() {
          $('#content').editable({inlineMode: false})
      });
  </script>
@endsection

My PostFormRequest

public function rules()
    {
        return [
            'title' => 'required',
            'content' => 'required',
            'categories' => 'required',
            'image' => 'required',
        ];
    }

I created 'Image' field in my database to store path to my image.

0 likes
3 replies
christopher's avatar

Of course you have to fetch also the image

http://laravel.com/docs/5.0/requests#files

if( $request->hasFile('image') ) {
        $file = $request->file('image');
        // Get the Image Name with the File Extension ( e.g .jpg / .png )
        $fileName = $file->getClientOriginalName().'.'.$file->getClientOriginalExtension();
        // Set the Filepath 
        $path = 'uploads/'.$fileName;
        // Move the file to the upload Folder
        $file = $file->move($path, $fileName);
    }
  new Post(array(
            'title' => $request->get('title'),
            'content' => $request->get('content'),
            'image' => $path;
            'slug' => Str::slug($request->get('title'),'-'),
      ));
blackpearl's avatar
public function store(PostFormRequest $request)
    {   
        $path = '';
        if( $request->hasFile('image') ) {
        $file = $request->file('image');
        // Get the Image Name
        $fileName = $file->getClientOriginalName().'.'.$file->getClientOriginalExtension();
        // Set the Filepath 
        $path = 'uploads/'.$fileName;
        // Move the file to the upload Folder
        $file = $file->move($path, $fileName);
        dd($path);
    }
        $post= new Post(array(
            'title' => $request->get('title'),
            'content' => $request->get('content'),
            'image' => $path,
            'slug' => Str::slug($request->get('title'),'-'),


        ));
        $post->save();
        $post->categories()->sync($request->get('categories'));

        return redirect('/admin/posts/create')->with('status', 'The post has been created');

News has been added but without saving path in database

freekmurze's avatar

I have made a package that does all the hard work for you. Once installed you can do this:

$post->addMedia($request->file('image'))->toCollection('images');

You can display the image in your blade view with:

<img src="{{ $news->getFirstMediaUrl('images') }}">

If you want a thumbnail of that image:

<img src="{{ $news->getFirstMediaUrl('images', 'thumb') }}">

The full documentation: https://github.com/spatie/laravel-medialibrary

Please or to participate in this conversation.