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

Msoft's avatar
Level 1

Why did not display latest updated record from the table in Laravel 5.6

Working with Laravel 5.6 and mysql in my app. I have two tables as vehicles and uploads, both Models relationship are as following, Vehicle Model,

public function uploads()
{
    return $this->hasMany(Upload::class);
} 

Upload Model,

public function vehicle()
{
    return $this->belongsTo(Vehicle::class);
}

and My vehicles table structure is as following,

id  name  adtype  created_at  updated_at
1   asc          1         2018-10-11  2018-10-11
2   lop          0         2018-11-09  2018-11-09
3   hyu         1         2018-11-10  2018-11-10

now I need to grab from vehicle table adtype == 1 and latest updated_at records, so I have following controller,

public function showad()
{
    $vehicles = Vehicle::with('uploads')
            ->orderBy('adtype')
            ->latest('updated_at');

    return view('vehicles.slider')->withVehicles($vehicles);
}

and My route is,

Route::get('vehicles.slider', [
    'uses' => 'VehicleController@showad',
    'as'   => 'vehicles.slider',
]);

and slider.blade.php is

@foreach( $vehicles->uploads as $upload)
    <img src="/images/{{ $upload->resized_name }}" height="150" width="200" >
@endforeach

and now I have include this slider blade file with show.blade.php file as following,

<div class="col-md-4">
@include('vehicles.slider')
</div>

show blade file url is like this,

http://localhost:8000/all-ads/23  // 23 is vehicle id

now my problem is here in my include slider blade file in show blade file. it is not showing latest updated vehicle table records. it is all ways showing existing show blade file include vehicle id records. as an example when I visit vehicle id 23 in show blade file my include file result is equel vehicle id 23. but I need latest updated vehicle id records in slider blade file. how can I do this? uploads table structure

id imagename vehicle_id
1  bgffg.png          1
2  jhjfhj.jpeg         2
3  jsdhj.png          1 
0 likes
5 replies
Snapey's avatar

If you want the adtype==1 then you need a where statement

$vehicles = Vehicle::with('uploads')
            ->where('adtype','=',1)
            ->latest('updated_at');

ideally you would not have magic numbers like this scattered through your code, you would create a scope on your model

$vehicles = Vehicle::with('uploads')
            ->paidAdverts()
            ->latest('updated_at');

or similar depending on what '1' actually represents

Msoft's avatar
Level 1

@snapey I did your comments on controller but still same results here. but I did not understand what you suggest on model in your answer. it is not clear for me...

Msoft's avatar
Level 1

@snapey did you mean this kind of scope,

public function scopeadvertise($query)
{
     return $query->Vehicle::with('uploads')
            ->paidAdverts()
            ->latest('updated_at');
      
}
Snapey's avatar

No, more like

public function scopeadvertise($query)
{
     return $query->where('adtype',1);
}

and then you only have this bit of code in one place

Please or to participate in this conversation.