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
?我想直接访问这个上下文,而不必毫不费力地分配变量。
答案 0 :(得分:3)
您可以手动创建数组,而不是使用compact():
public function index()
{
return view('media', [
'images' => $this->images,
]);
}