首页 文章

在Laravel中查看如何json_decode URL

提问于
浏览
1

我将json_encode之前的URL数据发送到数据库 . 现在我正在查看页面中的那些数据 . 但我需要 json_decode 它才能得到网址链接 . 但在代码的某些部分我无法做到 .

我的url json_decode(链接)的foreach不工作并返回laravel错误 . 错误是: Undefined variable: links (View: /var/www/html/laravel/resources/views/welcome.blade.php)

<tbody>
    @foreach($estates as $estate)
        <tr>
            <td>{{$estate->company_name}}</td>

            <?php
                $links = json_decode($estate->link); 
                $hrefs = "";
                foreach($links as $link){
                    $hrefs .= '<a href="'.$link.'" } target="_blank">{{$estate->name}}</a>
'; } $hrefs .= ""; ?> <td>{{$estate->address}}</td> <td>{{$estate->price}}</td> <td>{{$estate->hows_old}}</td> <td>{{$estate->extend}}</td> <td>{{$estate->rooms}}</td> <td>{{$estate->balcon_m2}}</td> <td>{{$estate->old}}</td> <td>{{$estate->entery}}</td> </tr> @endforeach </tbody>

没有json_decode estate->link 是这样的:

`http://localhost/[%22https:////www.31sumai.com//pid//about//%22,%22https:////www.31sumai.com//%22,%22https:////www.31sumai.com//mfr//K1503//%22,%22https:////www.31sumai.com//mfr//K1503//%22,%22http:////maps.google.com//maps?q=34.69465,135.519178%22,%22https:\/\/www.31sumai.com\/mfr\/K1503\/juko.htm]`

这是控制器代码:

$estates = Estates::get(); 

$data['estates'] = $estates; 
return view('welcome', $data);

谢谢你的协助 .

1 回答

  • 0

    如果您真的想在视图中使用json编码数据,请在foreach中使用json_decode():

    @foreach(json_decode($estate->link, true) as $value)
        $hrefs .= '<a href="'.$value.'" } target="_blank">{{$estate->name}}</a>
    '; @endforeach

相关问题