Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing json data to laravel view without casting in the model

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": "info@info.com",
        "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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@if(!$orders->isEmpty())
    <table class="table table-bordered">
        <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 class="btn btn-info" href="{{ url('/view_product/' . $order->id) }}">Show</a>
    
                    <a class="btn btn-primary" 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?

>Solution :

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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading