vendredi 2 décembre 2016

Laravel: How can I divide different types of the same model in my view

I have an Athlete model, and an Evaluation model.

Athlete

/**
 * An athlete has an evaluation.
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function evaluation()
{
    return $this->hasOne('App\Evaluation');
}

Evaluation

/**
 * An evaluation belongs to an athlete.
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function athlete()
{
    return $this->belongsTo('App\Athlete');
}

Here is my controller:

/**
 * Display the specified resource.
 *
 * @param Athlete $athlete
 * @return $this
 */
public function show(Athlete $athlete)
{
    return view('athletes.show')->with(compact('athlete'));
}

Within each Evaluation I have a status column which is an enum.

draft
published
revision

An athlete can only have 1 evaluation that is published at any given time. An athlete can only have 1 evaluation draft at any given time.

An athlete can have many revisions. A revision is just an archived version of the previous evaluation when a new one is saved.

I'm not sure how to separate out the versions in my view. For example, I only want the published version to populate a textarea.

<textarea class="form-control evaluation" rows="3" cols="5" id="evaluation" name="comments">
    
</textarea>

So I think I need something like:



But I know that is incorrect.

The same could be said for showing the revisions in a list somewhere on my page.

@foreach($athlete->evaluation->comments::where('status', 'revision')...

Or are these objects I need to separately set up in my controller as evaluation, drafts, revisions?

Then in my controller have:

return view('athletes.show')->with(compact('athlete', 'evaluation', 'drafts', 'revisions'));

That seems like a waste of resources though since I've already got everything I need.

Thank you for any suggestions!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire