Validation Error (Array to string conversion) in l

2019-09-11 03:31发布

I'm struggling a couple of hours to fix this error but no luck please I need help about this error always says: (Array to string conversion)

Code:

<?php 
namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Hash, Auth, URL, Route, Cart, View, Paypal;
use App\Product, App\ProductBenefit, App\Country, App\Currency, App\User, App\City;

class HomeController extends BaseController {
public function postCheckoutStepPayment(Request $request){
        if(!is_null($request->input('ship_to_diff_address'))){
            $validate = Validator::make($request->all(), User::$rules);
            if($validate->fails()) { //<- problem this part
               return 'failed';
            }

        }
   }
}

User.php

public static $rules = array(
             'diff_firstname' => 'required',
             'diff_lastname'  => 'required',
             'diff_phone'     => 'required',
             'diff_countries' => 'required',
             'diff_city'      => 'required',
             'diff_state'     => 'required',
             'diff_address'   => 'required',
             );

enter image description here

3条回答
狗以群分
2楼-- · 2019-09-11 03:53

add use Redirect at the top, and

 public function postRegister(Request $request)
        {
            $v = Validator::make($request->all(), [
             'firstname' => 'required',
             'lastname'  => 'required',
             'phone'     => 'required',
             'countries' => 'required',
             'city'      => 'required',
             'state'     => 'required',
             'address'   => 'required',
            ]);

            if ($v->fails()) { 
                return redirect::to('register')
                            ->withErrors($v->messages())
                            ->withInput();
            }


        }
查看更多
甜甜的少女心
3楼-- · 2019-09-11 03:55

Add this class in you controller

use App\Http\Requests;

And Try this code in blade file.

 @if(isset($errors))
     <ul style="list-style: none;" class="alert alert-warning">
     @foreach($errors->all() as $content)
         <li>{{$content}}</li>
     @endforeach
     </ul>
 @endif
查看更多
对你真心纯属浪费
4楼-- · 2019-09-11 03:57

It appears that the value for locale in your config/app.php is an array, whereas the function loadPath in vendor/laravel/framework/src/Illuminate/Translation/FileLoader.php expects it to be a string. So I suggest you to set it's value to either 'en' or 'sv' in the config file and then later change it programmatically in your code as required.

查看更多
登录 后发表回答