@bwrigley AdvertStoreRequest needs to extend PostStoreRequest, even tho both AdvertStoreRequest and PostStoreRequest extend FormRequest there is no indication that AdvertStoreRequest is a type of PostStoreRequest
Overriding methods with different parameters
I'm not sure if what I'm trying to achieve in my application is a php and/or Laravel no no, or perhaps just poor design, but I would be grateful for your thoughts.
In my forum application, users can create a Post, they can also create things like Advert,Poll,Question etc which are all types of Post, just with subtly different implementations on the front end and a few other variations.
It seemed sensible to me to make a generic Post model and controller and then extend those to reduce code duplication and then override methods that do require something a bit different.
I was just starting with my PostController::store() method:
class PostController extends Controller
{
protected $postableType;
public function __construct()
{
$this->postableType = PostType::GENERIC;
}
public function store(PostStoreRequest $request): RedirectResponse
{
$validated = $request->validated();
$post = new Post();
$post->message = $validated['message'];
$post->user_id = $request->user()->id;
$post->postable_type = $this->postableType;
$post->save();
return Redirect::route('feed');
}
}
And then began my Advert::store() method which needs slightly different validation rules so I created a different FormRequest :
class AdvertController extends PostController
{
protected $postableType;
public function __construct()
{
$this->postableType = PostType::ADVERT;
}
public function store(AdvertStoreRequest $request): RedirectResponse
{
$validated = $request->validated();
$advert = new Advert();
$advert ->message = $validated['message'];
$advert ->user_id = $request->user()->id;
$advert ->postable_type = $this->postableType;
$advert ->save();
return Redirect::route('feed');
}
}
When I run my unit test to make sure this simple logic works I get a PHP error:
Declaration of App\Http\Controllers\AdvertController::store(App\Http\Requests\AdvertStoreRequest $request): Illuminate\Http\RedirectResponse must be compatible with App\Http\Controllers\PostController::store(App\Http\Requ
ests\PostStoreRequest $request): Illuminate\Http\RedirectResponse
Both AdvertStoreRequest and PostStoreRequest extend FormRequest
Any thoughts would be really helpful!
Please or to participate in this conversation.