How to add rule to User::create (laravel-8) - laravel

I want to add hashed password to a User::create($validation);
UserController
public function store_user(UserRoleStoreRequest $request){
$validated = $request->validated();
$password = Hash::make($validated["password"]);
$user = User::create($validated);
$roles = $validated['role_id'];
$user->roles()->sync($roles);
return redirect('/user-role');
}

Add this to the User model
public function setPasswordAttribute($value){
$this->attributes['password'] = bcrypt($value);
}

Related

Filter field returns all records when search is empty

When I don't insert anything in the search field, all the records in my table return, I don't want that to happen, how can I do so that it doesn't return any results if $search = $request->input('search'); is empty?
public function search(Request $request)
{
$search = $request->input('search');
$client = ClientModel::where('name','LIKE',"%{$search}%")->get();
return view('search', compact('client'));
}
How to solve this problem?
public function search(Request $request)
{
$search = $request->input('search');
if(!empty($search)){
$client = ClientModel::where('name','LIKE',"%{$search}%")->get();
return view('search', compact('client'));
}else{
$client = [];
return view('search', compact('client'));
}
}
public function search(Request $request)
{
$client = array();
if($request->filled('search')){
$search = $request->input('search');
$client = ClientModel::where('name','LIKE',"%{$search}%")->get();
}
return view('search', compact('client'));
}
Please use fillled method to check the data present in the GET Super Global variable if data is there then run your query otherwise return an empty array.
Thanks

Query returns an object instead of Array

I have written a model function which has to give me all the posts with user's id, but it returns a model object instead.
My model:
public static function my_posts(){
$userId = Auth::id();
$getPosts = DB::table('posts')->where('user_id',$userId)->get();
return $getPosts;
}
My Controller function:
public function myPosts(){
$getPosts = new posts_model();
$getPosts->my_posts();
return view("myposts",["posts" => $getPosts]);
}
How can I fix this error?
Can you please change Model
public static function my_posts(){
$userId = Auth::id();
$getPosts = Post::where('user_id',$userId)->get();
return $getPosts;
}
Then Change Your controller
public function myPosts(){
$getPosts = new posts_model();
$data = $getPosts->my_posts();
return view("myposts", ["posts" => $data]);
}

Laravel queue to store data

I wanna try to store data into database via database queue Laravel.
But I always get this error "Undefined offset: 0"
this is my controller :
public function store(Request $request)
{
$order = new Order;
$order->code = $request->code;
$order->created_at = $request->created_at;
$this->dispatch(new SalesOrder($order));
}
and this is my SalesOrder Jobs :
protected $order;
public function __construct(Order $order)
{
$this->order= $order;
}
public function handle()
{
$this->order->save();
}
is there something wrong in my code? Please somebody help me fix this issue. Than's anyway.
Instead of pass Order object pass in job order data, And the in job save order.
Controller code.
public function store(Request $request)
{
$data['code'] = $request->code;
$data['created_at'] = $request->created_at;
$this->dispatch(new SalesOrder($data));
}
Job code
protected $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function handle(Order $order)
{
if (!$order->craete($this->data)) {
// when not saved try again
$this->release();
}
return true;
}
Try running
$order = new Order;
$order->code = $request->code;
$order->created_at = $request->created_at;
inside your job so it looks like this
protected $order;
protected $request
public function __construct($request)
{
$this->order = new Order;
$this->request = $request;
}
public function handle()
{
$this->order->code = $request->code;
$this->order->created_at = $request->created_at;
$this->order->save();
}
and in your controller
public function store(Request $request)
{
$this->dispatch(new SalesOrder($request));
}
This should work. Did not test this am writing on my phone.

Laravel 5.4 : Property [id] does not exist on this collection instance

