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

Antonella's avatar

permission User for Article

I have two tables User and Article how can I show a certain user only his articles? and not the articles of other users?

0 likes
9 replies
lacasera's avatar

It depends on how your database is structured. But you can do ‘’’ $userArticles = Article::where(‘user_id’, ‘=‘, auth()->id())->get(); ‘’’

Antonella's avatar

my db has following structure:

Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->text('img')->nullable();
            $table->rememberToken();
            $table->string('api_token', 64)->unique()->default(Str::random(50));
            $table->timestamps();

        });

Schema::create('articles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->text('title');
            $table->text('body');
            $table->text('img')->nullable();
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
        });

what I was wondering was how to modify Nova's Resources to do what I was interested in

Antonella's avatar

I put this feature in the resource but I don't even see an article anymore

public static function indexQuery(NovaRequest $request, $query)
{
    return $query->where('user_id',$request->user()->id);
}
Maria30's avatar

You probably need to create an articles policy and return true in every function. Watch the whole video, I just pointed out the time to see the feature you need.

Could you show the Nova Article resource?

1 like
Antonella's avatar

namespace App\Nova;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\DateTime;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Trix;
use Spatie\NovaTranslatable\Translatable;

use Laravel\Nova\Http\Requests\NovaRequest;

class Articles extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\Article::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'id';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id', 'user_id', 'title'
    ];

    public static function indexQuery(NovaRequest $request, $query)
    {
        return $query->where('user_id',auth()->id());
    }

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make(__('ID'), 'id')->sortable(),

            Translatable::make([
                Text::make('Title'),
                Text::make('Body'),
            ]),

//            DateTime::make('created_at'),
//            DateTime::make('updated_at'),
            BelongsTo::make('User'),

            Text::make('img'),

        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}
Maria30's avatar

you have

public static function indexQuery(NovaRequest $request, $query)
    {
        return $query->where('user_id',auth()->id());
    }

change it to:

public static function indexQuery(NovaRequest $request, $query)
{
    return $query->where('user_id', $request->user()->id);
}

Please or to participate in this conversation.