5128

Laravel creating different views from query

Question:

I have the following query:

$appointments = Appointment::orderBy('appointment', 'asc') ->leftJoin('appointments_labels', 'appointments_labels.id', '=', 'appointments.label_id') ->leftJoin('appointments_statuses', 'appointments_statuses.id', '=', 'appointments.status_id') ->select('appointments.id', 'appointments.appointment', 'appointments.label_id', 'appointments.status_id', 'appointments_labels.label', 'appointments_statuses.status') ->get();

What I am using this for is basically to display all my appointments like so (simplistic version of my view):

@foreach($appointments as $appointment) <table> <tr> <th>Appointment Name</th> <th>Label</th> <th>Status</th> </tr> <tr> <td>{{ $appointment->appointment }}</td> <td>{{ $appointment->label }}</td> <td>{{ $appointment->status }}</td> </tr> </table> @endforeach

This will display all my appointments, including the correct label name and status name from the proper database tables. My wish is, to be able to create a view like so:

@foreach($appointments as $appointment) <table> <tr> <th>{{ $appointment->label }}</th> <th>Status</th> </tr> @foreach($appointment->appointments as $label) <tr> <td>{{ $label->appointment }}</td> <td>{{ $label->status }}</td> </tr> @endforeach </table> @endforeach

So it lists the first label inluding all appointments which have that particular label, and then the next label with it's appointments, and so on. Is that simply possible with my above query? Or should I create relations in my models instead and work from there? If there is a better way to do all this ofcourse, I'm all ears.

Below is an export of my database tables for a better understanding of my code perhaps.

CREATE TABLE IF NOT EXISTS `appointments` ( `id` int(11) NOT NULL, `appointment` varchar(255) NOT NULL, `location` varchar(255) NOT NULL, `description` text NOT NULL, `start` datetime NOT NULL, `end` datetime NOT NULL, `label_id` int(11) NOT NULL, `status_id` int(11) NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ) CREATE TABLE IF NOT EXISTS `appointments_labels` ( `id` int(11) NOT NULL, `label` varchar(255) NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ) CREATE TABLE IF NOT EXISTS `appointments_statuses` ( `id` int(11) NOT NULL, `status` varchar(255) NOT NULL, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' )

Example:

<a href="https://i.stack.imgur.com/cNDrZ.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/cNDrZ.png" data-original="https://i.stack.imgur.com/cNDrZ.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer1:

You can use laravel collections groupBy:

Import it in your header:

use Illuminate\Support\Collection;

Then:

$appointments = Appointment::orderBy('appointment', 'asc') ->leftJoin('appointments_labels', 'appointments_labels.id', '=', 'appointments.label_id') ->leftJoin('appointments_statuses', 'appointments_statuses.id', '=', 'appointments.status_id') ->select('appointments.id', 'appointments.appointment', 'appointments.label_id', 'appointments.status_id', 'appointments_labels.label', 'appointments_statuses.status') ->get(); $appointments = Collection::make($appointments)->groupBy('label'); var_dump($appointments);

Recommend

  • What is wrong with “checking for self-assignment” and what does it mean?
  • Calculating Date Difference In XSL
  • Lift Authentication
  • Efficiency of JList Search
  • garbled css name when styling within UiBinder
  • How to open html table in xls on click of a button
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • Can't remove headers after they are sent
  • Azure table store snapshot/backup capability
  • Bigquery event streaming and table creation
  • xcode don't localize specific strings
  • List images(01.png) and descriptions(01.txt) from directory
  • How to use JavaScript to determine whether a file exists in a directory?
  • Swift: Switch statement fallthrough behavior
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • D3 nodes and links from JSON with nested arrays of children
  • Read a local file using javascript
  • Linq Objects Group By & Sum
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Submit form in a displaytag pagination
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Large data - storage and query
  • Delete MySQLi record without showing the id in the URL
  • Unanticipated behavior
  • Cannot Parse HTML Data Using Android / JSOUP
  • Comma separated Values
  • How to include full .NET prerequisite for Wix Burn installer
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?