Laravel Blade视图调用静态属性和命名空间

时间:2018-02-08 17:10:54

标签: php facebook laravel blade laravel-blade

我一直在使用Facebook Ad SDK,它在控制器中运行良好,因为它具有完整的PHP语法支持。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use FacebookAds\Object\AdAccount;
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Fields\CampaignFields;

class AdController extends Controller
{ 
    private $access_token = '';
    private $account_id = '';
    private $app_secret = '';
    private $app_id = '';

    public function __construct(){
        Api::init($this->app_id, $this->app_secret, $this->access_token);
     }


    public  function index()
    {

        $account = new AdAccount($this->account_id);

        $params = array(
            'time_interval' => array(
                'day_start' => array('year' =>'2013', 'month'=> '5', 'day'=>'1'),
                'day_stop' => array( 'year'=>'2017', 'month'=>'12', 'day'=>'31')
            ),
            'limit' => 500,
        );
        $fields = array(
            CampaignFields::NAME,
            CampaignFields::OBJECTIVE,
            CampaignFields::STATUS,
        );

        $campaigns = $account->getCampaigns($fields,$params);
/*
        return view('admin.campaigns.index', [
            'campaigns' => $campaigns
        ]);

*/




        foreach ($campaigns as $campaign) {
            //echo "campaign ID". . "<br/>";
            echo "<a href='ad_detail.php?id=".$campaign->{CampaignFields::ID}."'>".$campaign->{CampaignFields::NAME}."</a><br/>";



        }
    }
}

我试图以这种方式在视图中传递$ campaign,但我知道它不会起作用,因为没有命名空间和刀片{{}}和$ campaign-&gt; {CampaignFields :: NAME之间的冲突} / $活动层级&GT; {CampaignFields :: ID}

@foreach ($campaigns as $campaign) {

            <a href='ad_detail.php?id="{{$campaign->{CampaignFields::ID}}}"'>" {{$campaign->{CampaignFields::NAME}}}"</a>

@endforeach

我的问题是如何使用正确的刀片语法在视图中传递$ campaigns数组。

1 个答案:

答案 0 :(得分:0)

您可以将控件内的字段作为字符串返回,然后在其中引用它,如下所示:

Controller.php这样

 return view('index', ['campaigns' => $campaigns, 'properties' => $fields]);

index.blade.php

@foreach ($campaigns as $campaign) {

    <a href='ad_detail.php?id="{{ $campaign->{$properties[0]} }}"'>
        "{{ $campaign->{properties[1]} }}"
    </a>

@endforeach