diff --git a/app/Article.php b/app/Article.php index 8d795560a..b84a525a2 100644 --- a/app/Article.php +++ b/app/Article.php @@ -83,6 +83,11 @@ public function category() return $this->belongsTo(\App\Category::class); } + public function user() + { + return $this->belongsTo(User::class); + } + public function getUrl() { return URL::to('clanok/' . $this->attributes['slug']); diff --git a/app/Http/Controllers/ArticleController.php b/app/Http/Controllers/ArticleController.php index 07c5e02bb..7e3c062b1 100644 --- a/app/Http/Controllers/ArticleController.php +++ b/app/Http/Controllers/ArticleController.php @@ -4,6 +4,7 @@ use App\Article; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Session; @@ -11,13 +12,16 @@ class ArticleController extends Controller { /** * Display a listing of the resource. - * - * @return Response */ public function index() { - $articles = Article::orderBy('created_at', 'desc')->get(); - return view('articles.index')->with('articles', $articles); + $articles = Article::orderBy('created_at', 'desc'); + + if (Gate::denies('administer')) { + $articles = $articles->where('user_id', '=', auth()->id()); + } + + return view('articles.index')->with('articles', $articles->get()); } /** @@ -71,6 +75,7 @@ public function store(Request $request) $article->title_shadow = $request->input('title_shadow'); } + $article->user_id = auth()->id(); $article->save(); if ($request->hasFile('main_image')) { diff --git a/database/migrations/2024_10_17_164642_add_user_id_to_articles_table.php b/database/migrations/2024_10_17_164642_add_user_id_to_articles_table.php new file mode 100644 index 000000000..3c2c95054 --- /dev/null +++ b/database/migrations/2024_10_17_164642_add_user_id_to_articles_table.php @@ -0,0 +1,31 @@ +integer('user_id')->unsigned()->nullable(); + $table->foreign('user_id') + ->references('id') + ->on('users'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('articles', function (Blueprint $table) { + $table->dropColumn('user_id'); + }); + } +};