lundi 21 septembre 2015

Laravel belongsToMany and Eloquent query

I'm trying to get a list of the ID's of the users who read a particular magazine.

things table (magazines)

id
name

thing_progresses

id
progress_value

thingprogress_thing

id
thing_progress_id (FK from thing_progresses table)
thing_id (FK from things table)
user_id (FK from users table)

Thing Model

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Thing extends Model
{
    public function readers() {
        return $this->belongsToMany('\App\Models\ThingProgress', 'thingprogress_thing');
    }
}

My application

$readers = Thing::with(array('readers' => function($query) use ($id) {
$query->where('thing_progress_id', '1');
}))->find($id);

View

@foreach($readers as $reader)
    {{$reader->user_id}}
@endforeach

Here's the error I got.

Trying to get property of non-object (View: /Applications/MAMP/htdocs/master/resources/views/frontend/thing/book/index.blade.php)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire