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',
);
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.
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();
}
}
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
Related
I want to add the user while adding a vendor, but it shows me error. I have created the object of class User. This is my code:
<?php
namespace App\Http\Controllers;
use Session;
use App\Vendors;
use App\User;
use App\Category;
use App\Location;
use Illuminate\Http\Request;
class VendorsController extends Controller
{
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:2|max:20',
'email' => 'required|email|unique:users,email'
]);
$data = array(
'name' => $request->name,
'email' => $request->email,
'role' => 'Vendor'
);
$this->user->fill($data);
$this->user->save();
$professional = Professional::create([
'user_id' => $this->user,
'contact_number' => $request->contact_number,
'address' => $request->address,
'about_us' => $request->about_us,
'category_id' => $request->category_id
]);
return redirect()->back();
}
}
But I am getting this error:
Undefined property: App\Http\Controllers\ProfessionalsController::$user
Any help will be appreciated.
I am trying to do registration of multi-user in my registration controller and used the following code and checked. But it shows me error:
Method App\Http\Controllers\Auth\RegisterController::validator does not exist.
<?php
namespace App\Http\Controllers\Auth;
use App\Admin;
use App\Manager;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
use RegistersUsers;
public function __construct()
{
$this->middleware('guest');
$this->middleware('guest:admin');
$this->middleware('guest:manager');
}
protected function createAdmin(Request $request)
{
$this->validator($request->all())->validate();
$admin = Admin::create([
'name' => $request['name'],
'email' => $request['email'],
'password' => Hash::make($request['password']),
]);
return redirect()->intended('login/admin');
}
protected function createManager(Request $request)
{
$this->validator($request->all())->validate();
$manager = Manager::create([
'name' => $request['name'],
'email' => $request['email'],
'password' => Hash::make($request['password']),
]);
return redirect()->intended('login/manager');
}
}
Try to add validator() function like follow:
protected function validator(array $data, $table)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:'.$table],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
then you can make validation like so:
$this->validator($request->all(), 'table_name')->validate();
change table_name with corresponding name.
I use laravel 5.6
I use https://laravel.com/docs/5.6/validation#form-request-validation to validation server side
My controller like this :
<?php
....
use App\Http\Requests\UserUpdateRequest;
class UserController extends Controller
{
...
public function update(UserUpdateRequest $request)
{
// dd($request->all());
}
}
Before run statement in the update method, it will call UserUpdateRequest to validation server side
The validation like this :
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserUpdateRequest extends FormRequest
{
....
public function rules()
{
dd($this->request->all());
return [
'name' => 'required|max:50',
'gender' => 'required',
'birth_date' => 'required',
'address' => 'required',
'status' => 'required'
];
}
}
The result of dd($this->request->all()); like this :
Array
(
[selected_data] => {"name":"agis","gender":"2","birth_date":"2018-03-13","address":"london"}
)
How can I validation if the data is object array like that?
You can use a dot notation like so:
public function rules()
{
return [
'selected_data.name' => 'required|max:50',
'selected_data.gender' => 'required',
'selected_data.birth_date' => 'required',
'selected_data.address' => 'required',
'selected_data.status' => 'required',
];
}
Read more about it here: Validating Array.
Hope this helps.
I would add the * because if you pass multiple objects you need to verify them all.
So like this:
public function rules()
{
return [
'selected_data.*.name' => 'required|max:50',
'selected_data.*.gender' => 'required',
'selected_data.*.birth_date' => 'required',
'selected_data.*.address' => 'required',
'selected_data.*.status' => 'required',
];
}
Laravel Framework 5.4.35
Contacts Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactEmail;
class ContactsController extends Controller
{
public function index() {
return view('contact.index');
}
public function sendContact (Request $request) {
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'message' => 'required|min:5',
]);
Mail::to('bump#bumpy.net')
->send(new ContactEmail($request));
return redirect('/contact/success');
}
public function success() {
return view('contact.success');
}
}
The Controller that extends:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
When it goes here:
$request->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'message' => 'required|min:5',
]);
I get this output:
(1/1) BadMethodCallException Method validate does not exist
I have the namespace, the classes to be used. The call to the method seems to be ok.
What am I missing?
Care to advise?
If I create a validator instance manually using the Validator facade.
It seems to validate.
You mention your using version 5.4. The method you're using to validats via the request is only from version 5.5.
So you will need to do it like...
$this->validate($request, [
'name' => 'required|min:3',
'email' => 'required|email',
'message' => 'required|min:5',
]);
Hope this helps. Check out the 5.4v docs rather than the, aster/5.5v
https://laravel.com/docs/5.4/validation#validation-quickstart
Laravel 5.4
$this->validate($request, [
Laravel 5.5
$request->validate([
when I use validation in my controller or in a separate request file to validate request of form , it just redirect to itself. But when i remove the validation form just normally update the information please help me.
Here is the route:
Route::group(['prefix' => '{university}'], function() {
Route::Resource('instructor', 'university\univInstructorController');
});
Controller:
<?php
namespace App\Http\Controllers\university;
use App\Http\Requests\university\instructor\instructorUpProfileReq;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\instructor;
use App\university;
use Illuminate\Support\Facades\Redirect;
class univInstructorController extends Controller
{
public function update($university,$id, Request $request)
{
if($universities=university::whereuniversityName($university)->first()){
if($instructors = instructor::find($id)){
$this->validate($request, [
'name' => 'required|alpha|min:3',
'lName' => 'required|alpha|min:3',
'gender' => 'required|alpha',
'birth_date' => 'date',
'degree_type' => 'required|alpha',
'academic_degree' => 'required|alpha',
'phone' => 'max:15',
'address' => 'alpha_dash|min:4',
]);
$input = $request->all();
$instructors->update($input);
return Redirect::action('university\univInstructorController#show',['universities'=>$university,'id'=>$id]);
}
else{
return view('errors.404');
}
}
else{
return view('errors.404');
}
}
}