1

I want to fetch values from database and display it in the view, but I didn't get a correct result.

This is my controller:

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

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $hr_request = HrRequest::all();

        return array(
            'status' => 'success',
            'pages' => $hr_request->toArray());
    }
}

and this is my Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class HrRequest extends Model
{

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'hr_request';

    public $timestamps = false;
    /**
     * Fields.
     *
     * @var array
     */
    protected $fillable = [
        'profile_role_id', 'hr_id', 'vacancy', 'experience', 'job_description', 'status', 'viewed',
    ];
}

view name:view-requests.blade.php

I have no idea n how to do this in view. Can anyone help me?

1 Answer 1

5

Controller:

public function index()
{
    $hr_request = HrRequest::all();
    return view('view-requests')->with('hr_request', $hr_request);
}

View:

@foreach($hr_request as $row)
                <tr>
                    <td>{{$row->profile_role_id}}</td>
                    <td>{{$row->vacancy}}</td>
                    <td>{{$row->experience}}</td>
                    <td>{{$row->job_description}}</td>
                    <td>
                        <button type="button" class="btn btn-primary">View</button>
                        <button type="button" class="btn btn-success">Edit</button>
                        <button type="button" class="btn btn-danger">Delete</button>
                    </td>
                </tr>
            @endforeach

Route:

Route::get('/view-hr-requests', 'HrRequestController@index');

It's working

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.