editing joined

This commit is contained in:
Marley Rae 2022-04-26 18:43:36 -07:00
parent 239e628b11
commit 1b8da73604
8 changed files with 150 additions and 71 deletions

View file

@ -4,7 +4,6 @@
use App\Http\Requests\StoreJoinedRequest;
use App\Http\Requests\UpdateJoinedRequest;
use App\Models\Category;
use App\Models\Joined;
class JoinedController extends Controller
@ -26,15 +25,7 @@ public function create()
public function store(StoreJoinedRequest $request)
{
$validated = $request->safe()->only([
'categories',
'url',
'subject',
'image',
'approved',
]);
Joined::store($validated);
Joined::store($request->validated());
return redirect()->route('admin.joined.index')->with('success', 'Fanlisting added.');
}
@ -46,6 +37,9 @@ public function edit(Joined $joined)
public function update(UpdateJoinedRequest $request, Joined $joined)
{
$joined->patch($request->validated());
return redirect()->route('admin.joined.index')->with('success', 'Fanlisting updated.');
}
public function destroy(Joined $joined)

View file

@ -4,7 +4,6 @@
use App\Models\Joined;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
class StoreJoinedRequest extends FormRequest
{
@ -27,11 +26,11 @@ public function rules()
{
return [
'categories' => ['required', 'array'],
'categories.*' => [ 'numeric', 'exists:categories,id'],
'url' => ['required', 'url'],
'subject' => ['required', 'string'],
'image' => ['nullable', 'image'],
'approved' => ['nullable', 'boolean'],
'categories.*' => ['numeric', 'exists:categories,id'],
'url' => ['required', 'url'],
'subject' => ['required', 'string'],
'image' => ['nullable', 'image'],
'approved' => ['nullable', 'boolean'],
];
}
}
}

View file

@ -8,23 +8,24 @@ class UpdateJoinedRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
public function authorize() : bool
{
return false;
return $this->user()->can('update', $this->route('joined'));
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
public function rules() : array
{
return [
//
'categories' => ['required', 'array'],
'categories.*' => ['numeric', 'exists:categories,id'],
'url' => ['required', 'url'],
'subject' => ['required', 'string'],
'image' => ['nullable', 'image'],
'approved' => ['nullable', 'boolean'],
];
}
}

View file

@ -3,44 +3,81 @@
namespace App\Models;
use App\Traits\Categorizable;
use App\Traits\Imageable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
class Joined extends Model
{
use HasFactory, Categorizable;
use HasFactory;
use Categorizable;
use Imageable;
protected $table = 'joined';
protected $table = 'joined';
protected $casts = [
'approved' => 'boolean',
];
protected $fillable = [
'url',
'subject',
'approved',
'image',
];
/* --------------------------------------------------------------------------- relationships ---- */
protected $casts = [
'approved' => 'boolean',
];
public function collective()
{
return $this->belongsTo(Collective::class);
}
/* ----------------------------------------------------------------------- relationships ---- */
// injected by trait: categories (morph many-to-many)
public function collective()
{
return $this->belongsTo(Collective::class);
}
/* ---------------------------------------------------------------------------------- joined ---- */
// injected by trait: categories (morph many-to-many)
public static function store($request) : Joined
{
$joined = new Joined();
$joined->url = $request['url'];
$joined->subject = $request['subject'];
$joined->image = Storage::putFile('joined', $request['image']);
$joined->approved = $request['approved'] ?? false;
/* -------------------------------------------------------------------------- attributes ---- */
$collective = auth_collective();
$collective->joined()->save($joined);
protected function approved() : Attribute
{
return Attribute::make(
set: fn ($value) => isset($value) ? $value = $value : $value = false,
);
}
$joined->categories()->sync($request['categories']);
/* ------------------------------------------------------------------------------ joined ---- */
return $joined;
}
public static function store(array $validated) : Joined
{
$validated['image'] = $validated['image'] ?? null;
$validated['image'] = self::imagePath($validated['image']);
$validated['approved'] = $validated['approved'] ?? false;
$joined = auth_collective()->joined()->create($validated);
$joined->categories()->sync($validated['categories']);
return $joined;
}
/* ------------------------------------------------------------------------------- patch ---- */
public function patch(array $validated) : Joined
{
$validated['image'] = $validated['image'] ?? null;
$this->url = $validated['url'];
$this->subject = $validated['subject'];
$this->image = $this->updateImage($validated['image']);
$this->approved = $validated['approved'] ?? false;
if ($this->isDirty()) {
$this->save();
}
if (isset($validated['categories'])) {
$this->categories()->sync($validated['categories']);
}
return $this;
}
}

29
app/Traits/Imageable.php Normal file
View file

@ -0,0 +1,29 @@
<?php
namespace App\Traits;
use Illuminate\Support\Facades\Storage;
trait Imageable
{
public static function imagePath($image) : ?string
{
$path = strtolower(static::class);
if (isset($image)) {
return Storage::putFile($path, $image);
} else {
return null;
}
}
public function updateImage($image) : ?string
{
if (isset($this->image) && isset($image)) {
Storage::delete($this->image);
self::imagePath($image);
} else {
return null;
}
}
}

View file

@ -4,33 +4,28 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('joined', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->foreignId('collective_id')
->constrained('collectives')
->onUpdate('cascade')
->onDelete('cascade');
$table->string('url');
$table->string('subject');
$table->string('image');
$table->boolean('approved');
$table->foreignId('collective_id')
->constrained('collectives')
->onUpdate('cascade')
->onDelete('cascade');
$table->string('url');
$table->string('subject');
$table->string('image')->nullable();
$table->boolean('approved');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{

View file

@ -4,21 +4,45 @@
use App\Models\Category;
use App\Models\Joined;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class JoinedSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Joined::factory()
->count(50)
->hasAttached(Category::inRandomOrder()->limit(rand(0,10)))
->create();
->count(50)
->create();
$pivots = [];
$cats = Category::inRandomOrder()->select('id')->get();
$i = 1;
while ($i <= 50) {
$pivots[] = [
'category_id' => $cats->random()->id,
'categorizable_id' => $i,
'categorizable_type' => 'joined',
];
++$i;
}
$i = 1;
while ($i <= rand(20, 100)) {
$pivots[] = [
'category_id' => $cats->random()->id,
'categorizable_id' => rand(1, 50),
'categorizable_type' => 'joined',
];
++$i;
}
DB::table('categorizables')->insert($pivots);
}
}

View file

@ -8,7 +8,7 @@
@section('content')
<form action="{{ route('admin.joined.edit', $joined) }}" method="POST" autocomplete="off">
<form action="{{ route('admin.joined.update', $joined) }}" method="POST" autocomplete="off">
@csrf
@method('PATCH')