Skip to content

Nested Resources  #430

Open
Open
@natecorkish

Description

@natecorkish

Synopsis:

The ability to generate nested resources for other models/controllers. For example:

A job can have many comments, but a course could also have many comments, and they would use a Comment model, with a commentable morph (which we can do already), now to comment on a job with Laravel you can do:

web.php

Route::resource('job.comment', JobCommentController::class);

JobCommentController.php

class JobCommentController extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param \App\Models\Job $job
     * @param \App\Models\Comment $comment
     * @return \Illuminate\Http\Response
     */
    public function store(CommentRequest $request, Job $job, Comment $comment)
    {
        $comment = $job->comments()->create(array_merge([
            'owner_type' => 'user',
            'owner_id' => Auth::user()->id
        ], $request->validated()));

        return Redirect::back()->withSuccess('You have added a comment to this job');
    }
}

Currently we don't have a way to do this. Would this be something you're open to?

Proposed Syntax:

    # Job Comment Controller
    Job/JobComment:
        resource: store, destroy
        model: job
        nested: comment
        validation: comment/comment

So, the model is the resource model It is nested for, and nested is the model it uses to store/delete data, the validation is the file It generates for validation. Because the comment is used for multiple resources, it doesn't make sense to add that under the Requests/Job/ folder but the nested model's own folder, so you could use CommentStoreValidation for both Jobs and courses or whatever other model can be commented on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions