Orderby defaults to be ASC direction.
You must specify the direction.
->orderBy('created_at', 'DESC')
Also your orderby statement should be on your relationship method, and you don't need to use ->get() method.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have problem showing last content from database. I have following models :
Users.php
public function following() {
return $this->belongsToMany('App\User', 'following', 'following_from_id', 'following_to_id');
}
public function followers() {
return $this->belongsToMany('App\User', 'following', 'following_to_id', 'following_from_id');
}
public function statuses() {
return $this->hasMany('App\statuses','uid');
}
public function articles()
{
return $this->hasMany('App\Article');
}
public function comments()
{
return $this->hasMany('App\Comments');
}
Statuses.php
protected $table = 'statuses';
protected $fillable = ['uid','status','created_at','updated_at'];
following.php
protected $table = "following";
protected $fillable = ['following_from_id','following_to_id'];
Users table :
id | and other fields
Following table :
Schema::create('following', function(Blueprint $table)
{
$table->increments('id');
$table->integer('following_from_id')->unsigned();
$table->integer('following_to_id')->unsigned();
$table->timestamps();
$table->foreign('following_from_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('following_to_id')->references('id')->on('users')->onDelete('cascade');
});
Statuses table :
Schema::create('statuses', function(Blueprint $table)
{
$table->increments('id');
$table->integer('uid')->unsigned();
$table->string('status');
$table->timestamps();
$table->foreign('uid')->references('id')->on('users')->onDelete('cascade');
});
I'm returning the followed user's status in HomeController.
$status = Auth::user()->following()->with('statuses')->orderBy('created_at')->get();
return view('home',compact('status'));
I'm using orderBy but still it doesn't display the latest contents :( Rather it shows : http://i.imgur.com/UjYu1Dp.png
It's not displaying status by created_at.
Please help me out.
Thanks!
Please or to participate in this conversation.