从laravel的前端保存数据

时间:2018-01-09 01:55:57

标签: php laravel

我有testimonials表,我从用户那里得到评论,对于后端(管理员),一切都运行正常,我的问题是前端表单,用户(已登录或访客)可以发送他们的推荐,不知何故它没有不起作用,也没有显示任何错误,以查看哪个部分有问题。

以下是我的代码:

Routes

Route::get('/write-testimonials', 'frontend\FrontendController@testimonialcreate')->name('write-testimonials');
Route::post('/write-testimonials', 'frontend\FrontendController@testimonialcreate')->name('write-testimonialsstore');

控制器:

public function testimonialcreate()
    {
      return view('front.testimonials-create');
    }

    public function testimonialcreatestore(Request $request)
    {
      $this->validate($request, array(
          'body'=>'required',
          'user_email' => 'nullable',
          'user_name' => 'nullable',
          'image' => 'nullable|image',
          'status_id' => 'required|numeric',
          'user_id' => 'nullable|numeric',
        ));
        $testimonial = new Testimonial;

        $testimonial->body = $request->input('body');
        $testimonial->user_email = $request->input('user_email');
        $testimonial->user_name = $request->input('user_name');
        $testimonial->status_id = $request->input('status_id');
        $testimonial->user_id = $request->input('user_id');

        if ($request->hasFile('image')) {
          $image = $request->file('image');
          $filename = 'Testimonial' . '-' . time() . '.' . $image->getClientOriginalExtension();
          $location = public_path('images/');
          $request->file('image')->move($location, $filename);

          $testimonial->image = $filename;
        }

        $testimonial->save();
        return redirect()->back()
            ->with('success', 'Testimonial sent!');
    }

我的blade

<form  action="{{route('write-testimonialsstore')}}" method="POST" enctype="multipart/form-data">
    {{ csrf_field() }}

    <div class="col-md-8">
        @guest
            <div class="row">
              <div class="col-md-4">
                {{Form::label('user_email', 'Email')}}
                {{Form::text('user_email', null, array('class' => 'form-control'))}}
              </div>
              <div class="col-md-4">
                {{Form::label('user_name', 'Name')}}
                {{Form::text('user_name', null, array('class' => 'form-control'))}}
              </div>
              <div class="col-md-4">
                {{Form::label('image', 'Image')}}
                {{Form::file('image', array('class' => 'form-control'))}}
              </div>
            </div>
        @endguest

        <div class="row">
          <div class="col-md-12">
            <div class="mt-20">
              {{Form::label('body', 'Write your testimonial')}}
              {{Form::textarea('body', null, array('class' => 'form-control'))}}
            </div>
          </div>
        </div>
    </div>

    <div class="col-md-4">
        <div class="row">
            <label for="status_id" hidden>Status</label>
            <input type="hidden" name=status_id" value="1">
          @auth
            <label for="user_id" hidden>User</label>
            <input type="hidden" name="user_id" value="{{ Auth::user()->id }}" placeholder="{{ Auth::user()->username }}">
          @endauth
        </div>
    </div>


    <div class="mt-20">
        {{ Form::submit('Send', array('class' => 'btn btn-success')) }}
    </div>
</form>

任何想法?

1 个答案:

答案 0 :(得分:1)

看起来你误解了路线名称。

Route::post('/write-testimonials', 'frontend\FrontendController@testimonialcreate')->name('write-testimonialsstore');

@符号后面的名称是操作名称。发布请求必须使用其他操作名称。因此,只需将@testimonialcreate行/ {/ p>从@testimonialcreatestore更改为Route::post即可