Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel数据[';标题';]_Php_Laravel - Fatal编程技术网

Php Laravel数据[';标题';]

Php Laravel数据[';标题';],php,laravel,Php,Laravel,因此,我试图获取一个标题并在我的控制器上定义它,但当我请求数据['title']时,它得到“1”,我设置“Test” 我家的控制器 Route::get('/', 'HomeController@index'); 我的行设置$title $data['title'] = "Test"; return view('home.home', $data); My head.blade.php <title>{{ $title or Helpers::meta((!is

因此,我试图获取一个标题并在我的控制器上定义它,但当我请求数据['title']时,它得到“1”,我设置“Test”

我家的控制器

 Route::get('/', 'HomeController@index');
我的行设置$title

  $data['title']   = "Test";
  return view('home.home', $data);
My head.blade.php

  <title>{{ $title or Helpers::meta((!isset($exception)) ? Route::current()->uri() : '', 'title') }} {{ $additional_title or '' }}</title>
{$title或Helpers::meta(!isset($exception))?Route::current()->uri():'''title')}{{{$additional_title或''}}
laravel版本是5.8。
我是php世界和laravel的新手,谢谢你的帮助

尝试以一种更简单的方式将其分解,以便视图通过。使用
compact
并确保在compact中使用字符串,而不是变量。像这样:

$title  = "Test";
return view('home.home', compact('title');

尝试一下,应该可以正常工作。

将数据传递给视图

您应该使用键/值对在数组中传递数据。 在视图中,您可以使用相应的键访问每个值,例如
。除了将完整的数据数组传递给view helper函数之外,您还可以使用
with
方法将单个数据段添加到视图中:

 return view('home.home')->with('title', 'Test');

我已经阅读了每个答案,但每个答案都缺少将变量传递给视图的魔法类型

在函数中声明大量变量时,这个答案似乎有点帮助

拉威尔5.7*

比如说

public function index()
{
    $activePost = Post::where('status','=','active')->get()->count();

    $inActivePost = Post::where('status','=','inactive')->get()->count();

    $yesterdayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(-1))->get()->count();

    $todayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(0))->get()->count();

    return view('dashboard.index')->with('activePost',$activePost)->with('inActivePost',$inActivePost )->with('yesterdayPostActive',$yesterdayPostActive )->with('todayPostActive',$todayPostActive );
}
当您看到返回的最后一行时,它看起来不太好

当你的项目越来越大的时候,它是不好的

因此:

正如您看到的,所有变量都声明为
$viewShareVars
数组,并在视图中访问

但是我的函数变得更大了,所以我决定让这行变得非常简单:

public function index()
    {
        $activePost = Post::where('status','=','active')->get()->count();

        $inActivePost = Post::where('status','=','inactive')->get()->count();

        $yesterdayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(-1))->get()->count();

        $todayPostActive = Post::whereDate('created_at', Carbon::now()->addDay(0))->get()->count();

        $viewShareVars = array_keys(get_defined_vars());
    
        return view('dashboard.index',compact($viewShareVars));
    }
本机PHP函数
get_defined_vars()
从函数中获取所有已定义的变量,
array_key
将获取变量名

因此,在您的视图中,您可以作为
{{{$todayPostActive}}
访问函数中所有声明的变量

所以在你的情况下:

$data['title']   = "Test";

  return view('home.home', compact(array_keys(get_defined_vars())));

在您的视图中,
{$data['title']}

如何将数据返回到视图?它应该是这样的
返回视图('home',$data)抱歉,我忘了在问题中添加,正如您所说->返回视图('home.home',$data);或者,对于更漂亮的语法:
returnview('home.home')->with('title',$title)
Yep也可以,但是对于典型的多变量,我更喜欢compact
$data['title']   = "Test";

  return view('home.home', compact(array_keys(get_defined_vars())));