phplaravellaravel-bladelaravel-controllerlaravel-views

How to return a specific value from the database on View from Controller in Laravel?


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


Solution

  • 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