postman {“message”:“未授权,请登录创建帖子,”

4ioopgfo  于 11个月前  发布在  Postman
关注(0)|答案(1)|浏览(120)

我想用 Postman 发一个帖子,但每次我发一个帖子,它都说“未经授权。请登录创建一个帖子”,但我已经登录了,这是laravelcode postcontroller
在postcontroller上,store方法被定位为之前的问题, Postman 显示我试图在auth()->user()行访问null id,因此我创建了另一个方法来首先检查用户是否登录以避免该错误,但现在错误显示我没有登录,尽管登录了

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\post;

class postController extends Controller
{
    public function index()
    {
        return response([
            'posts' => post::orderBy('created_at', 'desc')
                ->with('user:id,image', 'name')
                ->withCount('comments', 'likes')->get(),
        ], 200);
    }


    public function show($id)
    {
        return response(['post' => post::where('id', $id)->withCount('likes', 'comments')->get()], 200);
    }

    //create a post
    public function store(Request $request)
    {
        $attrs = $request->validate([
            'body' => 'required|string',
        ]);

        if (auth()->check()) {
            $user_id = auth()->user()->id;
            $post = post::create([
                'body' => $attrs['body'],
                'user_id' => $user_id,
            ]);

            // $post = post::create([
            //     'body' => $attrs['body'],
            //     'user_id' => auth()->user()->id,
            // ]);

            return response([
                'message' => 'post created',
                'post' => $post
            ], 200);
        } else {
            return response([
                'message' => 'Unauthorized. Please log in to create a post.'
            ], 401);
        }

    }

    public function update(Request $request, $id)
    {
        $post = post::find($id);
        if (!$post) {
            return response([
                'message' => 'not found'
            ], 403);
        }

        if ($post->user_id != auth()->user()->id) {
            return response([
                'message' => 'permission denied'
            ], 403);
        }

        $attrs = $request->validate([
            'body' => 'required|string',
        ]);

        $post->update([
            'body' => $attrs['body']
        ]);

        return response([
            'message' => 'post updated',
            'post' => $post
        ], 200);
    }

    public function destroy($id)
    {
        $post = post::find($id);
        if (!$post) {
            return response([
                'message' => 'not found'
            ], 403);
        }

        if ($post->user_id != auth()->user()->id) {
            return response([
                'message' => 'permission denied'
            ], 403);
        }

        $post->coments()->delete();
        $post->like()->delete();
        $post->delete();

        return response([
            'message' => 'post deleted',
        ], 200);

    }

}

字符串
post模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\comment;
use App\Models\like;
use App\Models\User;

class post extends Model
{
    use HasFactory;
    protected $fillable = [
        'user_id',
        'body',
        'image'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);

    }
    public function comments()
    {
        return $this->hasMany(comment::class);
    }

    public function likes()
    {
        return $this->hasMany(like::class);
    }
}


post表

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id');
            $table->string('body');
            $table->string('image')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('post');
    }
};

nwlls2ji

nwlls2ji1#

请确保您的路由中有中间件('auth:sanctum')。因此,它可能是这样的:

Route::group(
    ['middleware' => 'auth:sanctum'],
    function () {
        Route::post('/posts', [PostsController::class, 'create']);
    }
);

字符串

相关问题