lundi 29 février 2016

Laravel 5 - passinf data to view when they are null

I have a Model called Data. My Data Model has a show page. On this show page I have tabs where data related to this Model can be inputted. So the show view for Data looks something like this.

<div id="cInfo" class="tab-pane in active fade">
    @if (count($dType) == 0)
        @include('forms.dTypes.dtypes')
    @else
        @include('forms.dTypes.dtypesEdit')
    @endif
</div>
<div id="cAndC" class="tab-pane fade">
    @if (count($dCreative) == 0)
        @include('forms.dCreatives.dcreatives')
    @else
        @include('forms.dCreatives.dcreativesEdit')
    @endif
</div>

dType and dCreative are Models that belong to Data. Relationships have been set up appropiately. Now I am having an issue in my DataController, specifically the show function. At the moment I have something like so

public function show(Data $data)
{
    $dataManager = User::select('userName')->where('id', $data->userId)->first();
    $clientName = Client::select('clientName')->where('id', $data->clientId)->first();

    $dType = DataType::where('dataId', '=', $data->id)->get();

    $dCreative = CampaignCreatives::where('dataId', '=', $data->id)->first();

    return View::make('campaigns.show', compact('data', 'dataManager', 'clientName', 'dType', 'dCreative'));
}

My first problem is with dType and dCreative. So when a Data Model is first added, dType and dCreative have not yet been created.
Therefore, when I get to the show page for Data, I get the error Trying to get property of non-object. These two Collections should only be passed to the view if they have been created.

What would be the best way to achieve this?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire