在laravel 5中从空值创建默认对象

时间:2017-08-21 09:56:39

标签: laravel-5

我正在尝试在控制器中创建具有后期操作且位于同一页面中的两个函数。 我的控制器

public function store(Request $request)
{


    $status = DB::table('analytics')->where('dienstleistung', '!=', '')->get();
    //Save data
        $rules = [          
        'site_id'       => 'required',
        'dienstleistung'        => 'required',
        'objekt'        => 'required',
        'zimmer'        => 'required',
        'vorname'       => 'required',
        'name'      => 'required',
        'strasse'       => 'required',
        'ort'       => 'required',
        'plz'       => 'required',
        'tel'       => 'required',
        'email'     => 'required|email',
        'reinigungstermin'      => 'required',
        'gekommen'      => 'required',
        'message'       => 'required',
        'status'        => 'required',
        'answer'        => 'required',
        'notiz'         => 'required',
        'userId'        => 'required',
                ];

                $validator = Validator::make(Input::all(), $rules);

                if($validator->fails()) {
                    return Redirect::to('anfrage')
                        ->withErrors($validator)
                        ->withInput();
                }
                else {
                    $anfrage = new Analytic();                      
                    $anfrage->site_id = Input::get('site_id');
                    $anfrage->dienstleistung = Input::get('dienstleistung');
                    $anfrage->objekt = Input::get('objekt');
                    $anfrage->zimmer = Input::get('zimmer');
                    $anfrage->vorname = Input::get('vorname');
                    $anfrage->name = Input::get('name');
                    $anfrage->strasse = Input::get('strasse');
                    $anfrage->ort = Input::get('ort');
                    $anfrage->plz = Input::get('plz');
                    $anfrage->tel = Input::get('tel');
                    $anfrage->email = Input::get('email');
                    $anfrage->reinigungstermin = Input::get('reinigungstermin');
                    $anfrage->gekommen = Input::get('gekommen');
                    $anfrage->message = Input::get('message');
                    $anfrage->status = Input::get('status');
                    $anfrage->answer = Input::get('answer');
                    $anfrage->notiz = Input::get('notiz');
                    $anfrage->userId = Input::get('userId');
                    try {

                        $anfrage->save();
                        flash()->success(trans('app.success'));
                        return Redirect::to('anfrage');

                    } catch (\Exception $e) {
                        Log::writeException($e);
                        return Redirect::to('anfrage')
                            ->withErrors($e->getMessage())
                            ->withInput();
                    }

                }

}
 public function editItem(Request $request) {
    $anfrages = Analytic::find($request['id']  );

        $anfrages->status = $request->status;
    $anfrages->answer = $request->answer;
    $anfrages->notiz = $request->notiz;
     $anfrages->save();
    return response ()->json( $anfrages );
}

我的路线:

Route::post('anfrage', 'AnfrageController@store');
Route::post ( 'anfrage', 'AnfrageController@editItem' );

EditItem功能没问题,它在我想编辑数据时进行更改,但是当我想存储数据时,显示的信息是:
从空值创建默认对象

所以,我只需要激活其中一个功能,两个都不起作用。

0 个答案:

没有答案