如何使用Laravel Eloquent预加载获取built_load_cost与built_load_revenue数组值?
built_load_cost and built_load_revenue Data Hey there! Let's break down how to retrieve those built_load_cost and built_load_revenue datasets using Laravel Eloquent's eager loading. First, we'll assume you have separate database tables for your main load data, cost data, and revenue data (since eager loading is designed for model relationships, not nested JSON fields). Here's a step-by-step guide tailored to your scenario:
1. Define Your Eloquent Models & Relationships
First, create the necessary models and set up the correct relationships between them. Let's assume your main model is Load, with associated BuiltLoadCost and BuiltLoadRevenue models.
Load Model (app/Models/Load.php)
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Load extends Model { use SoftDeletes; // Fillable fields for your main load table protected $fillable = [ 'load_number', 'dispatcher_id', 'carrier_id', 'customer_id', 'ip' ]; // Relationship to BuiltLoadCost (one-to-one: one load has one cost record) public function builtLoadCost() { // If your foreign key on `built_load_costs` isn't `load_id`, specify it here: // return $this->hasOne(BuiltLoadCost::class, 'custom_foreign_key', 'id'); return $this->hasOne(BuiltLoadCost::class); } // Relationship to BuiltLoadRevenue (one-to-one: one load has one revenue record) public function builtLoadRevenue() { return $this->hasOne(BuiltLoadRevenue::class); } }
BuiltLoadCost Model (app/Models/BuiltLoadCost.php)
namespace App\Models; use Illuminate\Database\Eloquent\Model; class BuiltLoadCost extends Model { // Fillable fields for your cost table protected $fillable = [ // Add all fields from your `built_load_cost` dataset here (created_at, updated_at, etc.) ]; // Reverse relationship back to Load public function load() { return $this->belongsTo(Load::class); } }
BuiltLoadRevenue Model (app/Models/BuiltLoadRevenue.php)
namespace App\Models; use Illuminate\Database\Eloquent\Model; class BuiltLoadRevenue extends Model { // Fillable fields for your revenue table protected $fillable = [ // Add all fields from your `built_load_revenue` dataset here ]; // Reverse relationship back to Load public function load() { return $this->belongsTo(Load::class); } }
Note: If your table names don't follow Laravel's pluralization rules (e.g., your cost table is named built_load_cost instead of built_load_costs), add a $table property to the model:
protected $table = 'built_load_cost';
2. Use Eager Loading to Fetch Associated Data
Now you can use Eloquent's with() method to eager load the associated cost and revenue data in a single query (avoiding the N+1 query problem).
Fetch a Single Load with Associated Data
// Get the load with ID 5, along with its built_load_cost and built_load_revenue $load = Load::with(['builtLoadCost', 'builtLoadRevenue'])->find(5); // Access the cost data $costData = $load->builtLoadCost; // Returns a BuiltLoadCost model instance (or null if no record exists) echo $costData->id; // Access individual fields like in your dataset example // Access the revenue data $revenueData = $load->builtLoadRevenue;
Fetch Multiple Loads with Associated Data
// Get all loads, each with their cost and revenue data $loads = Load::with(['builtLoadCost', 'builtLoadRevenue'])->get(); foreach ($loads as $load) { echo "Load Number: " . $load->load_number . "\n"; if ($load->builtLoadCost) { echo "Cost ID: " . $load->builtLoadCost->id . "\n"; } if ($load->builtLoadRevenue) { echo "Revenue Created At: " . $load->builtLoadRevenue->created_at . "\n"; } }
3. Handle Missing Associated Records
If some loads might not have a cost or revenue record, use withDefault() to return a default model instance instead of null, preventing errors when accessing properties:
// In Load model's builtLoadCost method public function builtLoadCost() { return $this->hasOne(BuiltLoadCost::class)->withDefault([ 'id' => null, 'created_at' => null, // Add default values for other fields as needed ]); }
内容的提问来源于stack exchange,提问作者DukesNuz

