mardi 10 décembre 2019

Laravel FollowController relationship error

I am making FollowController where i have two tables following_users table and following_user_item table. which is in hasmany relationship. when a authenticate current_user wants to follow an user, the id of the user will stored in following_users table and its relational table stored the current_user_id and the following_user_id(which is the id of following_users table). here is the schema.

following_users_table:

public function up()
    {
        Schema::create('following_users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')
                  ->references('id')
                  ->on('users');
            $table->timestamps();
        });
    }

following_user_item_table

public function up()
    {
        Schema::create('following_user_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('following_users_id')->unsigned();
            $table->foreign('following_users_id')
                  ->references('id')
                  ->on('following_users');
            $table->bigInteger('user_id')->unsigned();
            $table->timestamps();
        });
    }

I have done the FollowController but the problem is comming when try to check whether the user is already followed or not.

Follow Relationship in User Model

public function followingUserList()
    {
        return $this->hasOne('App\FollowingUser');
    }

    /**
     * Get the stories associated with the user through an intermediate table
     *
     */
    public function followingUsers()
    {
        return $this->hasManyThrough(
            'App\FollowingUserItem',
            'App\FollowingUser',
            null,
            'following_users_id'
        );
    }

FollowinUser Model Relationship with User and FollowingUserItem

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


public function users()
{
    return $this->hasMany('App\FollowingUserItem','following_users_id');
}

Here is my FollowController

class FollowController extends Controller
{
    //

    public function index($id)
    {
        $user = User::find($id);
        $logged_userId = Auth::User();

        if ($user->id == $logged_userId->id) {
            return [
                'status' => false,
                'message' => 'You can not Follow yourself',
            ];
        }
        if ($user && $logged_userId) {
            $checkUsers = FollowingUser::where('user_id', $user->id)->get()->users()->where('user_id', $logged_userId->id);
            if ($checkUsers) 
            {

                    return 'already followed';

            }
            else 
            {
                $user->followingUserList()->save(new FollowingUser())->users()->save(new FollowingUserItem(['user_id' => $logged_userId->id]));
                return 'sucess';
            }
        }
    }       
}

I go the error Method Illuminate\Database\Eloquent\Collection::users does not exist.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire