Laravel,如何直接访问属性

时间:2016-12-13 19:41:22

标签: php laravel

    namespace App\Http\Controllers;

    use App\Image;
    use Illuminate\Http\Request;


    /**
     * @property \Illuminate\Database\Eloquent\Model|null|static about
     * @property \Illuminate\Database\Eloquent\Collection|static[] images
     */
    class MediaController extends Controller
    {


        /**
         * MediaController constructor.
         */
        public function __construct()
        {
            $this->images = Image::all();
        }

        /**
         * Generate the Media page.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {

            $images = $this->images; //Is there a way to bypass this
            return view('media', compact('images'));
        }

    }

可能很简单,有没有办法在紧凑方法中直接引用$this->images?我想直接访问这个上下文,而不必毫不费力地分配变量。

1 个答案:

答案 0 :(得分:3)

您可以手动创建数组,而不是使用compact()

public function index()
{
    return view('media', [
        'images' => $this->images,
    ]);
}