2

First thing, I have changed the Blade syntax to be using {= =} instead of {{ }}.

I have data stored across multiple tables and pivot tables.

The model "Template" has restful routes, I have created the "create" and "index" pages and am able to store the data successfully.

Relationships go as follow:

A Template can have multiple blocks, variables and layouts.

Tables:

  • templates table with fields ID, name
  • block_template pivot table with ID, block_id, template_id
  • template_variable pivot table with ID, template_id, variable_id
  • layout_template pivot table with ID, layout_id, template_id

My question is: How can I load the data in edit mode for stuff that is stored across pivot tables ? I can get the "name" field to display because it is stored on the template table, but how do I access the stuff that is on the pivot tables?

Can I refer to multiple models using the form Helper

My Template Model:

<?php

class Template extends Eloquent {
protected $guarded = array();

public static $rules = array();

public function variables() {
    return $this->belongsToMany('Variable');
}
public function blocks() {
    return $this->belongsToMany('Block');
}
public function layout() {
    return $this->belongsToMany('Layout');
}
public function accounts() {
    return $this->belongsTo('Account');
}

}

My create.blade.php:

<div class="row">




    <h1>Create a new Template</h1>

        {= Form::open(array('route' => 'templates.store', 'files'=> true)) =}
        {= Form::label('name', 'Template Name:'); =}{= Form::text('name', '', array('id' => 'name', 'placeholder' => 'Name', 'class' => 'form-control')); =}<br />

    <h2>Choose blocks</h2>

        @foreach($blocks as $block)
        {= Form::checkbox('blocks[]', $block->id); =} {= $block->title =}<br />
        @endforeach

    <h2>Choose variables</h2>

        @foreach($variables as $variable)
        {= Form::checkbox('variables[]', $variable->id); =} {= $variable->name =}
        @endforeach

    <h2>Choose a layout</h2>

        @foreach($layouts as $layout)
        {= Form::select('layout', array($layout->id => $layout->name)); =}
        @endforeach
        <br /><br />


        {=Form::submit("Save as a template",array('class' => 'btn btn-primary'))=}

        {= Form::close() =}




</div>

My Templates Controller:

public function edit($slug)
{

    $template = Template::where('slug', '=' , $slug)->firstOrFail();
    $blocks = getAccount()->blocks;
    $chosenBlocks = DB::table('block_template')->where('template_id', '=', $template['id'])->get();
    $variables = getAccount()->variables;
    $chosenVariables = DB::table('template_variable')->where('template_id', '=', $template['id'])->get();
    $layouts = getAccount()->layouts;
    $chosenLayout = DB::table('layout_template')->where('template_id', '=', $template['id'])->pluck('layout_id');
        return View::make('templates.edit', compact('template', 'blocks', 'layouts', 'variables', 'chosenBlocks', 'chosenVariables', 'chosenLayout'));
}

Not sure if anything is needed ?

My edit.blade.php:

<h1>Edit Template</h1>

    {= Form::model($template, array('route' => array('templates.update', $template->id), 'method' => 'put', 'files'=> true)) =}


    {= Form::label('name', 'Template Name:'); =}{= Form::text('name'); =}<br />

    <h2>Choose blocks</h2>

        @foreach($blocks as $block)
        {= Form::checkbox('blocks[]', $block->id); =} {= $block->title =}<br />
        @endforeach

    <h2>Choose variables</h2>

        @foreach($variables as $variable)
        {= Form::checkbox('variables[]', $variable->id); =} {= $variable->name =}
        @endforeach

    <h2>Choose a layout</h2>

        @foreach($layouts as $layout)
        {= Form::select('layout', array($layout->id => $layout->name)); =}
        @endforeach
        <br /><br />

    {=Form::submit("Update",array('class' => 'btn btn-primary'))=}

    {= Form::close() =}

So like I said, only the name gets populated, and I would like to populate the checkboxes with the value that are stored in the pivot tables, can someone point me in the right direction?

UPDATE I could do the following (it's not using model binding for the other fields though)

edit.blade.php:

@foreach($blocks as $block)

        <input type="checkbox" value="{=$block->id=}" name="blocks[]"@foreach($chosenBlocks as $chosenBlock)@if($block->id == $chosenBlock->block_id) checked @endif @endforeach>{= $block->title =}<br />

        @endforeach

Which marks the right data as checked

Ben Dubuisson
  • 727
  • 13
  • 38

0 Answers0