Skip to content

Commit

Permalink
Merge pull request #1027 from SlovakNationalGallery/MG-109
Browse files Browse the repository at this point in the history
[article] filter by user
  • Loading branch information
rastislav-chynoransky authored Nov 9, 2024
2 parents b94077b + 1fcf103 commit 20da583
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
13 changes: 9 additions & 4 deletions app/Http/Controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

use App\Article;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

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());
}

/**
Expand Down Expand Up @@ -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')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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::table('articles', function (Blueprint $table) {
$table->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');
});
}
};

0 comments on commit 20da583

Please sign in to comment.