简体   繁体   中英

How to print single field data from json array

I have run a query using laravel DB facade and it's return a json array object, but when i want to access single field value from json array it's show an error.

Here is my Query part.

$dsale=DB::table('directsales')
            ->join('clients','directsales.client_id','=','clients.id')
            ->join('products','directsales.product_id','=','products.id')
            ->select('clients.client_name','clients.addr','directsales.*','products.name')
            ->where('directsales.client_id','=',$client_id)
            ->whereBetween('directsales.issue_date',[$fromdate,$todate])
            ->distinct()
            ->get();

            return $dsale;

this is my result.

[{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":1,"transaction_code":"IN--00000001","client_id":1,"product_id":1,"product_code":"P0007","unitperctn":20,"unitprice":100,"ctn":4,"pcs":5,"total":4500,"deliverd_by":"nazim","issue_date":"2017-12-01","created_at":"2017-11-28 13:55:29","updated_at":"2017-11-28 13:55:33","name":"Dano Instant 2.5 Kg Packet 6"},{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":2,"transaction_code":"IN--00000002","client_id":1,"product_id":1,"product_code":"P0001","unitperctn":12,"unitprice":100,"ctn":1,"pcs":1,"total":2,"deliverd_by":"majed","issue_date":"2017-12-01","created_at":null,"updated_at":null,"name":"Dano Instant 2.5 Kg Packet 6"},{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":3,"transaction_code":"IN--00000002","client_id":1,"product_id":4,"product_code":"P0004","unitperctn":12,"unitprice":90,"ctn":1,"pcs":3,"total":6,"deliverd_by":"majed","issue_date":"2017-12-01","created_at":null,"updated_at":null,"name":"All time bread"},{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":5,"transaction_code":"IN--00000004","client_id":1,"product_id":1,"product_code":"P0001","unitperctn":12,"unitprice":100,"ctn":1,"pcs":0,"total":1,"deliverd_by":"majed","issue_date":"2017-12-12","created_at":null,"updated_at":null,"name":"Dano Instant 2.5 Kg Packet 6"},{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":6,"transaction_code":"IN--00000005","client_id":1,"product_id":4,"product_code":"P0004","unitperctn":12,"unitprice":90,"ctn":3,"pcs":3,"total":3510,"deliverd_by":"add","issue_date":"2017-12-03","created_at":null,"updated_at":null,"name":"All time bread"},{"client_name":"client one","addr":"jdkdhjfkhdkshfds","id":10,"transaction_code":"IN--00000007","client_id":1,"product_id":4,"product_code":"P0004","unitperctn":12,"unitprice":90,"ctn":1,"pcs":6,"total":1620,"deliverd_by":"majed","issue_date":"2017-11-01","created_at":null,"updated_at":null,"name":"All time bread"}]

When i print client_name $dsale->['client_name'][0] this way it's show error.

in array.. you need to get the index first, then get the object..

ex: if the item is an object, do like this..

$dsale[0]->client_name;

or if the item is an array type, do like this..

$dsale[0]['client_name']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM