Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marcelwinter's avatar

Simple API with a token

Hello, i'm new to laravel and wanted to make a simple API with a token.

So i've already expanded the user database with an api column and everything works great when i search for an user with his token. But now i want to create another table named weather and do the same here, but no matter what i do, i'm always getting an empty array back.

So, what i've done already ;

Created a route in api.php:

Route::middleware('auth:api')->get('/weather', function(Request $request) {
    return $request->user();
});

Weather.php

    public function toArray($request)
    {
        return parent::toArray($request);
    }

created the db :

        Schema::create('weathers', function (Blueprint $table) {
            $table->string('plantID');
            $table->unsignedBigInteger('user_id');
            $table->string('temp');
            $table->string('humidity');
            $table->string('pressure');
            $table->string('api_token', 80)->unique();
            $table->timestamps();

            $table->index('user_id');
        });

I think i must change something in the auth.php, but i'm not really sure what..

I've watch so many videos but nothing really helped me, so hopefully you can change this.

0 likes
1 reply
marcelwinter's avatar

Hi, solved it by my self.

I just used the normal auth:api from users and created a relationship to the weather table. In the request i'm returning the weather data. Thats it.

Please or to participate in this conversation.