レンダリングするファイルが Views 以下にある場合はこちらでOKです。
$html = view('welcome', compact('user'))->render();
dd( $html );
Views 以外のディレクトリにあるテンプレートファイルをレンダリングして変数に代入するには
/**
* views 以外にあるbladeテンプレート( *.blade.php )を描画して変数とし受け取る
*
* @param string $template_full_path テンプレートhtmlファイル名(フルパス)
* @param string $compact compact() で受け取るテンプレートパラメーター
*
* @return string htmlテンプレート描画内容
*/
private function getViewRender( string $template_full_path , array $compact )
{
$template_path = pathinfo($template_full_path, PATHINFO_DIRNAME);
$templatefile_name = pathinfo($template_full_path, PATHINFO_FILENAME);
$app = app();
$paths = [$template_path];
// もとの設定を取得
$originalFinder = \Illuminate\Support\Facades\View::getFinder();
$finder = new \Illuminate\View\FileViewFinder($app['files'], $paths);
\Illuminate\Support\Facades\View::setFinder($finder);
$html = view($templatefile_name, $compact )->render();
dd($html);
// もとの設定に戻す
\Illuminate\Support\Facades\View::setFinder($originalFinder);
return $html;
}
使い方
$html = $this->getViewRender( $template_html, compact('param') );