从数据库中获取数据并在laravel中显示数据库中的值

时间:2017-04-07 06:18:32

标签: laravel-5.4

我想从数据库中获取值并在视图中显示它,但我没有得到正确的结果。

这是我的控制器

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());
    }
}

这是我的模型

<?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

我不知道如何在视野中这样做。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:5)

<强>控制器:

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

查看:

@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::get('/view-hr-requests', 'HrRequestController@index');

它正在运作