jackdaw
jackdaw

Reputation: 2314

Laravel, how to access properties directly

    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'));
        }

    }

Quite a simple one probably, is there a way to directly reference $this->images from within the compact method here? I'd like to access the this context directly without needlessly assigning a variable.

Upvotes: 1

Views: 129

Answers (1)

baikho
baikho

Reputation: 5358

Instead of using compact(), you can manually create the array:

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

Upvotes: 3

Related Questions