Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cimrie's avatar

@jearson

The Fractal package definitely works, so I expect this is an error with the code you're using. Can you show me the parts of the code you're using fractal? Also, if you can show me the transformers you have defined that would also help. Try to follow the error message stack and see if you can find the point where your code fails (basically look at the error log that is spat out and find the top-most error that states your file, and look there, that's usually the issue).

jay_gorio's avatar

@Clmrie I think so. Here's the code.

    use League\Fractal;
    use League\Fractal\Manager;
    use League\Fractal\Resource\Collection;
    use App\Transformers\CrimeReportTransformer;

  class CrimeReportController extends Controller

{

public function __construct()
{
    $this->middleware('auth');
} 

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{   
    
  $reports = CrimeReport::all();
  $collection = new Collection($reports,new CrimeReportTransformer());
  $fractalManager = new Manager();
  return view('crimereports.index')->with(['data'=>$fractalManager->createData($collection)->toArray()]);

}

VictimTransformer

 <?php

namespace App\Transformer; use App\VictimProfile; use League\Fractal\TransformerAbstract; class VictimTransformer extends TransformerAbstract {

public function transform(VictimProfile $victim)
{
    return [
        'id' => $victim->id,
        'firstName'=> $victim->firstname,
        'middleName' => $victim->middlename,
        'lastName' => $victim->lastname,
        'streetNo' => $victim->street,
        'vbarangay' => $victim->barangay_id,
        'vgender' => $victim->gender,
        'vcontact_no' => $victim->contact_no,
    ];
}

}

  SuspectTransformer

 use App\SuspectProfile;

use League\Fractal\TransformerAbstract; class SuspectTransformer extends TransformerAbstract {

public function transform(SuspectProfile $suspect)
{
    return [
        'id' => $suspect->id,
        'firstName' => $suspect->firstname,
        'middleName' => $suspect->middlename,
        'lastName' => $suspect->lastname,
        'houseNo' =>$suspect->house_no,
        'sstreet' => $suspect->street,
        'sbarangay_id' =>$suspect->barangay_id,
        'scity' => $suspect->city,
        'sgender' => $suspect->gender,
        'sheight' => $suspect->height,
        'sweight' => $suspect->weight,
        'sstatus' => $suspect->status,
        'snationality' => $suspect->nationality,
        'scrime_type_id' => $suspect->crime_type_id,
        'sdistinguising_marks' => $suspect->distinguising_marks,
        'scontact_no' => $suspect->contact_no,

    ];
}

} CrimeReportTranformer

 <?php
   namespace App\Transformers;

 use League\Fractal\TransformerAbstract;
 use App\CrimeReport;
 use App\Transformers\SuspectTransformer;
  use App\Transformers\VictimTransformer;

class CrimeReportTransformer extends TransformerAbstract
   {
protected $defaultIncludes = [
    'suspects',
    'victims',
];
public function transform(CrimeReport $report){
    return [
        'id' => $report->id,
    ];
}
 public function includeSuspects(CrimeReport $report){
     return $this->collection($report->suspects, new SuspectTransformer());
}
 public function includeVictims(CrimeReport $report){
     return $this->collection($report->victims, new VictimTransformer());
}

}

jay_gorio's avatar

@Clmrie I didnt use the fractal at this moment. However i used different method found in eloquent laravel documentation. Here's the code:

        public function index()
{   
  $reports = CrimeReport::with('crimeName','crimeType','suspect','victim')
        ->orderBy('created_at','desc')->get();
  return view('crimereports.index',compact('reports'));
}

This method works. Can I consider this as inner join? Thanks. Also when I used created_at to sort the crimereport to latest It is not working.

internet-crossroads's avatar

When I started using laravel I had much more experience with MySQL than I did with laravel, so I did joins etc because I understood them better.

The models really are the way to go, and if you stick with the conventions of the intermediary tables most things will be simple, but you gotta really dig into the docs.

Model::with('pivotThingy')->where('whatever')->etc

and for queries

Model::whereHas('pivotThingy')->where('whatever')->etc

Chain em up as much as you want.

It's not really an inner join afaik, it's more like a "eager load this with the model." Joins function differently, because the model is far more than just extra tables. It might help to look at the result of the Model::with('thing') after ->toArray() because thats most likely what you're used to seeing as the result of an inner join.

cimrie's avatar

@jearson

What do your models look like? that code all looks okay so I think that it will be incorrectly set relationships that are returning items rather than eloquent collections. Which of course means that when fractal tries to use foreach to loop through the items, it is only getting 1, and fails as a result. (this is all hypothetical until I see code of course).

@jay_gorio

Hey, that's absolutely fine. The reason I recommended fractal is simply because it allows you to customise the output down to each field name and separate the http output from the database schema. As @internet-crossroads said, I don't think it is the traditional 'join' on SQL. I think Laravel just eager loads the related items using normal select queries, and pulls the information together as objects using PHP, not directly from the output of the query, if that makes sense? I doubt you'll see a difference anyway.

1 like
jay_gorio's avatar

@Clmrie Okay I got it. While experimenting yesterday I found it that using fractal is quite good. However it seems it doesnt work with my code I think. And I cannot found the error causing it. That's why I used the eloquent orm. But it seems it works like the inner join so if there is a null value in the table it will return an error. How do I fix this so that it works like left join?

Previous

Please or to participate in this conversation.