Rope
Rope

Reputation: 21

getting undefined variable in laravel view 5.7

getting undefined variable in my view

public function confirm()
    {
        $data = Transaction::select('number','total','package')->where('user_id','=',\Auth::user_id()->id)->first();

       return view('user.package',compact('data'));

    }

// My view

 <center> 
<h3 class="card-title"><b>You are about to buy {{$data->number}} package of {{$data->package}} for {{$data->total}}</b>
</h3>
</center>

Upvotes: 0

Views: 126

Answers (4)

Shailendra Gupta
Shailendra Gupta

Reputation: 1118

use this

use Auth;
public function confirm()
{
    $data = Transaction::select('number','total','package')->where('user_id','=',Auth::user()->id)->first();

   return view('user.package',compact('data'));

}

Upvotes: 0

Jignesh Joisar
Jignesh Joisar

Reputation: 15095

change to \Auth::user_id()->id to \Auth::user()->id

$data = Transaction::select('number','total','package')->where('user_id',\Auth::user()->id)->first();

by getting current authenticate user id two ways.

1.Auth::id();
2.Auth::user()->id

in your view check like that isset condition row is exist or not

            @isset($data)
                <center> 
                   <h3 class="card-title"><b>You are about to buy {{$data->number}} package of {{$data->package}} for {{$data->total}}</b>
                  </h3>
                </center>
             @endif

Upvotes: 2

Kiran Patel
Kiran Patel

Reputation: 379

Try auth()->user()->id instead of \Auth::user_id()->id

or You can use an Eloquent relation like

$user->with('Transaction')->select('number','total','package')->first();

Upvotes: 0

arispapapro
arispapapro

Reputation: 319

Try to use ->get() instead of ->first()

Upvotes: 0

Related Questions