kclark6595's avatar

Fetching data from a view

Learning about Laravel. Know how to create a controller that builds an array of data from an Eloquent model, pass that array to a view and display it.

What I am trying to do is create a main view that has the header and footer information all pages use, then use a child view to display the body.

The main blade file has an include for a file that builds a dynamic navigation bar.

How to I go about retrieving data from within the include file the main blade file that's called by a child?

0 likes
5 replies
Snapey's avatar

There are many good books on Laravel. I recommend Easy Laravel by W.Jason Gilmore (https://leanpub.com/easylaravel)

But if paying is a problem, Blade is explained concisely in the laravel docs; http://laravel.com/docs/5.1/blade

Any data you pass to the view is available to the whole blade template (all sections and includes). You don't have to explicitly pass data to the sub-views.

kclark6595's avatar

What I have (very abbreviated) is:

main.blade.php

<html>
@include('header')
<body>
@section('content')

@stop
</body>
</html>

home.blade.php

@extends('main')

@section('content')
    <h1>Home</h1>
@endsection

Now from the controller, I call the home.blade.php view

Can I fetch the data thats needed in the header view from INSIDE the header file rather than having to pass the data from every view I call? Or do I need to simply create a global function that I call in every controller before I call a view?

spoon's avatar

@kclark6595, I'm also new to Laravel, but I'll try my best. (Late edit, I think I misunderstood your question. Shoot :/)

Here's what you need to display data.

1-) A model. I presume that you already have a model, so I skip this part.

2-) A view (You can also display data without using a view)

3-) A controller

Here's my example, I hope you understand better.

Let's define a route. (routes.php)

    Route::get('users', 'DashBoardController@listUsers');

Now create a function inside your DashBoardController. Here's my DashBoardController.php file. I added App\Models\User because I'm using the User model that lives in the App\Models folder. I also needed to add namespace because my DashboardController lives in the App\Http\Controllers\Backend\Admin folder.

<?php namespace App\Http\Controllers\Backend\Admin;
use App\Models\User;

use App\Http\Controllers\Controller;

class DashboardController extends Controller {
    public function listUsers() {
        $users = User::all();
        return view('backend.admin.user.index')->with('users', $users);
    }

}

If you want to add multiple views, just add another with. As far as I know, there are also two other ways to do it.

return view('backend.admin.user.index')
->with('users', $users)
->with('random',$variable);

index.blade.php (This file is in the views/backend/admin/user folder, check out the dots above! We use dots instead of slash here.)

@foreach($users as $user)
{{$user->user_id}} // I echo all of the ID's in my users table.
@endforeach
kclark6595's avatar
kclark6595
OP
Best Answer
Level 1

I found that what I needed to use was a composer to load a collection on the view call

Please or to participate in this conversation.