hassanz93's avatar

Get value for $id from blade section

I am still new to Laravel so I hope someone could help me with the problem I am facing. I have a controller that updates a row in a table depending on the value of the $id. if $id=1, first row is updated, if $id=2, second row is updated, if $id=3, third row is updated, and so on. Is there a way to pass $id to the blade section and from each form(see blade section) assign $id a value?

Controller

public function index()
{         
    return view("theme-changers.index");
}
public function change(Request $request, Response $response, $id)
{         
    $user = ThemeChanger::find($id);
    $user->update(array('color_code' => $request->input('color_code')));
    return view('theme-changers.index', compact('user', 'id'));;
    // $response->withCookie(cookie('color', $input, 999999));
}

Routes

    Route::get('theme-changers', [ThemeChangerController::class, 'index'])->name('theme-  changers.index');
    Route::post('theme-changers/{id}', [ThemeChangerController::class, 'change'])->name('themes.store');

Blade (theme-changer -> index.blade.php)

{{ Form::model($user, array('route' => array('themes.store, $user->id))) }}

                                    <tr class="odd">
                                        <td>
                                            Primary Color
                                        </td>

                                        <td>
                                            <input style="width:100px" name="color_code" type="color" class="form-control form-control-color" id="themechanger1" title="Choose your color">
                                        </td>

                                        <td class=" text-center">
                                            <div>
                                                <button type="submit" class="image__file-upload" id="saveColorChange1">Confirm</button>
                                                <button type="submit" class="image__file-upload" id="cancelColorChange">Cancel</button>
                                            </div>
                                        </td>
                                    </tr>
                                    {{ Form::close() }}

Model (ThemeChanger)

 /**
 * @var string[]
 */
public $fillable = [
        'color_name',
        'color_code',
        'created_at',
        'updated_at',
];

 /**
 * @var string[]
 */
protected $casts = [
    'color_name' => 'string',
    'color_code' => 'string',
];

}

0 likes
2 replies
automica's avatar
automica
Best Answer
Level 54

A form is not allowed to be a child element of a table, tbody or tr. Attempting to put one there will tend to cause the browser to move the form to it appears after the table (while leaving its contents — table rows, table cells, inputs, etc — behind).

You can have an entire table inside a form. You can have a form inside a table cell. You cannot have part of a table inside a form.

Use one form around the entire table. Then either use the clicked submit button to determine which row to process (to be quick) or process every row (allowing bulk updates).

in answer to your question, if you are planning on having multiple forms, then you can add a hidden field within each form to determine which theme you wish to update.

<input type="hidden" name="theme_id" value="{{ $id }}"> 

in your post method,

$theme = ThemeChanger::where(['user_id' => $id, 'theme_id' => $request->get('theme_id'));

$theme->update(['color_code' => $request->input('color_code')]);

if thats not exactly what you're after, please supply migration detail for ThemeChanger table and details of the relationship between your user and your theme

hassanz93's avatar

@automica Thank you, wouldn't have asked for a better explanation. The code is working now like I wanted

1 like

Please or to participate in this conversation.