Laravel Blade 查看调用静态 属性 和命名空间
Laravel Blade view calling static property and namespace
我一直在使用 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/>";
}
}
}
我试图以这种方式在视图中传递 $campaigns,尽管我知道它不会工作,因为缺少名称空间以及 blade {{ }} 和 $campaign-> 之间的冲突{CampaignFields::NAME}/$campaign->{CampaignFields::ID}。
@foreach ($campaigns as $campaign) {
<a href='ad_detail.php?id="{{$campaign->{CampaignFields::ID}}}"'>" {{$campaign->{CampaignFields::NAME}}}"</a>
@endforeach
我的问题是如何使用正确的 blade 语法在视图中传递 $campaigns 数组。
您可以 return 控件中的字段作为字符串,然后像这样在 blade 中引用它:
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
我一直在使用 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/>";
}
}
}
我试图以这种方式在视图中传递 $campaigns,尽管我知道它不会工作,因为缺少名称空间以及 blade {{ }} 和 $campaign-> 之间的冲突{CampaignFields::NAME}/$campaign->{CampaignFields::ID}。
@foreach ($campaigns as $campaign) {
<a href='ad_detail.php?id="{{$campaign->{CampaignFields::ID}}}"'>" {{$campaign->{CampaignFields::NAME}}}"</a>
@endforeach
我的问题是如何使用正确的 blade 语法在视图中传递 $campaigns 数组。
您可以 return 控件中的字段作为字符串,然后像这样在 blade 中引用它:
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