Seif5's avatar

Laravel 5 image Upload error

Hello I want to upload image on my laravel project , so when I submit the request I get this error ErrorException in BlogRepository.php line 55: Argument 1 passed to App\Repositories\BlogRepository::savePost() must be an instance of Illuminate\Http\Request, instance of App\Models\Post given, called in /home/theaccidentguys/public_html/laravel/app/Repositories/BlogRepository.php on line 285 and defined

BlogRepository.php

    private function savePost(Request $request, $post, $inputs, $user_id = null)
    {
        $post->title = $inputs['title'];
        $post->summary = $inputs['summary'];
        $post->content = $inputs['content'];
        $post->slug = $inputs['slug'];
        $post->active = isset($inputs['active']);
        if ($user_id) {
            $post->user_id = $user_id;
        }
        $post->save();
if(Input::hasFile('image')){
        $file = Input::file('image');
        $imageName = $post->id . '.' . 
        $request->file('image')->getClientOriginalExtension();
        $post->path_file = $imageName ;
        $request->file('image')->move(base_path() . '/public/images/', $imageName);
                    $post->save();

}

        return $post;
    }

View.php

    {!! Form::open(['url' => 'blog','files' => true, 'method' => 'post', 'class' => 'form-horizontal panel']) !!}   

          <label>Select image to upload:</label>
            <input type="file" name="image" id="image">
            <input type="hidden" name="_token" value="{!! csrf_token() !!}">
            </br>
            <center>
                          <input class="btn btn-primary" type="submit" value="Upload" name="submit">

            </center>

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

        {!! Form::submit(trans('front/form.send')) !!}

        {!! Form::close() !!}

Cany Any one help me please

0 likes
4 replies
ChristopherSFSD's avatar

You might be thinking that Laravel will automagically inject the Request into your savePost method, but I'm pretty sure it does not do that for you.

1 like
Seif5's avatar

@ChristopherRaymond Thank you for answer Routes.php

Route::resource('blog', 'BlogController');

BlogController.php

<?php namespace App\Http\Controllers;

use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use App\Http\Requests\PostRequest;
use App\Http\Requests\SearchRequest;
use App\Repositories\BlogRepository;
use App\Repositories\UserRepository;

class BlogController extends Controller {

    /**
     * The BlogRepository instance.
     *
     * @var App\Repositories\BlogRepository
     */
    protected $blog_gestion;

    /**
     * The UserRepository instance.
     *
     * @var App\Repositories\UserRepository
     */
    protected $user_gestion;

    /**
     * The pagination number.
     *
     * @var int
     */
    protected $nbrPages;

    /**
     * Create a new BlogController instance.
     *
     * @param  App\Repositories\BlogRepository $blog_gestion
     * @param  App\Repositories\UserRepository $user_gestion
     * @return void
    */
    public function __construct(
        BlogRepository $blog_gestion,
        UserRepository $user_gestion)
    {
        $this->user_gestion = $user_gestion;
        $this->blog_gestion = $blog_gestion;
        $this->nbrPages = 6;

        $this->middleware('redac', ['except' => ['indexFront', 'show', 'tag', 'search']]);
        $this->middleware('admin', ['only' => 'updateSeen']);
        $this->middleware('ajax', ['only' => ['updateSeen', 'updateActive']]);
    }   

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function indexFront()
    {
        $posts = $this->blog_gestion->indexFront($this->nbrPages);
        $links = $posts->render();

        return view('front.blog.index', compact('posts', 'links'));
    }

    /**
     * Display a listing of the resource.
     *
     * @return Redirection
     */
    public function index()
    {
        return redirect(route('blog.order', [
            'name' => 'posts.created_at',
            'sens' => 'asc'
        ]));
    }

