如何在laravel控制器中全局定义多维数组?

时间:2016-12-25 12:11:52

标签: laravel laravel-5.3

我想在全局的laravel控制器中定义多维数组。 我这样定义它

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Communication_link;
use App\Contact;
use DateTime;
use App\Resource_status;
use App\Inquiry;
use App\Contact_communication;
use App\Pincode;
use App\City;
use App\User;

class createInquiryController extends Controller
{
    public $response;
    $map = array(
                  array("contact","id"),
                  array("communication_link", "id"),
                  array("contact_communication","id")
    );



    public function contact_select(Request $request){
            return $map;
    }
}

但这会引发错误“未定义的地图”。

1 个答案:

答案 0 :(得分:3)

定义它并在构造函数中分配数据:

protected $map;

public function __construct()
{
    $this->map = array(
        array("contact","id"),
        array("communication_link", "id"),
        array("contact_communication","id")
    );
}

然后您可以从此控制器中的任何方法访问此变量:

public function index()
{
    $data = $this->map;
}