mardi 23 février 2016

Laravel 5 - Issue with design and data in edit page

I am having issues with one of my edit pages, and I am not too sure if it is down to my database design. I have set up the following JSFiddle which demonstrates my create form

Now my first schema captures the simple information from that page, basically just the value of the radio button choice and the date input.

Schema::create('campaign_creatives', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('templateOptions')->default('')->nullable();
    $table->date('arrival')->nullable();
    $table->timestamps();
});

Now if you select a number from one of the select boxes, you will see that number of text inputs appear. It is the data within this I am interested in. Additionally, if you select the Dynamic radio button, additional inputs appear. Furthermore, if you select Other, a text area appears to provide further details. SO I wanted a way to capture all this information, and I created the following table

Schema::create('campaign_creatives_data', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('name')->default('')->nullable();
    $table->string('label')->default('')->nullable();
    $table->longText('value')->default('')->nullable();
    $table->integer('campaignCreativesId')->unsigned()->default(0);
    $table->foreign('campaignCreativesId')->references('id')->on('campaign_creatives')->onDelete('cascade');
    $table->timestamps();
});

The name would represent the thing I am capturing, while the label and value represents the input label and value. After all this, when I save data, it looks a bit like the following

campaign_creatives
+----+--------------+-----------------+------------+---------------------+---------------------+
| id | campaignType | creativeArrival | campaignId | created_at          | updated_at          |
+----+--------------+-----------------+------------+---------------------+---------------------+
| 14 | Dynamic      | 2016-02-25      |          2 | 2016-02-23 15:56:43 | 2016-02-23 15:56:43 |
+----+--------------+-----------------+------------+---------------------+---------------------+

campaign_creatives_data
+----+----------------+-------------------+--------------+---------------------+---------------------+---------------------+
| id | name           | label             | value        | campaignCreativesId | created_at          | updated_at          |
+----+----------------+-------------------+--------------+---------------------+---------------------+---------------------+
| 62 | creativeOption | Other             | dfsdfsdfsdf  |                  14 | 2016-02-23 16:00:01 | 2016-02-23 16:00:01 |
| 60 | creativeOption | checkboxSelection | Pizza        |                  14 | 2016-02-23 16:00:01 | 2016-02-23 16:00:01 |
| 61 | creativeOption | checkboxSelection | Lemondade    |                  14 | 2016-02-23 16:00:01 | 2016-02-23 16:00:01 |
| 59 | creativeNumber | Food2             |              |                  14 | 2016-02-23 16:00:01 | 2016-02-23 16:00:01 |
| 57 | creativeNumber | Drink2            | Some input   |                  14 | 2016-02-23 16:00:01 | 2016-02-23 16:00:01 |
| 58 | creativeNumber | Food1             | Some input   |                  14 | 2016-02-23 16:00:01 | 2016-02-23 16:00:01 |
| 56 | creativeNumber | Drink1            | Some input   |                  14 | 2016-02-23 16:00:01 | 2016-02-23 16:00:01 |
+----+----------------+-------------------+--------------+---------------------+---------------------+---------------------+

creativeOption represents the checkboxes, creativeNumber represents the data relating to Number of Choices. So to produce the above in the FIddle, I would choose 2 for Food and Drink and in the text boxes that appear enter Some input (apart from one which is left empty). I would choose Dynamic and select the Pizza, Lemonade and Other checkboxes. In the textarea for the Other checkbox, I entered dfsdfsdfsdf (so for the other checkbox, I am interested in the textbox data rather than the checkbox value).

In regards to my Models, a CampaignCreatives can have many CampaignCreativesData. So everything for the creating side of things works great. The issue comes with the edit page, seeing that a lot of the display is dynamic. I pass the view this

$campaignCreative = CampaignCreatives::where('campaignId', '=', $campaign->id)->first();

I use first because there can only be one CampaignCreatives. Within my view, I now have access to all that data displayed above. Here is an example. On my edit page, I have the following which represents the Number of Options select boxes.

<div class="form-group">
    <div class="row">
        <div class="col-md-12">
            <h3>Number of Options</h3>
            <div class="col-md-3 noPadding">
                {!! Form::label('cFood', 'Food:', array('class' => '')) !!}
                {!! Form::select('cFood', ['0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7'], null, ['class' => 'cSelectType']) !!}
            </div>
            <div class="col-md-3">
                {!! Form::label('cDrink', 'Drink:', array('class' => '')) !!}
                {!! Form::select('cDrink', ['0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7'], null, ['class' => 'cSelectType']) !!}
            </div>
            <div class="col-md-3">
                {!! Form::label('cOther', 'Other:', array('class' => '')) !!}
                {!! Form::select('cOther', ['0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7'], null, ['class' => 'cSelectType']) !!}
            </div>
        </div>
    </div>
</div>

From the above data, I know that there are 4 creativeNumber data, 2 for Food and 2 for Drink. Therefore, I need the text inputs displayed for these with their value. How would I go about doing something like this?

I know this is a lot of information, I would be so greatful if someone could guide me in the correct direction.

Many thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire