2016-09-17 5 views
-1

こんにちは私はモデルのメソッドを保存する上で私のjson情報を出力する問題を抱えています。次のエラーが表示される - Response.php行のUnexpectedValueException 397: レスポンスのコンテンツは、__toString()、booleanを実装する文字列またはオブジェクトである必要があります。 節約しながら私は、モデルの検証を行い、モデルの検証方法で私はJSONを配置する必要がありますが、私はlaravel on saving model jsonが検証から復帰しました

のJavascript JSONオブジェクトの代わりにブール値を取得しています:

submit: function(e) { 

    e.preventDefault(); 

    var contact = this.model.save({ 
     firstname: this.firstname.val(), 
     lastname: this.lastname.val(), 
     company: this.company.val(), 
     email_address: this.email_address.val(), 
     description: this.description.val(), 
    }, {success:function(response){ console.log(response)}, wait: true});  

接触モデル:

public function update(Request $request, $id) { 

    $contact = Contact::find($id)->with('user')->first(); 
    $contact->firstname = $request->get('firstname'); 
    $contact->lastname = $request->get('lastname'); 
    $contact->email_address = $request->get('email_address'); 
    $contact->company = $request->get('company'); 
    $contact->description = $request->get('description');  

    return $contact->save(); //return formatted json 
} 
+0

[保存ではフォーマットされたjson文字列は返されませんが、ブール値](https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Model.html#method_save)が返されます。 – Daedalus

答えて

2

検証の実装によると、あなたは)Contactに(次の部分を変更する必要があります)ここでエラーをキャッチしていない:

// check for failure 
if ($v->fails()) 
{    
    // set errors and return false 
    // json here not return response it's always boolean true or false 
    return new JsonResponse(array('error' => true, 'errors' => $v->messages())); 
} 

このようなものに:

if ($v->fails()) {    
    $this->errors = $v->errors(); 
    return false; 
} 

nは、Controllerから、このような何かを試してみてください。

また
// If validation failed 

if(!$contact->save()) { 
    return response()->json([ 
     'error' => true, 
     'errors' => $contact->errors() 
    ]); 
} 

// Contact created if reached here... 
return response()->json(['error' => false, 'contact' => $contact]); 

、チェックAjax-Request-ValidationForm-Request-Validation(簡単で管理可能)。


注:モデルからHTTP Responseのいずれかの種類を返すようにしようとしないでください。 HTTP responseを返すことはアプリケーションロジックの一部であり、モデルはこれらを気にするべきではありません。

0

class Contact extends Model 
{ 
    protected $table = "contacts"; 

    protected $fillable = ['firstname', 'lastname', 'company', 'email_address', 'description']; 

    public static function boot() { 
     parent::boot(); 

     static::creating(function($model) { 

      return $model->validate('POST'); 
     }); 

     static::updating(function($model) { 
      return $model->validate('PUT'); 
     }); 

     static::saving(function($model) {    
      return $model->validate('PUT'); 
     }); 
    } 


    public function rules($method) 
    { 


     switch($method) 
     { 
      case 'GET': 
      case 'DELETE': 
      { 
       return []; 
      } 
      case 'POST': 
      { 
       return [ 
        'firstname' => 'required', 
        'lastname' => 'required', 
        'email_address' => 'required|email|unique:contacts,email_address', 
        'description' => 'requried'     
       ]; 
      } 
      case 'PUT': 
      case 'PATCH': 
      { 
       return [ 
        'firstname' => 'required', 
        'lastname' => 'required', 
        'email_address' => 'required|email|unique:contacts,email_address,'.$this->id, 
        'description' => 'required', 
       ]; 
      } 
      default: break; 
     } 

     return []; 
    } 

    public function messages() { 
     return [ 
      'firstname.required' => 'Please enter your first name.', 
      'lastname.required' => 'Please enter your first name.', 
      'email_address.required' => 'Please enter a email address.', 
      'email_address.email' => 'Please enter a valid email address', 
      'email_address.unique' => 'The email is not unique.', 
      'description' => 'Please enter a description.' 
     ]; 
    } 


    public function validate($method) 
    {   
     $data = $this->attributes; 

     // if($data['slug'] === '') { 
     //  // if the slug is blank, create one from title data 
     //  $data['slug'] = str_slug($data['title'], '-'); 
     // } 

     // make a new validator object 
     $v = Validator::make($data, $this->rules($method), $this->messages()); 


     // check for failure 
     if ($v->fails()) 
     {    
      // set errors and return false 
      // json here not return response it's always boolean true or false 
      return new JsonResponse(array('error' => true, 'errors' => $v->messages())); 
     } 

     // validation pass 
     return true; //new JsonResponse(array('errors'=>false)); 

    } 

    public function errors() { 
     return $this->errors; 
    } 

    public function user() { 
     return $this->hasOne('App\User', 'email', 'email_address'); 
    } 
} 

モデルを保存しますsave()はブール値を返すので、問題がないかどうかチェックしてください。

1)モデルのエラーのparamにエラーを置くためにあなたのContactモデルを変更します。

public function update(Request $request, $id) { 

    $contact = Contact::find($id)->with('user')->first(); 
    if(!$contact) { 
     return response('Contact not found', 404); 
    } 

    $contact->firstname = $request->get('firstname'); 
    $contact->lastname = $request->get('lastname'); 
    $contact->email_address = $request->get('email_address'); 
    $contact->company = $request->get('company'); 
    $contact->description = $request->get('description');  

    return $contact->save()? 
      $contact->toJson() : // returns 200 OK status with contact (json) 
      response($contact->errors, 400); // returns proper 400 Bad Request header with errors (json) in it 
} 

P.S.:あなたのコントローラで

/* if($v->fails()) remove/comment this line 
    ... 
} */ 
$this->errors = $v->errors(); 
return !$v->fails(); 

2)は、このコードを配置しますhttpステータスのリクエスタに答えるのはいいですし、業界では開発者の生活を楽にするためにすべてを作っていますので、2xxでない場合は3xxステータスso =>クライアント側アプリケーションからのリクエストはerror(ハンドラsuccess: function(response)

関連する問題