It depends on how your database is structured. But you can do ‘’’ $userArticles = Article::where(‘user_id’, ‘=‘, auth()->id())->get(); ‘’’
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?
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
@gianmarx this video has exactly what you need (at 7:50)
https://laracasts.com/series/laravel-nova-mastery/episodes/7
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);
}
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?
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 [];
}
}
First check your database if user_id has any value.
If not, check https://nova.laravel.com/docs/3.0/resources/fields.html#hidden-field
Combined with default values, Hidden fields are useful for passing things like related ID's to your forms:
Hidden::make('User', 'user_id')->default(function ($request) {
return $request->user()->id;
})
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.