Anastasie Laurent
Anastasie Laurent

Reputation: 1179

laravel using data in view that has been sent from controller

I always send data from view like this ->with(array('message'=>'there is an error ')), and it works.

I want to allow the customer to edit some information, so when he/she clicks on the the edit like, this function in a controller is being executed:

public function edit($id)
    {
        $waitingTimes = WaitingTimes::find($id);
        return View::make('waitingtimes.edit')->with(array(
            'verticalMenu' => 'none',
            'verticalMenuTab' => 'none',
            'data' => $waitingTimes
        ));
    }

So later in the view, I should be able to say this:

$data->startTime, $data->endTime, $data->id, $data->restaurant_id

but every time I do that, I got $data->startTime printed on the browser, but I should have got the value of the startTime attribute.

This is my view:

<div class="oneInfo">
            {{ Form::text('startTime', '', array('class' => 'time ui-timepicker-input', 'id' => 'startTime', 'autocomplete' => 'off'))}}
            <span class="errorMessage">
                <?php
                echo $errors->first('startTime');
                ?>
            </span>
        </div>

The view has an input text and I want that input text to be filled with the data that has been sent from the controller.

how could I do that please?

Upvotes: 0

Views: 415

Answers (1)

Jeemusu
Jeemusu

Reputation: 10533

The Form::text() method's second parameter allows you to pass it the value to be assigned to the input element. Your form input declarations are currently setting the value of the inputs to an empty string.

The best way to handle this would be to replace your empty string with $value=null.

{{ Form::text('startTime', $value=null, array('class' => 'time ui-timepicker-input', 'id' => 'startTime', 'autocomplete' => 'off'))}}

This will automatically replace the value with your models data or the data input by the user (should validation fail and you redirect back to the form).

From the looks of things, you could also make things a bit easier for yourself by using form model binding to bind the WaitingTimes model to your form.

Upvotes: 1

Related Questions