0

I've started my first project in laravel (following along with the beginner laracasts series). I've successfully made a 'joggers' scope and get_joggers() function in the controller to pass that set of data to a view- great!

So now- I have a view where I pass all the people, and a partial to show different subsets of them, like so:

@include ('partials._people_list', ['person_group' => $joggers]);

My question: On a view page, what's the syntax declare/set a variable to the results of a function in the controller? I would imagine something like this, but since a view page wants HTML I'd at least have to put it in some php tags or something (though there's probably a better way):

$person_group = action('PeopleController@get_joggers');

This seems so painfully basic, but the first 27 laracasts didn't get there and I've been hunting around for while. Thanks in advance for any help!

Diane Kaplan
  • 1,626
  • 3
  • 24
  • 34
  • What I understand from your question is how to call a method directly from view, is that the case look at this link http://stackoverflow.com/questions/27322854/how-to-call-controller-method-from-blade-view-directly-as-url – Maytham Fahmi Oct 11 '15 at 13:12
  • The approach here would be to define s separate class (perhaps a [repository](https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/)) that defines the method that fetches the joggers and returns is as a collection. Then you can call if from your controller and add it to your view response, and call if from elsewhere and use the collection as is. – Bogdan Oct 11 '15 at 13:18
  • maytham- that's not my question today, however very useful to know, I'm sure I'll use that at some point. Thank you! Bogdan- wow, just scrolling through, I think that's EXACTLY the sort of thing I'm looking for. The whole repository idea is new for me but this looks like a great step-by-step walkthrough. Going out for a run right now, and so excited to dig into this when I get back. THANK YOU!!!!! – Diane Kaplan Oct 11 '15 at 13:39
  • Thanks again for that repositories article- really helpful. It sounds like it's great for reducing code duplication and separating out different things, in this case I think my need is a lot simpler: everyone in my $people class has all the same requirements, I just want to be able to group them differently on the view- so I pass the whole set of $people to the view, but then in displaying the page I can show one list of the people who are joggers (I made a partial for list display in hopes of reusing it), one list of the people who are yogis, etc. Is that possible? – Diane Kaplan Oct 12 '15 at 00:59

1 Answers1

1

Update! I talked to a buddy at work, and the answer was to make/set $joggers in the PeopleController, and then pass it all along like so:

public function index()
{
    $people = Person::latest('created_at')
        ->displayable()
        ->orderBy('last', 'asc', 'first', 'asc')
        ->get();

    $joggers= Person::joggers('created_at')->get();

return view('person.index', compact('people', 'joggers'));

I've returned different types of data before, and it hadn't occurred to me that I could send two different collections of people. Mystery solved :)

Diane Kaplan
  • 1,626
  • 3
  • 24
  • 34