lundi 2 août 2021

How to compare the current auth_id with id passed as foreign key in some table in laravel 8?

I am facing an issue while comparing the current auth:id with users_id passing as foreign key in Post Model: here is the migration of Post :

 Schema::create('posts', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('description');
        $table->string('Image');
        $table->integer('user_id');
        $table->timestamps();
    });

and while deleting the particular post from blade say, an user has created that post , now he wants to delete that post. I have used like this in delete method in PostController :

public function destroy($id)
{
    $Post = Post::where('id', $id)->pluck('user_id');
    
    $user_id = Auth::id();
    // dd($user_id);

    if ($Post == $user_id) {
        // dd('hii');
        $Post->delete();
    }
    else{
        abort('you are not authorized to delete this');
    }
    return redirect()->back()->with('success');
}

here I am getting an auth:id and user_id from post table : I am getting user_id like this while doing dd :

Illuminate\Support\Collection {#1252 ▼
  #items: array:1 [▼
    0 => 1
   ]
  }

,but I am not able to convert in single integer value to compare to auth:id . Thus,Anyone know how to compare them with the current auth:id ??

Aucun commentaire:

Enregistrer un commentaire