7942

Validation Error (Array to string conversion) in laravel 5.1

Question:

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)

<strong>Code:</strong>

<?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'; } } } } <blockquote>

User.php

</blockquote> 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', );

<a href="https://i.stack.imgur.com/2QOSL.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/2QOSL.png" data-original="https://i.stack.imgur.com/2QOSL.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer1:

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.

Answer2:

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(); } }

Answer3:

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

Recommend

  • How to stream zipped file (on the fly) via Play Framework 2.5 in scala?
  • Additional paypal fee on Woocommerce checkout page
  • Save new value to Laravel session array
  • How to show “regular price” instead of a “discounted price” on mini-cart (Woo)
  • How to output a euro sign in C++
  • Play Framework won't run inline javascript
  • Cant Run Default ASP.NET Web App In Visual Studio Code for Mac
  • Extending a Function1 in Scala
  • How to create a dollar amount input field in an HTML form?
  • Identity Server by leastprivilege doesn't work properly on Azure
  • JPA - getting distinct value from one column
  • php regex remove digits
  • Add delivery info to query in SAP Crystal Reports
  • Align Excel cell to center VB - xlCenter is not declared
  • Update a record where _id = :id with Mongoose
  • @Autowired for @ModelAttribute
  • Django: ORDER BY DESC on FloatField puts null values top?
  • Angular2 ag-grid datasource not working
  • Organizing large javascript files [closed]
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • is there a way to update filter with async data
  • Laravel 4 routing not working due to .htaccess file?
  • Iron Router: How do I send data to the layout?
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • how to avoid repetitive constructor in children
  • Highlight and Bold text in JTextPane
  • how to do an event when i swipe from fragment to the other
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • Authorize attributes not working in MVC 4
  • unknown Exception android
  • Busy indicator not showing up in wpf window [duplicate]
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?