Reputation: 449
In my Laravel project, I am trying to display some data from the database. The data is looked up from multiple tables. The structure looks like this:
Group ( e.g. name = "G1")
Group (e.g. name = "G2")
Group, Book, Pencil & Ink are Eloquent/Model classes. I can lookup the data fine (with all the relationship between the tables). Now, I want to send this data to the Laravel view php, so that it can be displayed. At the view php, there will be drop-down element for Group with group name (e.g. "G1" or "G2") displayed, so if the user select "G1", the page should display all the G1 related info (e.g. B1, P1, I1) and for "G1", it's related info. By default (or on page load), first group in the list and its related info should be displayed.
I want to know how should I pack the data (I believe is some array) and use it in view php? And also how to update the view when the selection has changed in the Group dropdown?
Many Thanks!
Upvotes: 1
Views: 10214
Reputation: 1814
$groupOne = Group::where()->get();
$groupTwo = Group::where()->get();
We can use this command when there is 1 or 2
return view('pathTo.someView', compact('groupOne', 'groupOne'));
We can use an another method like this.
$params = [
'groupOne' => $groupOne,
'groupTwo' => $groupTwo,
'groupThree'=>$groupThree,
'groupFour'=>$groupFour,
];
return view('pathTo.someView')->with($params);
Use these in blades as follows..
@foreach( $groupThree as $item )
@endforeach
@foreach( $groupfour as $item )
@endforeach
Upvotes: 2
Reputation: 62
Give each model a variable, and pass it to the view method. It's the second argument: I use compact('customerModel', 'staffModel')
; looks much cleaner.
Upvotes: 1
Reputation: 371
First, you gather the data you need with an eloquent query:
$groups= \App\Group::where('')->get();
This method returns a Collection class with a bunch of helper methods. If you want a single instance, use ->first() instead.
Then you can pass your data to a view in a second parameter like this:
return view('myview', [
'groups' => $groups
]);
And finally, you can use blade templating directives to render your list in your view file:
<select>
@foreach($groups as $group)
<option>{{$group->book->attribute}}</option>
@endforeach
</select>
To update the dropdown selections dynamically, you need to use dynamic JavaScript to show the relevant dropdown. If you have a lot of groups or other models, you should probably only fetch a single group at a time using ajax when the selection is changed. When using ajax to load the lists dynamically, you can use the blade directive to render the initial list but cannot rely on it to update your views since blade is a server side rendering engine and cannot change it's content dynamically after the view has been sent to the clientside.
Upvotes: 0
Reputation: 1215
In your controller method
$groupOne = Group::where()->get(); // Your Group One Query, Whatever It May Be
$groupTwo = Group::where()->get(); // Your Group Two Query, Whatever It May Be
return view('pathTo.someView', compact('groupOne', 'groupOne'));
You will be able to loop through these in your blade temeplate. Ex:
@foreach( $groupOne as $item )
@endforeach
@foreach( $groupTwo as $item )
@endforeach
If you want to change the view/content of the page depending on change of a dropdown, you are going to have to use JavaScript for that. Laravel comes with VueJS out of the box. I suggest starting there for dynamically changing content.
Upvotes: 3