jeudi 17 août 2017

Laravel 5.4 User::find($id)->first() always returns first found entry

Strange problem that I've been trying to troubleshoot. NO core laravel (5.4) files have been changed, they still have the original install date for the date last touched on the server, but this problem just started to happen today.

$id = 3;
$getgroup = new \App\Group;
$group = $getgroup->find($id);

This result is that the group with id 3 is found.

$id = 3;
$getgroup = new \App\Group;
$group = $getgroup->find($id)->first();

This returns id 1.

While troubleshoot I tried this:

$id = 3;
$getgroup = new \App\Group;
$group = $getgroup->find($id);
print_r($group);
echo $group->first()->toSql();

The print_r confirms that the group with id 3 was returned. The toSql returns this:

select * from `groups`

Now, the weird part, this works:

$group2 = DB::table('groups')->where('id', '=', $id)->first();

So it looks like there is something with my model that's wrong. Here is the full model file:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Group extends Model
{
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
}

So, somehow in the last day the first() function in my models completely quit working. I found the file that defines first and it looks unchanged. I also went to a different installation of my application that is installed on the same server, but several days behind my current dev code base, and it too shows the same problem.

I have no idea what's going on here. Any ideas why the first is not returning just the first found entry and is instead rerunning a new query looking for everything?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2w6STOp
via IFTTT

Aucun commentaire:

Enregistrer un commentaire