Home > Enterprise >  Passing json data to laravel view without casting in the model
Passing json data to laravel view without casting in the model

Time:04-23

I have this function that fetches data from a tabel paginates and passes data to the view

public function job_requests(){
        
        $orders = DB::table('orders')
                    ->select('id','order_data','order_status')
                    ->paginate(5);
        
        return view('autorepair/mechanics/job_requests',compact('orders'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
            
    }

The column order_data contains data of the following format

{
    "personal_data": {
        "email": "[email protected]",
        "telephone_number": "0999",
        "postal_address": "LON",
        "car_registration": "GB BHG"
    },
    "inperson_diagnostic": {
        "diagnostic_inspection": "67.30",
        "car_wont_start_inspection": "67.30",
        "plugin_diagnostic_inspection": "67.30"
    },
    "tyres": {
        "front_wheels": 1,
        "rear_wheels": 1,
        "wheel_width": 45,
        "wheel_profile": 1,
        "wheel_rim": 1,
        "speed_rating": "w",
        "final_price": 90
    },
    "servicing_and_mot": {
        "mot_with_collection_delivery": 75,
        "major_service": 304.52,
        "full_service": 203.45,
        "interim_service": "149.70",
        "vehicle_health_check": 50
    },
    "inspection_services": {
        "premium_prepurchase_inspection": 146.38,
        "standard_prepurchase_inspection": 104,
        "basic_prepurchase_inspection": 86.44
    },
    "repairs": {
        "ABS wheel speed sensor replacement": 964,
        "ABS pump replacement": 712,
        "Brake pedal switch replacement": 568,
        "Air conditioning regas (R1234yf Gas ONLY)": 469
    }
}

In my view i have this

@if(!$orders->isEmpty())
    <table >
        <tr>
            <th>No</th>
            <th>Order Status</th>
            <th>Order Data</th>
            <th width="280px">Action</th>
        </tr>
        @foreach ($orders as $order)
        <tr>
            <td>{{   $i }}</td>
            <td>{{ $order->order_status }}</td>
            <td>{{ $order->order_data}}</td>
            <td>
   
                    <a  href="{{ url('/view_product/' . $order->id) }}">Show</a>
    
                    <a  href="{{ url('/edit_product/' . $order->id ) }}">Accept</a>
                    

            </td>
        </tr>
        @endforeach
    </table>
  @else
    <p>No orders yet.</p>  
  @endif
    {!! $orders->links() !!}

I can display the josn data here $order->order_data but i would like to have it decoded first. How can i pass a decoded array to my view and be able to keep the pagination links?

CodePudding user response:

You could decode it in the view.

Depending on the format of order_data that is actually passed to the view (forgive me, I don't know off-hand how builder or eloquent retrieves jsonb type), but you can use dd() or gettype() in the view, to help identify what data type order_data is when delivered to the view):

  • If the content comes into the view as a string, try json_decode() on the order_data value. I think you should be able to iterate over the result of that, if that is your intent.
  • Related