vendredi 27 avril 2018

How to return and display json data saved to database laravel

In laravel I am saving input data to json to store it in database, but having trouble displaying the data out again.

I am saving and updating the data as shown in the controller:

public function store(Request $request)
{   
    $this->validate(request(), [
        'start' => 'required',
        'end' => 'required'
    ]);

    if (Auth::check()) {

        Myroutes::create([ //posting to myroutes table
            'user_id' => Auth::user()->id,
            'start' => $request->start,
            'end' => $request->end,
            'waypoints' => json_encode($request->waypoints)
        ]);
        return redirect('/my-saved-routes');
    } else {
        return redirect('/login');
    }
}

 public function update(Request $request, $id)
{    
     Myroutes::where('myroute_id', $id)
    ->update(['start' => $request->input('start'),
             'end'=>$request->input('end'),
             'waypoints'=>$request->input('waypoints')]
            );
     return redirect('/my-saved-routes');
}

Data is being input into individual input fields in showing.blade:

<div id="dynamicInput" class="form-group">
                    <label>Additional Destinations</label>
                    <input type="text" name="waypoints[]" class="form-control" autocomplete="on">
                </div>

                <input type="button" class="btn btn-secondary" value="+" onClick="addInput('dynamicInput');" style="padding:0 10px;">               

And is being output in edit.blade:

<div id="dynamicInput" class="form-group">
                                    <label>Additional Destinations</label>
                                    <input type="text" name="waypoints" class="form-control" autocomplete="on" value="">
                                </div>

shows the entire array and " shows only the first value which is [

Database structure for reference: database



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire