1

Whenever I am directed to ad page and then reload same data is again entered in the database. Is there any other of redirecting of page while keeping those compact variables???

Controller.php

  public function StorePost(Request $request)

 {

    $formInput = $request->except('image');

    $this->validate($request, [
        'title' => 'required',
        'name' => 'required',
        'email' => 'required|email',
        'contact' => 'required',
        'model' => 'required',
        'city' => 'required',
        'description' => 'required',
        'image'=>'image|mimes:png,jpg,jpeg|max:10000'
    ]);  


    $image = $request->image;

    if($image){

        $imageName = $image->getClientOriginalName();
        $image->move('images',$imageName);
        $formInput['image'] = $imageName;
    }


    Post::create($formInput); 

    $posts = Post::all();  

    return view('ad.ad',compact('name','posts'));


}}
Faiez
  • 285
  • 1
  • 6
  • 20
  • Have you tried `Post::create($formInput)` in `if` condition and then execute? – Nikita Sep 20 '17 at 13:20
  • @Nikita it seems the `if` is meant to handle inclusion of image in case its present in the request. Its strange however that the validation made 'image' field compulsory then there is an if `image` again. – Oluwatobi Samuel Omisakin Sep 20 '17 at 13:25
  • Look into post-redirect-get, eg https://stackoverflow.com/questions/10827242/understanding-post-redirect-get – Don't Panic Sep 20 '17 at 13:26

3 Answers3

0

You may want to simply redirect (redirect('/ad_url')) to the 'ad' route/page. Lets say maybe 'ControllerForAddPage' is the one that displays your 'ad' view, simply instead fetch all the posts in that controller and not in the controller that handles saving your data.

0

I think it's better to use different route between store and show. you just need to redirect to index route after store success. ex:

...

Post::create($formInput); 

return redirect()->route('ad.index');
bhill77
  • 865
  • 11
  • 12
0

You should redirect to different location to avoid this. If data is successfully store then redirect it to posts list url otherwise to the same location again with error message.

Example code is follows:

public function StorePost(Request $request) {

        $formInput = $request->except('image');

        $this->validate($request, [
            'title' => 'required',
            'name' => 'required',
            'email' => 'required|email',
            'contact' => 'required',
            'model' => 'required',
            'city' => 'required',
            'description' => 'required',
            'image' => 'image|mimes:png,jpg,jpeg|max:10000'
        ]);

        $image = $request->image;

        if ($image) {
            $imageName = $image->getClientOriginalName();
            $image->move('images', $imageName);
            $formInput['image'] = $imageName;
        }


        $store = Post::create($formInput);

        if($store){
            //return to posts list view 
             return redirect('posts')->with('success', 'Post successfully stored.');
        }
        //redirect to the form to create post
        return redirect()->back()->with('failed', 'Failed to store the post!');

    }
Hasan Tareque
  • 1,761
  • 11
  • 21