Level 102
Try using the correct case
Route::get('product/{slug}', function ($slug) {
$product = Product::where('slug', $slug)->with('data')->first();
$data = $product->data;
return view('product');
});
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm testing a simple example and can't understand why the second select * from data is needed/executed when the data is already returned by the first query. Could anybody offer any insights?
Thanks!
![]()
Route::get('product/{slug}', function ($slug) {
$product = Product::where('slug', $slug)->with('Data')->first();
$data = $product->data;
return view('product');
});
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
public function data()
{
return $this->hasMany('App\Models\Data');
}
public function getRouteKeyName()
{
return 'slug';
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Data extends Model
{
use HasFactory;
public function product()
{
return $this->belongsTo('App\Models\Product');
}
}
Try using the correct case
Route::get('product/{slug}', function ($slug) {
$product = Product::where('slug', $slug)->with('data')->first();
$data = $product->data;
return view('product');
});
Please or to participate in this conversation.