Msoft
11 months ago

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

Posted 11 months ago by Msoft

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' => '[email protected]',
    '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 

Please sign in or create an account to participate in this conversation.