    /**
     * Display a listing of the resource.
     *
     * @param  Illuminate\Http\Request $request
     * @return Response
     */
    public function indexOrder(Request $request)
    {
        $statut = $this->user_gestion->getStatut();
        $posts = $this->blog_gestion->index(
            10, 
            $statut == 'admin' ? null : $request->user()->id,
            $request->name,
            $request->sens
        );
        
        $links = $posts->appends([
                'name' => $request->name, 
                'sens' => $request->sens
            ]);

        if($request->ajax()) {
            return response()->json([
                'view' => view('back.blog.table', compact('statut', 'posts'))->render(), 
                'links' => e($links->setPath('order')->render())
            ]);     
        }

        $links->setPath('')->render();

        $order = (object)[
            'name' => $request->name, 
            'sens' => 'sort-' . $request->sens          
        ];

        return view('back.blog.index', compact('posts', 'links', 'order'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $url = config('medias.url');
        
        return view('back.blog.create')->with(compact('url'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  App\Http\Requests\PostRequest $request
     * @return Response
     */
    public function store(PostRequest $request)
    {
        $this->blog_gestion->store($request->all(), $request->user()->id);

        return redirect('blog')->with('ok', trans('back/blog.stored'));
    }

    /**
     * Display the specified resource.
     *
     * @param  Illuminate\Contracts\Auth\Guard $auth     
     * @param  string $slug
     * @return Response
     */
    public function show(
        Guard $auth, 
        $slug)
    {
        $user = $auth->user();

        return view('front.blog.show',  array_merge($this->blog_gestion->show($slug), compact('user')));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  App\Repositories\UserRepository $user_gestion
     * @param  int  $id
     * @return Response
     */
    public function edit(
        UserRepository $user_gestion, 
        $id)
    {
        $post = $this->blog_gestion->getByIdWithTags($id);

        $this->authorize('change', $post);

        $url = config('medias.url');

        return view('back.blog.edit',  array_merge($this->blog_gestion->edit($post), compact('url')));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  App\Http\Requests\PostUpdateRequest $request
     * @param  int  $id
     * @return Response
     */
    public function update(
        PostRequest $request,
        $id)
    {
        $post = $this->blog_gestion->getById($id);

        $this->authorize('change', $post);

        $this->blog_gestion->update($request->all(), $post);

        return redirect('blog')->with('ok', trans('back/blog.updated'));        
    }

    /**
     * Update "vu" for the specified resource in storage.
     *
     * @param  Illuminate\Http\Request $request
     * @param  int  $id
     * @return Response
     */
    public function updateSeen(
        Request $request, 
        $id)
    {
        $this->blog_gestion->updateSeen($request->all(), $id);

        return response()->json();
    }

    /**
     * Update "active" for the specified resource in storage.
     *
     * @param  Illuminate\Http\Request $request
     * @param  int  $id
     * @return Response
     */
    public function updateActive(
        Request $request, 
        $id)
    {
        $post = $this->blog_gestion->getById($id);

        $this->authorize('change', $post);
        
        $this->blog_gestion->updateActive($request->all(), $id);

        return response()->json();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $post = $this->blog_gestion->getById($id);

        $this->authorize('change', $post);

        $this->blog_gestion->destroy($post);

        return redirect('blog')->with('ok', trans('back/blog.destroyed'));      
    }

    /**
     * Get tagged posts
     * 
     * @param  Illuminate\Http\Request $request
     * @return Response
     */
    public function tag(Request $request)
    {
        $tag = $request->input('tag');
        $posts = $this->blog_gestion->indexTag($this->nbrPages, $tag);
        $links = $posts->appends(compact('tag'))->render();
        $info = trans('front/blog.info-tag') . '<strong>' . $this->blog_gestion->getTagById($tag) . '</strong>';
        
        return view('front.blog.index', compact('posts', 'links', 'info'));
    }

    /**
     * Find search in blog
     *
     * @param  App\Http\Requests\SearchRequest $request
     * @return Response
     */
    public function search(SearchRequest $request)
    {
        $search = $request->input('search');
        $posts = $this->blog_gestion->search($this->nbrPages, $search);
        $links = $posts->appends(compact('search'))->render();
        $info = trans('front/blog.info-search') . '<strong>' . $search . '</strong>';
        
        return view('front.blog.index', compact('posts', 'links', 'info'));
    }

}

Please or to participate in this conversation.