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.
Related
I'm trying to redirect with a message to a view ( notification ) after the form is successfully validated / stored and emailed.
Everything Works as expected but unable to redirect after the email is sent.
Tried redirecting after form validation and it works. But if i call the store() and try to redirect after save() it does not redirect.
I though maybe something was wrong with my store() function so i created an empty function test() and tried redirecting if like this
public function test($request){
Session::flash('message', 'alert-danger');
return Redirect::to('/notifications');
}
public function test(){
Session::flash('message', 'alert-danger');
return Redirect::to('/notifications');
}
It still does not work.
but if redirect from validator() it works fine.
I don't know what is wrong. Please help.
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\Mail\RegistrationMail;
use Illuminate\Support\Facades\Mail;
use App\Register;
use Illuminate\Support\Facades\URL;
use Session;
use Illuminate\Support\Facades\Redirect;
class RegisterController extends Controller
{
protected function store(Request $request){
$length = 16;
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
$user = new Register();
$user->fname = request('fname');
$user->mname = request('mname');
$user->lname = request('lname');
$user->email = request('email');
$user->kathum_tui = $code;
$user->created = \Carbon::now();
$vfname = request('fname');
$vlname = request('lname');
$vemail = request('email');
$url = URL::temporarySignedRoute('verify', now()->addMinutes(1500), ['fname' => $vfname ,'lname'=> $vlname , 'email'=> $vemail]);
$user->save();
Mail::to($user['email'])->send(new RegistrationMail($user,$url));
Session::flash('message', 'alert-danger');
return Redirect::to('/notifications');
}
protected function validator(Request $request)
{
$validator = Validator::make($request->all(),[
'email' => ['required', 'string', 'email', 'max:255', 'unique:registers'],
]);
if ($validator->fails()) {
$request->validate( [
'fname' => ['required', 'string', 'max:255'],
'mname' => ['nullable','string', 'max:255'],
'lname' => ['required', 'string', 'max:255'],
],
[
'fname.required' => 'First Name Cannot be empty!',
'lname.required' => 'Last Name Cannot be empty!',
'email.unique' => 'You have not completed the Registration. Please check your email to complete the Registration.',
]
);
}else{
$request->validate( [
'fname' => ['required', 'string', 'max:255'],
'mname' => ['nullable','string', 'max:255'],
'lname' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:registers'],
],
[
'fname.required' => 'First Name Cannot be empty!',
'lname.required' => 'Last Name Cannot be empty!',
'email.unique' => 'An account already Exist with this email. Forgot Password',
]
);
$this->store($request);
}
}
}
You should return your store method call in the validator function like as following:
return $this->store($request);
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',
];
}
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');
}
}
}
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