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

abhishek009's avatar

Show latest content from database.

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!

0 likes
29 replies
EliasSoares's avatar

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.

EliasSoares's avatar

I've almost sure that's because you're trying to order after your relationship.

Put your ->orderBy in following relationship method chained after the belongsToMany.

EliasSoares's avatar

Sorry. I've tested here and your way should work.

Do your table following have the timestamps being touched?

cipsas's avatar

@abhishek009 You sorting users, not statuses. To sort statuses, try to use something like this:

$status = Auth::user()->following()->with('statuses'=> function ($query) {
        $query->orderBy('created_at', 'asc');
    })->get();
abhishek009's avatar
$status = Auth::user()->following()->with('statuses'=> function ($query) {
    $query->orderBy('created_at', 'asc');
})->get();

Seems this one query has many errors. http://prntscr.com/71ot8x

1 like
cipsas's avatar

I forgot to add array symbols:

$status = Auth::user()->following()->with(['statuses'=> function ($query) {
        $query->orderBy('created_at', 'asc');
    }])->get();
cipsas's avatar

@abhishek009 By the way, why you query first users and not status? More logical if your query would be something like:

$status = Statuses::following()->with('user')->orderBy('created_at')->get();

of course then you have to update Statuses.php and Following.php

abhishek009's avatar

What do I need to update in model ? Can you elaborate please ?

abhishek009's avatar

Since following table and statuses table has no direct relationship.

cipsas's avatar

I don't know you app logic. What following do? perhaps it enough :

$status = Statuses::with('user')->orderBy('created_at')->get();

Then in Statuses.php add:

public function user() {
        return $this->belongsTo('App\User');
    }
abhishek009's avatar

See, For example I'm following you and other 30 people. So I need a table called "follow" and it will have fields like "following_from_user_id" and "following_to_user_id". And we also need a "statuses" table which will store statuses with users id. Now I want only statuses of users I follow. That's the logic!

abhishek009's avatar

@cipsas You query is giving me the status of the every users that I know. But I want statuses of users I'm following.

cipsas's avatar

OK, one way to handle it would be:

get all users ID you follow:

$ids  =Auth::user()->following()->lists('id');

Then get all statuses associated these ids:

$statuses = Statuses::whereIn('uid', $ids)->with('user')->orderBy('created_at')->get();
abhishek009's avatar
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select `id` from `users` inner join `following` on `users`.`id` = `following`.`following_to_id` where `following`.`following_from_id` = 1)
pmall's avatar

Howcome you can get a join from this ? :D

abhishek009's avatar

This is database queries and relationship is really frustrating for me :( @pmall Don't you think instead at laughing you could help ?

pmall's avatar

I read the code you posted I have no idea why you have a join here. Are you sure you showed us everything ?

pmall's avatar

Show us the code generating this query error please

abhishek009's avatar

This query is giving me an error.

$status  = Auth::user()->following()->lists('id');

I have no problem running this query if I pass ID's manually.

// $status = statuses::whereIn('uid', ['2','3'])->with('user')->orderBy('created_at','desc')->get();
abhishek009's avatar

Okay, So I made a query which is retrieving the followed users statuses. @pmall @cipsas

$following = following::where('following_from_id','=',Auth::user()->id)->lists('following_to_id');
$status = statuses::whereIn('uid', $following)->with('user')->orderBy('created_at','desc')->get();

But the thing is I'm not able to get user's information. It shows only statuses.

[
{
"id": 22,
"uid": 3,
"status": "test now",
"created_at": "2015-05-05 11:30:53",
"updated_at": "2015-05-05 11:30:53",
"user": null
},
{
"id": 21,
"uid": 2,
"status": "hello!",
"created_at": "2015-05-05 08:43:40",
"updated_at": "2015-05-05 08:43:40",
"user": null
},
{
"id": 20,
"uid": 2,
"status": "test1",
"created_at": "2015-05-05 00:44:30",
"updated_at": "2015-05-05 00:44:30",
"user": null
},
{
"id": 19,
"uid": 2,
"status": "test2",
"created_at": "2015-05-05 00:44:04",
"updated_at": "2015-05-05 00:44:04",
"user": null
},
{
"id": 18,
"uid": 2,
"status": "test3",
"created_at": "2015-05-04 23:38:34",
"updated_at": "2015-05-04 23:38:34",
"user": null
},
{
"id": 17,
"uid": 2,
"status": "test4",
"created_at": "2015-05-04 23:11:47",
"updated_at": "2015-05-04 23:11:47",
"user": null
},
{
"id": 16,
"uid": 3,
"status": "test5",
"created_at": "2015-05-04 23:10:59",
"updated_at": "2015-05-04 23:10:59",
"user": null
},
{
"id": 14,
"uid": 3,
"status": "test7",
"created_at": "2015-05-04 20:53:53",
"updated_at": "2015-05-04 20:53:53",
"user": null
},
{
"id": 12,
"uid": 3,
"status": "test9",
"created_at": "2015-05-04 20:52:13",
"updated_at": "2015-05-04 20:52:13",
"user": null
}
]
cipsas's avatar

do you add

public function user() {
        return $this->belongsTo('App\User');
    }

into Statuses.php ?

abhishek009's avatar

Yes sir! @cipsas

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class statuses extends Model {

    protected $table = 'statuses';
    protected $fillable = ['uid','status','created_at','updated_at'];
    public function user() {
        return $this->belongsTo('App\User');
    }
}
cipsas's avatar

Try to add second argument 'uid':

public function user() {
        return $this->belongsTo('App\User', 'uid');
    }
abhishek009's avatar

Yes. I solved it by adding uid. Thanks for the all helps. :) Seems I really need to struggle.

And yes no need to use orderby. I found a function called latest() which will get latest contents.

Please or to participate in this conversation.