When user sign up he get free amount of 100, amount is present in the "USERS" table. I want that a user cannot withdraw (amount in "Withdraw" table), if he has no record in the "Fund" table.
I tried the following in the withdraw controller. If the "Fund" table holds a record, it will return the view, otherwise it will redirect to login.
Is this right?
public function newWithdraw()
{
$fund = Fund::where('user_id', Auth::user()->id)->first();
if ($fund==null)
{
Session::flash('type','danger');
Session::flash('message','please add funds atleast once');
return redirect()->route('login');
}
else
$data['general'] = GeneralSetting::first();
$data['site_title'] = $data['general']->title;
$data['basic'] = BasicSetting::first();
$data['page_title'] = "User Withdraw Method";
$data['method'] = ManualPayment::whereStatus(1)->get();
return view('withdraw.withdraw-new',$data);
}
Use empty() to check Fund have a record or not.
public function noamount()
{
$fund = Fund::where('user_id', Auth::user()->id)->first();
if (empty($fund)) {
return redirect()->back()->with('type','danger')->with('message','please add funds atleast once');
}
}
Related
I have column total leave in table user and column days taken and status in table leave. I tried to deduct the total leave with the leave days taken when the leave is approved but it is not updated. I've tried with this code.
This function is for the applicant to apply for leave.
public function store(Request $request){
$dateFrom = Carbon::createFromFormat('Y-m-d', $request->date_from);
$dateTo = Carbon::createFromFormat('Y-m-d', $request->date_to);
$days_taken = $dateFrom->diffInDays($dateTo)+1;
if($request->leave_id == 1){
if($days_taken > auth()->user()->total_annual){
return redirect()->route('admin.leaverequest.create')->with('error', 'Leave exceed');
}
}
else{
return redirect()->route('admin.leaverequest.index')
->with('success', 'Leave Application Submitted');
}
Application::create([
'user_id'=>auth()->user()->id,
'leave_id'=>$request->leave_id,
'date_from'=>$request->date_from,
'date_to'=>$request->date_to,
'days_taken'=> $dateFrom->diffInDays($dateTo)+1,
]);
return redirect()->route('admin.leaverequest.index')
->with('success', 'Leave Application Submitted');
}
This is the function for approve/rejecting the leave request.
public function approval(Request $request, $id){
$leaverequest = Application::find($id);
if($leaverequest->status = $request->approve){
$leaverequest->save();
return redirect()->back();
}
elseif($leaverequest->status = $request->reject){
$leaverequest->save();
return redirect()->back();
}
}
This function is for the calculation to deduct the total leave everytime if the leave request is approved. If not, the total leave will stay as it is.
public function leaveBalance(Request $request, $id){
$leaverequest = Application::all();
$days_taken = $request->days_taken;
$annual_balance = User::find($id);
if($request->status == 1){
$annual_balance->total_annual = $annual_balance->total_annual - $days_taken;
$annual_balance->save();
return redirect()->route('home');
}
}
The other functions seems to function well except for the leaveBalance function. I've tried using this code but also didn't work.
auth()->user()->total_annual = auth()->user(->total_annual - $days_taken;
auth()->user()->save();
First of code looks fine , but if you getting error or not updating record then i suggests to debug your code first that all variables have values like $annual_balance = User::find($id); did you get data in this? and if you get all things then try to store $annual_balance->total_annual in one variable and then subtract code from variable.
function leaveBalance(Request $request, $id){
$leaverequest = Application::all();
$days_taken = $request->days_taken;
$annual_balance = User::find($id);
if($request->status == 1){
$total_annual = $annual_balance->total_annual;
$annual_balance->total_annual = $total_annual - $days_taken;
$annual_balance->save();
return redirect()->route('home');
}
}
// Or you can try using DB if above not work don't forget to add Use DB on top of Controller.
$affected = DB::table('users')
->where('id', $id)
->update(['total_annual' => $total_annual]);
You could also try this
I would say return a single total_annual (total_annual refers to the total leave field in the users table) for a certain user from the user table and note the keyword value() in the query which indicates one value.
$annual_balance variable store a total annual leave and $days_taken store the number of leave days taken and do some calculation as below to set the total annual which is the remaining total annual leave after days taken
$annual_balance->total_annual = $annual_balance - $days_taken;
public function leaveBalance(Request $request, $id){
$days_taken = $request->days_taken;
$annual_balance = User::where('user_id', $id)->select('total_annual')->value('total_annual');
if($request->status == 1)
{
$annual_balance->total_annual = $annual_balance - $days_taken;
$annual_balance->save();
return redirect()->route('home');
}
}
In the CakePHP 3 Blog Tutorial, users are conditionally authorized to use actions like edit and delete based on ownership with the following code:
public function isAuthorized($user)
{
// All registered users can add articles
if ($this->request->getParam('action') === 'add') {
return true;
}
// The owner of an article can edit and delete it
if (in_array($this->request->getParam('action'), ['edit', 'delete'])) {
$articleId = (int)$this->request->getParam('pass.0');
if ($this->Articles->isOwnedBy($articleId, $user['id'])) {
return true;
}
}
return parent::isAuthorized($user);
}
public function isOwnedBy($articleId, $userId)
{
return $this->exists(['id' => $articleId, 'user_id' => $userId]);
}
I've been attempting to implement something similar for my own tables. For example, I have a Payments table, which is linked to Users through several different tables as follows:
Users->Customers->Bookings->Payments.
Foreign keys for each:
user_id in Customers table = Users->id (User hasOne Customer)
customer_id in Bookings table = Customers->id (Customer hasMany Bookings)
booking_id in Payments table = Bookings->id(Booking hasMany Payments)
My AppController's initialize function:
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth',[
'authorize' => 'Controller',
]);
$this->Auth->allow(['display']); //primarily for PagesController, all other actions across the various controllers deny access by default
}
In my PaymentsController, I have the following
public function initialize()
{
parent::initialize();
}
public function isAuthorized($user)
{
if (in_array($this->request->action,['view', 'edit', 'index', 'add']
return (bool)($user['role_id'] === 1); //admin functions
}
if (in_array($this->request->action,['cart'])) {
return (bool)($user['role_id'] === 2) //customer function
}
if (in_array($this->request->action, ['cart'])) {
$bookingId = (int)$this->request->getParam('pass.0');
if ($this->Payments->isOwnedBy($bookingId, $user['id'])) {
return true;
}
}
return parent::isAuthorized($user);
}
public function isOwnedBy($bookingId, $userId)
{
return $this->exists(['id' => $bookingId, 'user_id' => $userId]);
}
I'm unsure as to how to link through the different tables to determine ownership.
Currently if a customer who is paying for Booking #123 could just change the URL so they are paying for Booking #111, provided that Booking exists in the database.
Additionally, the Booking ID is passed to the Cart function (since customers are paying for a specific booking). For example: If customer is paying for Booking #123, then the URL = localhost/project/payments/cart/123. Upon submitting their cart, a new Payment entry is created.
Also, regarding the getParam and isOwnedBy methods, hovering over them in my editor shows this:
Method 'getParam' not found in \Cake\Network\Request
Method 'isOwnedBy' not found in App\Model\Table\PaymentsTable
However, I've gone through the entire BlogTutorial and can't find anywhere else that getParam or isOwnedBy is used or set up in the Model.
In the IsAuthorized function in PaymentsController:
if (in_array($this->request->action, ['cart'])) {
$id = $this->request->getParam('pass'); //use $this->request->param('pass') for CakePHP 3.3.x and below.
$booking = $this->Payments->Bookings->get($id,[
'contain' => ['Artists']
]);
if ($booking->artist->user_id == $user['id']) {
return true;
}
}
I am trying to create an application with which users can enrol on to a course. To enrol a user has to be approved by the admin. A user will select a course by clicking on a course,then the course ID and the user ID should get saved in a database. However I can't get the user ID to save in a database. I have added the user ID as a part of the session. So how can I save the user ID which is now in the session?
Here is the model:
public function select_course()
{
$data['users_id']=$this->input->post($this->session->userdata('users_id'));
$query =$this->db->insert('gp_approval_course',$data);
if ($query)
{
return true;
}
else
{
return false;
}
}
Here is the controller:
public function add_course()
{
$this->load->model('model_users');
if ($this->model_users->select_course())
{
echo "You have selected a course to enroll on to!";
echo ($this->session->userdata('users_id'));
return true;
}
else
{
echo "Database problem";
return false;
}
}
change
$data['users_id']=$this->input->post($this->session->userdata('users_id'));
to
$data['users_id']=$this->session->userdata('users_id');
$data['course_id'] = $this->input->post('your_course_id_var');
I am trying to send the latest user's id from UsersController to AdminController whose add_employee() action creates a new employee. My users and employees table are separate and what I want to do is when Admin creates a new user its entry go into users table. Then he opens create employee form and the latest user id will be assigned to the new employee the admin is creating. So when admin will open create new employee form the latest user id will be shown in the form.
My UsersController has this code for sending latest user it to AdminsController:
public function get_latest_user_id()
{
$content = $this->User->query("SELECT id FROM users ORDER BY id DESC LIMIT 0,1");
$this->set('latest_user', $content);
}
AdminsController page's add_employee contains this code:
public function add_employee()
{
$this->loadModel('Employee');
$this->set('latest_user', $this->requestAction('/Users/get_latest_user_id'));
if ($this->request->is('post'))
{
$this->Employee->create();
if ($this->Employee->save($this->request->data))
{
$this->Session->setFlash(__('The employee profile has been saved.'));
return $this->redirect(array('action' => 'list_of_employees'));
}
else
{
$this->Session->setFlash(__('The employee profile could not be saved. Please, try again.'));
}
}
}
So UserController's get_latest_user_id function sends latest user id to add_employee function of AdminController. There latest_user is set to latest user id so that when add_employee view is called it is there. But it is not showing. So I want to know that am i doing it right? Please help and thanks.
In add_employee.ctp I am displaying it like this:
echo $latest_user['User']['id'];
Move get_latest_user_id to the User model
public function get_latest_user_id()
{
$user = $this->query("SELECT id FROM users ORDER BY id DESC LIMIT 1");
if (empty($user)) {
return 0;
}
// return only the Id
return $user[0]['users']['id'];
}
In the controller:
public function add_employee()
{
$this->loadModel('Employee');
$this->loadModel('User');
$this->set('latest_user', $this->User->get_latest_user_id());
if ($this->request->is('post'))
{
// ....
}
}
cornelb is right that you should move the method to your User model. Although a more Cake-ish approach would be to use a find('first'), rather than doing a direct query():
// app/Model/User.php
public function getLatest() {
// Get the latest user
$user = $this->find('first', array(
'fields' => array('id'), // Only interested in id? Use this.
'order' => array('User.id' => 'DESC')
));
if (!empty($user)) {
return $user['User']['id'];
} else {
// Nothing was returned, this is very awkward
throw new NotFoundException(__('No users found!'));
}
}
And in your controller:
// app/Controller/AdminsController.php
public function add_employee() {
$this->loadModel('User');
$this->set('latestUser', $this->User->getLatest());
// ...
}
I have the following two actions in my controller:
function add()
{
if (!empty($this->data))
{
if ($this->Favour->save($this->data))
{
$this->Session->setFlash('Your favour has been saved.');
$this->redirect(array('controller'=>'favours','action'=>'index'));
}
}
}
function edit($id = null)
{
$this->Favour->id = $id;
if (empty($this->data))
{
$this->data = $this->Favour->read();
}
else
{
if ($this->Favour->save($this->data))
{
$this->Session->setFlash('Your favour has been updated.');
$this->redirect(array('controller'=>'favours','action'=>'index'));
}
}
}
1) I want to be able to add the logged in user id to the add action so that the new post is created with that user as its author id (their is a foreign key in the db table). I'm not sure how to talk to fields within the controller itself.
2) And for the edit action I want to make it so that only the author can edit the post so for example user 200 creates post 20 but user 100 cannot edit this post because his id is not 200! I'm not using ACL for my app but just simple authentication.
I've thought about doing a simple if statement in the action like:
function edit($id = null)
{
$this->Favour->id = $id;
$this->Favour->user_id = $user_id;
if($this->Auth->user('id') != $user_id)
{
$this->Session->setFlash('You do not have permission to edit that favour!');
$this->redirect(array('controller'=>'favours','action'=>'index'));
}
else
{
if (empty($this->data))
{
$this->data = $this->Favour->read();
}
else
{
if ($this->Favour->save($this->data))
{
$this->Session->setFlash('Your favour has been updated.');
$this->redirect(array('controller'=>'favours','action'=>'index'));
}
}
}
Would this be correct? BUT how do I get the user id from the favour?
function add() {
if (!empty($this->data)) {
$this->data['Favour']['user_id'] = $this->Auth->user('id');
if ($this->Favour->save($this->data)) {
//etc
This code assumes:
Your user is logged in
the user can access the add function
You are storing the id value of the logged in user in the field id
You have a foreign key in Favours table called user_id that matches the data type of the user id
As for edit; couple ways of achieving it.
I'd do:
function edit($id) {
$this->Favour->id = $id;
$favour_author = $this->Favour->field('user_id');
// get the user of this post
if($this->Auth->user('id') != $favour_author) {
$this->Session->setFlash('You do not own this post.');
$this->redirect('/someplace');
}
if (empty($this->data)) {
$this->data = $this->Favour->read();
}
// carry on.
If you use Auth Component, you can access the logged-in user record in $this->Auth->user() in controller. So to access the id: $this->Auth->user('id'). If you write your own authentication, it's up to you.
how to talk to fields within the controller itself.
What do you mean?