Laravel uses ajax to submit this form. How should the controller code be written?
phpcn_u1582
phpcn_u1582 2017-05-16 16:54:07
0
2
470

Use ajax to submit a set of forms, which contains an image.
Originally, it could be successfully saved to the database when submitted without ajax.
However, because the image needs to be compressed before uploading, it was changed to ajax submission, and the ajax request was successful. Sent, but cannot be saved to the database,
I think it may be that the code in the controller needs to be modified, but I don’t know how to change it. The code is below.
ps: The localResizeIMG plug-in is used to compress images. https://github.com/think2011/localResizeIMG

The following is the view of submitting the form using ajax:

        

提交

//用了localResizeIMG插件,用于上传图片前先压缩。https://github.com/think2011/localResizeIMG

The following is the original controller when ajax was not used. It can be successfully saved to the database. Now I don’t know how to change it.

public function store(Requests\StoreArticleRequest $request) { $article = new Article($request->except('photo')); $article -> user_id = \Auth::id(); $file = $request->file('photo'); $destinationPath = 'uploads/'; $extension = $file->getClientOriginalExtension(); $fileName = \Auth::user()->id . '_' . time() . '.' . $extension; $file->move($destinationPath, $fileName); $article -> photo = '/'.$destinationPath.$fileName; $article->save(); return redirect()->action('ArticleController@show', ['id' => $article->id]); }

The following is StoreArticleRequest

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

reply all (2)
巴扎黑

save前你先dd$articleTake a look at the data

    曾经蜡笔没有小新

    Don’t forget csrf and ajax when submitting.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!