Ace
Ace

Reputation: 83

Laravel 5.2 Updating the Database Table

I just want to update my database table Time_tracker, but i keep getting error when i attempt something. Can anyone help me with this? the current error is ErrorException in PaypalController.php line 57: Trying to get property of non-object

this is the code.

PaypalController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Input;

use Illuminate\Support\Facades\Validator;

use Illuminate\Support\Facades\Redirect;

use Illuminate\Support\Facades\Request;

use Illuminate\Support\Facades\Session;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\DB;

use Illuminate\Mail\Mailer;

use App\Maintenance;

use App\Time_tracker;


class PaypalController extends Controller {

 /**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth');
}

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Http\Response
 */

 public function index() {

 return view('paypal'); 

 }

 public function success() {
 $price = \Session::get('value_price');
 $value = $price;
 if($value == '465'){

     $user_id = Auth::user()->id;

     $Totalpurchase = DB::table('time_tracker')->where('user_id', $user_id->user_id )->sum('purshase_time');         

         $purchase = $Totalpurchase + 15;

         DB::table('users')
             ->where('user_id', $user_id)
             ->update(['status' => 'paid',
                       'purshase_time' => $purchase_time
             ]);

 }elseif($value == '700'){

     $user_id = Auth::user()->id;

     $Totalpurchase = DB::table('time_tracker')->where('user_id', $user_id->user_id )->sum('purshase_time');

         $purchase = $Totalpurchase + 25;

         DB::table('users')
             ->where('user_id', $user_id)
             ->update(['status' => 'paid',
                       'purshase_time' => $purchase_time
             ]);


 }elseif($value == '1300'){

     $user_id = Auth::user()->id;

     $Totalpurchase = DB::table('time_tracker')->where('user_id', $user_id->user_id )->sum('purshase_time');

         $purchase = $Totalpurchase + 50;

         DB::table('users')
             ->where('user_id', $user_id)
             ->update(['status' => 'paid',
                       'purshase_time' => $purchase_time
             ]);

 }


 return view('success');    

 }


 public function failed() {

 return view('failed'); 

 }


 }

Upvotes: 1

Views: 230

Answers (1)

Risan Bagja Pradana
Risan Bagja Pradana

Reputation: 4674

You tried to access the user_id property from an integer (the $user_id):

$Totalpurchase = DB::table('time_tracker')->where('user_id', $user_id->user_id )->sum('purshase_time');

Just replace the $user_id->user_id part with $user_id. The $user_id is already an id of logged user. So your code should look like this instead:

$Totalpurchase = DB::table('time_tracker')->where('user_id', $user_id)->sum('purshase_time');

I found three occurrences of $user_id->user_id on your PaypalController class. Don't forget to replace all of them.

Hope this help!

Upvotes: 1

Related Questions