katie hudson
katie hudson

Reputation: 2893

Blade conditional statements

I have several models. Each model has a create and edit page. If the model has not been created yet, I need to display its create page. If it has been created, I need to show its edit page.

So far I have this which works

<div class="panel-group" id="accordion">
    @if (is_null($project->projectType))
        @include ('projects.partials.projectTypesCreate')
    @else
        @include ('projects.partials.projectTypesEdit')
    @endif

    @if (is_null($project->projectData))
        @include ('projects.partials.projectDataCreate')
    @else
        @include ('projects.partials.projectDataEdit')
    @endif

    @if (is_null($project->projectTesting))
        @include ('projects.partials.projectTestingCreate')
    @else
        @include ('projects.partials.projectTestingEdit')
    @endif
</div>

Now, the above is not the prettiest, to many conditional statements in my opinion. What makes this worse is the fact that I need to display things in order. By this I mean that if projectType is null, only projectTypesCreate should be displayed. If it is not null. projectTypesEdit and projectDataCreate should be displayed, and so on.

To accomplish this I could potentially do something like so

@if (is_null($project->projectType))
    @include ('projects.partials.campaignTypes')
@else
    @include ('projects.partials.campaignTypesEdit')
@endif

@if (!is_null($project->projectType) && is_null($project->projectData))
    @include ('projects.partials.projectDataCreate')
@else
    @include ('projects.partials.projectDataEdit')
@endif

@if (!is_null($project->projectData) && is_null($project->projectTesting))
    @include ('projects.partials.projectTestingCreate')
@else
    @include ('projects.partials.projectTestingEdit')
@endif

Once again though, it is getting a bit clunky and it is not accurate as to what I need because the edit page will display when the condition fails.

So really I am just after some advice as to how to best handle this situation. I need to determine what view to display for each model, whilst at the same time displaying views sequentially.

Thanks

Upvotes: 0

Views: 3278

Answers (2)

Harry Loyd
Harry Loyd

Reputation: 429

I'd have a look at RESTful routing in laravel. For one line in your routes file, several routes will be generated to allow you to perform CRUD operations on a model.

https://laravel.com/docs/5.1/controllers#restful-resource-controllers

Then if you need to reuse form elements you could create partials that are included on multiple views. Its a bit more work to set up but is easy to edit later on.

Upvotes: 1

Joseph Silber
Joseph Silber

Reputation: 219930

Create a single $action variable that holds the action you want to do, then use that in your @include statements:

<div class="panel-group" id="accordion">
    @php($action = is_null($project->projectType) ? 'Create' : 'Edit')

    @include('projects.partials.projectTypes'.$action)
    @include('projects.partials.projectData'.$action)
    @include('projects.partials.projectTesting'.$action)
</div>

Upvotes: 1

Related Questions