Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rarepyre's avatar

(DONE) How to return Array from Model laravel?

so i have this syntax in my blalde:

            @foreach($fr->delivery->deliveryTrack as $fp)
                <li class="text-left">
                    <b>{{ $fp->date }}</b>
                    <br>
                    {{ $fp->desc }}
                </li>
            @endforeach

and this is the output when i dd($track) in my deliveryTrack function which return an array like this:

array:3 [
  0 => array:2 [
    "date" => "5 Sep 2020 16:27"
    "desc" => "Terkirim [CIBINONG - DC_PAKANSARI]"
  ]
  1 => array:2 [
    "date" => "5 Sep 2020 15:02"
    "desc" => "Sedang Diantar [CIBINONG - ]"
  ]
  2 => array:2 [
    "date" => "5 Sep 2020 14:20"
    "desc" => "Telah Tiba [CIBINONG - DC_PAKANSARI]"
  ]
]

when i try to return $track, it shows an error:

App\Models\OrderDelivery::deliveryTrack must return a relationship instance.

here my deliveryTrack syntax:

public function deliveryTrack(){
        $receipt_number = [];
        $seller = [];
        $buyer = [];
        $delivery_date = [];
        $track = [];
        $status = [];
        $success = [];
        $data = [];
        $error = [];
        
        $order = Order::where('id',$this->order_id)->first();
        
        $orderDelivery = OrderDelivery::where('order_id',$order->id)->first();
        
        if(!empty($orderDelivery->waybill)){
            $deliveryService = DeliveryService::where('id',$orderDelivery->delivery_service_id)->first();
            $restaurant = Restaurant::where('id',$order->restaurant_id)->first();
            $user = User::where('id',$order->user_id)->first();

            $curl = curl_init();
        
            curl_setopt_array($curl, array(
            CURLOPT_URL => "https://pro.rajaongkir.com/api/waybill",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "waybill=".$orderDelivery->waybill."&courier=".$deliveryService->key,
            CURLOPT_HTTPHEADER => array(
                "content-type: application/x-www-form-urlencoded",
                "key: d5775da3b766b41dc67281515c116e97"
            ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);

            curl_close($curl);

            if ($err) {
            echo "cURL Error #:" . $err;
            } else {
                $array = json_decode($response, true);
                $object = $array['rajaongkir']['result'];
                $track = [];

                if(!empty($object)){

                    foreach ($object['manifest'] as $key => $field) {
                        array_push($track, [
                            "date" => date('j M Y', strtotime($field['manifest_date'])) . ' ' . Carbon::parse($field['manifest_time'])->format('H:i'),
                            "desc" => $field['manifest_description'] . ' [' . $field['city_name'] . ']'
                        ]);
                    };

                    // how to pass array of ($track) to my view?
                    
                    return $track;
                    dd(1);
                }
            }
        }
    }

thank you for your attention..

0 likes
16 replies
Rarepyre's avatar

please if anyone can help, i still need it

automica's avatar

@rarepyre what is the method which is passing data to this blade?

   @foreach($fr->delivery->deliveryTrack as $fp)
                <li class="text-left">
                    <b>{{ $fp->date }}</b>
                    <br>
                    {{ $fp->desc }}
                </li>
            @endforeach
1 like
Rarepyre's avatar

@automica what do you mean of "method which is passing data to this blade"? i'm not understand, i think i just doing @foreach which $fr = table orders (model= Order) and delivery = table order_deliveries (model= OrderDelivery), which i call deliveryTrack function in model OrderDelivery

i think i've done with the function (when i try dd($track) its return laravel dump which stopped the @foreach from the view

the value of $track is an array like (look at my thread)

nah i need to pass variable $track to my view, so i can do @foreach for array value of $track

automica's avatar

@rarepyre you've got a controller method passing data to the blade that has that foreach in it. Can you share that?

1 like
automica's avatar

@rarepyre it its your deliveryTrack method thats returning the $tracks, then replace the bottom of it with:

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            $array = json_decode($response, true);
            $object = $array['rajaongkir']['result'];
            $tracks = []; // change this to tracks as you have an array

            if(!empty($object)){

                foreach ($object['manifest'] as $key => $field) {
                    array_push($tracks, [ // also change array name here
                        "date" => date('j M Y', strtotime($field['manifest_date'])) . ' ' . Carbon::parse($field['manifest_time'])->format('H:i'),
                        "desc" => $field['manifest_description'] . ' [' . $field['city_name'] . ']'
                    ]);
                };
            }
            return view('tracks.view',compact('tracks')); // return to view in location resources/views/tracks/view.blade.php
        }

I have renamed $track to track as its a multiple.

Then in your blade:

   @foreach($tracks as $track)
                <li class="text-left">
                    <b>{{ $track->date }}</b>
                    <br>
                    {{ $track->desc }}
                </li>
            @endforeach
1 like
Rarepyre's avatar

view:

@foreach($resellerOrderStatus as $key => $fr)
    @foreach($fr->delivery->deliveryTrack as $fp)
                <li class="text-left">
                    <b>{{ $fp->date }}</b>
                    <br>
                    {{ $fp->desc }}
                </li>
    @endforeach
@endforeach

controller:

            $resellerOrderStatus = Order::where('type','delivery')->where('order_status_id','delivered')->paginate(10);
            
            return view('nk.orders.index', compact('resellerOrderStatus'));
Rarepyre's avatar

the deliveryTrack is not controller, but its a model

Rarepyre's avatar

btw is it possible to return array/string like (return ['a','b','c'] from Model without $this relationship / ORM ?

automica's avatar

@rarepyre looking at your models though:

should your Order haveOne Delivery?

in that case, you wouldn't need a join table, as you can store delivery_id on order table.

1 like
Rarepyre's avatar

yes order (one to one) to order_deliveries

ya! i think i can make it more simply, but is it possible to return array/string like (return ['a','b','c']) from Model without doing ORM ?

automica's avatar

@rarepyre whats wrong with doing ORM? You make it hard for yourself if you don't use it.

1 like
automica's avatar
automica
Best Answer
Level 54

@rarepyre your current issue is that your have set deliveries to be belongsToMany which means you are passing back an array

so in your deliveryTrack method you want to return $tracks;

and in your blade you need three foreach loops:

@foreach($resellerOrderStatus as $key => $orderStatus)
    @foreach($orderStatus->delivery as $delivery)
        @foreach($delivery->deliveryTrack as $deliveryTrack)
            <li class="text-left">
                <b>{{ $deliveryTrack->date }}</b>
                <br>
                {{ $deliveryTrack->desc }}
            </li>
        @endforeach
    @endforeach;
@endforeach
1 like
Rarepyre's avatar

because in my case, i dont need to join to other table, i think i'm doing something wrong xD thanks for your response! i really appreciate it!

automica's avatar

@rarepyre btw I think as your deliveryTrack is more of the equivalent of a field rather than a model then you could call the method

getDeliveryTrackAttribute()

and then you can call it as a field

$tracks = $delivery->deliveryTrack;

see https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

I would recommend looking at revising your model relationships though as if you only need hasOne instead of hasMany and getting the first one it will make your table structure much easier.

Please or to participate in this conversation.