0

I am trying to return a specific value from the database on View(activate_id) from AdminController but it returns nothing.

web.php

Route::get('/activate-id', 'AdminController@activateId')->middleware('role:admin');

AdminController.php

public function activateId(Request $request)
{
    $by = DB::table('users')
        ->select('referrer')
        ->where('username', $request->username)
        ->first();

    return view('activate_id')->with('by', $by);
}

activate_id.blade.php

<form method="post" action="/activate-user">
    @csrf
    <div class="input-group mb-3">
        <div class="form-group">
            <input id="fuser" type="text" class="form-control" name="username" placeholder="Username">
            <p class="text-info" id="fdetails"></p>
        </div>
    </div>
    <div class="input-group mb-3">
        <div class="form-group">
            <label for="recipient-name" class="control-label">Referrer</label>
            <input type="text" class="form-control" name="by" id="recipient-name" value="{{$by}}" readonly>
        </div>
    </div>
    <div class="col-12">
        <button class="btn btn-outline-success"> Activate</button>
    </div>
</form>

enter image description here

Rwd
  • 34,180
  • 6
  • 64
  • 78
  • Does `dd($by);` show the expected result in your controller before returning the view? You may need to do `{{ $by->referrer }}` in your blade file. – Brian Lee May 03 '20 at 09:40
  • After {{ $by->referrer }} adding this it returns Arguments "Trying to get property 'referrer' of non-object (View: C:\xampp\htdocs\mlm-solution-master\resources\views\activate_id.blade.php)" – Vinayak Sahu May 03 '20 at 09:51

1 Answers1

0

Result that comes out from the database is object, which in your case is $by variable, so you need to extract the value of referrer from it.

So to solve the problem try this:

 {{$by->referrer}}

I hope it helps

Hardood
  • 503
  • 1
  • 5
  • 15
  • Arguments "Trying to get property 'referrer' of non-object (View: C:\xampp\htdocs\mlm-solution-master\resources\views\activate_id.blade.php)" – Vinayak Sahu May 03 '20 at 09:49
  • Can you please var_dump the value of $by in the controller before you send to the view. – Hardood May 03 '20 at 09:51
  • It returns NULL public function activateId(Request $request) { $by = DB::table('users')->select('referrer')->where('username', $request->username)->first(); var_dump($by); die(); return view('activate_id')->with('by', $by); } – Vinayak Sahu May 03 '20 at 10:02
  • Then the problem is in your query, try to fix it, possible cause is the value of the username that comes from the request. – Hardood May 03 '20 at 10:07
  • thanks, you are correct @Hardood that was my silly mistake... – Vinayak Sahu May 03 '20 at 10:41