I have trouble saving image or fileupload in laravel 5.4.
the admin is the only one allowed to register a user together with his/her image upon registering the user should receive a confirmation email on his inbox. for other related codes that I think that is related to this controller and the email function or method that could have messed up my code.
paste bin https://pastebin.com/h3Sj7sGN?
//UserController
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Mail;
use App\Mail\verifyEmail;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$users = User::all();
//Load all users on the table and pass the users
$users = User::where(['archive'=>1])->orderBy('id')->get();
return view('usercrud.index')->with('users', $users);
}
public function create(Request $request)
{
//
if ($request->file('images') == null)
{
$file = "";
}else{
$filename = $request->images->getClientOriginalName();
$filesize = $request->images->getClientSize();
$file = $request->file('images')->storeAs('images', $filename);
$usersSt = new User;
$usersSt->filename = $filename;
$usersSt->filesize = $filesize;
$usersSt->position = $request->companyPos;
$usersSt->empid = $request->empid;
$usersSt->name = $request->fullname;
$usersSt->email = $request->email;
$usersSt->password = bcrypt($request->password);
$usersSt->roles = $request->role_id;
$usersSt->save();
}
$users = User::all();
$users = User::where(['archive'=>1])->orderBy('id')->get();
$thisUser = User::findOrFail($users->id);
$this->sendEmail($thisUser);
$thisUser = User::findOrFail($users->id);
dd($id);
$this->sendEmail($thisUser);
return redirect()->to('userIndex')->with('users', $users);
return $request->all();
}
public function verifyEmailFirst(Request $request)
{
//
return view('usercrud.verifyEmailFirst');
}
/* public function emailtoken(Request $request)
{
//
return
} */
/**
public function edit($id)
{
//
$users = User::where(['id'=>$id])->first();
// dd($users);
return view('usercrud.edit',compact('users'));
}
public function update(Request $request, $id)
{
//
$usersUp = new User;
$password = bcrypt($request->editpassword);
// dd($request->editcompanyPos);
$usersUp = User::where('id',$id)
->update(['position'=>$request->editcompanyPos,'name'=>$request-
>editfullname,'email'=>$request-
>editemail,'password'=>$password,'roles'=>$request->editrole_id]);
$users = User::all();
$users = User::where(['archive'=>1])->orderBy('id')->get();
return redirect()->to('userIndex')->with('users', $users);
}
public function destroy($id)
{
$userDel = User::where('id',$id)->update(['archive'=>0]);
$users = User::all();
$users = User::where(['archive'=>1])->orderBy('id')->get();
return redirect()->to('userIndex')->with('users', $users);
}
public function sendEmail($thisUser)
{
Mail::to($thisUser['email'])->send(new verifyEmail($thisUser));
}
public function sendEmailDone($email, $verifyToken)
{
$user = User::where(['email'=>$email, 'verifyToken'=>$verifyToken])-
>first();
//return $user;
if ($user){
return user::where(['email'=>$email, 'verifyToken'=>$verifyToken])-
>update(['status'=>'1','verifyToken'=>NULL]);
}else{
return 'user not found';
}
}
}
Do this
public function create(Request $request)
{
//
if ($request->file('images') == null)
{
$file = "";
}else{
$filename = $request->images->getClientOriginalName();
$filesize = $request->images->getClientSize();
$file = $request->file('images')->storeAs('images', $filename);
$usersSt = new User;
$usersSt->filename = $filename;
$usersSt->filesize = $filesize;
$usersSt->position = $request->companyPos;
$usersSt->empid = $request->empid;
$usersSt->name = $request->fullname;
$usersSt->email = $request->email;
$usersSt->password = bcrypt($request->password);
$usersSt->roles = $request->role_id;
$usersSt->save();
}
$this->sendEmail($usersSt);
return redirect()->to('userIndex')->with('users', $usersSt);
}

Updating Pivot Table Laravel 5.4

I'm trying to save my Pivot but nothing happened, it doesnt prompt any error I dunno where to debug cause there is no prompt, any idea ? attached here is my code.
Controller note: Email and Name is updating properly.
public function update(Request $request, User $User)
{
$user=auth()->user();
$User->name = $request->get('name');
$User->email = $request->get('email');
$User->warehouse_id = $request->get('warehouse_id');
$User->role_id = $request->get('role_id');
$User->save();
$User->roles()->updateExistingPivot($request->get('role_id'), ['role_id' => $request->get('role_id')]);
$User->warehouse()->updateExistingPivot($request->get('warehouse_id'), ['warehouse_id' => $request->get('warehouse_id')]);
return redirect()->route('user.index');
}
Model for relationship
Warehouse
class Warehouse extends Model
{
protected $fillable = ['warehouse','warehouse_description','created_by'];
public $primaryKey='id';
public function users()
{
return $this->belongsToMany(User::class);
}
}
User
public function roles()
{
return $this->belongsToMany(Role::class);
}
public function warehouse()
{
return $this->belongsToMany(Warehouse::class);
}
Role
class Role extends Model
{
protected $fillable = ['role'];
public $primaryKey='id';
public function users()
{
return $this->belongsToMany(User::class);
}
}
What I did was putting isnerting the previews id before yusing updateExistingPivot
Here is my Controller
public function update(Request $request, User $User)
{
$User->name = $request->get('name');
$User->email = $request->get('email');
$User->warehouse_id = $request->get('warehouse_id');
$User->role_id = $request->get('role_id');
$User->roles()->updateExistingPivot($User->role_id, ['role_id' => $request->get('role_id')]);
$User->warehouse()->updateExistingPivot($User->warehouse_id, ['warehouse_id' => $request->get('warehouse_id')]);
$User->save();
return redirect()->route('user.index');
}

Resources