lundi 5 février 2018

Getting posts by users I am following

So, a user can follow multiple people, who can create multiple posts, I am morphing my posts, because multiple items can create posts,

So, my Timeline relationship to the user is like so:

/**
 * Get the owner.
 *
 * @return 
 *  App\Modules\Account\Entites\User
 *  App\Modules\Groups\Entites\Groups
 *  App\Modules\Pages\Entites\Pages
 *  App\Modules\Events\Entites\Events
 */
public function owner()
{
    return $this->morphTo('owner');;
}

Timeline table:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('timeline', function (Blueprint $table) {
        $table->increments('id');
        $table->text('status')->nullable();
        $table->string('location')->nullable();
        $table->string('feeling')->nullable();
        $table->string('type')->nullable();
        $table->string('privacy')->default('public');
        $table->integer('owner_id');
        $table->string('owner_type');
        $table->timestamps();
        $table->softDeletes();
    });
}

Now, my user can follow other users, and pages, so that, too is being morphed like so:

/**
 * Return item followings.
 *
 * @param string $class
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function followings($class = __CLASS__)
{
    return $this->morphedByMany($class, config('follow.morph_prefix'), config('follow.followable_table'))
                ->wherePivot('relation', '=', Follow::RELATION_FOLLOW)
                ->withPivot('followable_type', 'relation', 'created_at');
}


/**
 * Return followers.
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function followers()
{
    return $this->morphToMany(config('follow.user_model'), config('follow.morph_prefix'), config('follow.followable_table'))
                ->wherePivot('relation', '=', Follow::RELATION_FOLLOW)
                ->withPivot('followable_type', 'relation', 'created_at');
}

Following structure:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create(config('follow.followable_table', 'followables'), function (Blueprint $table) {
        $table->unsignedInteger('user_id');
        $table->unsignedInteger('followable_id');
        $table->string('followable_type')->index();
        $table->string('relation')->default('follow')->comment('folllow/like/subscribe/favorite/');
        $table->timestamp('created_at');

        $table->foreign('user_id')
            ->references(config('follow.users_table_primary_key', 'id'))
            ->on(config('follow.users_table_name', 'users'))
            ->onUpdate('cascade')
            ->onDelete('cascade');
    });
}

Now, I am trying to get the statuses(posts) by people I am following:

    $statuses = $this
        ->timeline
        ->whereHas('owner.followers', function ($q) use ($user) {
            return $q->where('id', Auth::user()->id);
        })
        // ->whereHas('owner.likes', function ($q) use ($user) {
        //     return $q->where('id', Auth::user()->id);
        // })
        ->orderBy('timeline.created_at', 'desc')
        ->get();

But this throws an error:

Call to undefined method Illuminate\Database\Query\Builder::followers()

So, my question is, how come I am getting the error when that relationship exists, and how can I fix it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire