当我发布多个值来查看时,laravel未定义的变量

时间:2017-12-07 10:32:49

标签: laravel laravel-5

在控制器中,当我尝试发送多个值以查看此代码时:

$content = Contents::find($contents);
$content_categories['categories'] = ContentCategories::all()->pluck('title', 'id');
$selected_categories['selected_categories'] = $content->categories()->pluck('title', 'id');

return view('layouts.backend.pages.manage_contents.edit', compact($selected_categories, $content_categories, ['content' => $content]));

我在视图中收到错误:

$categories
$selected_categories
$content

dd(['content' => $content], $content_categories, $selected_categories);的结果是:

array:1 [▼
  "content" => Contents {#220 ▶}
]

array:1 [▼
  "categories" => Collection {#214 ▶}
]

array:1 [▼
  "selected_categories" => Collection {#227 ▶}
]

我的代码问题是在视图上解析它们还是以正确的方式发送它们

我的观点:

1) {{ Form::select('categories[]', $categories, $selected_categories, array('class' => 'multiselect-success','multiple'=>'multiple')) }}

2) action="{{ route('manage_contents.update' , ['id' => $content->id ]) }}"

错误:

"Undefined variable: categories (View: /Applications/XAMPP/xamppfiles/htdocs/alachiqServer/resources/views/layouts/backend/pages/manage_contents/edit.blade.php)"

"Undefined variable: selected_categories (View: /Applications/XAMPP/xamppfiles/htdocs/alachiqServer/resources/views/layouts/backend/pages/manage_contents/edit.blade.php)"

"Undefined variable: content (View: /Applications/XAMPP/xamppfiles/htdocs/alachiqServer/resources/views/layouts/backend/pages/manage_contents/edit.blade.php)"

5 个答案:

答案 0 :(得分:1)

你可以试试这个:

$content_categories = ContentCategories::all()->pluck('title', 'id');
$selected_categories = $content->categories()->pluck('title', 'id');

return view('layouts.backend.pages.manage_contents.edit', compact('selected_categories', 'content_categories', 'content'));

希望这能帮到你!

答案 1 :(得分:1)

您需要更改类似的代码以传递值。

return view('layouts.backend.pages.manage_contents.edit', compact('selected_categories', 'content_categories','content'));

第二点你得到未定义的$ categories,因为你没有将类别传递给紧凑的函数。你需要将类别变量传递给紧凑函数。

我认为你需要改变这样的选择标签代码:

{{ Form::select('categories[]', $selected_categories['categories'], NULL , array('class' => 'multiselect-success','multiple'=>'multiple')) }}
希望可以帮助你。祝你好运!

答案 2 :(得分:1)

试试这种方式

$content = Contents::find($contents);
$categories = ContentCategories::all()->pluck('title', 'id');
$selected_categories = $content->categories()->pluck('title', 'id');

return view('layouts.backend.pages.manage_contents.edit', compact($selected_categories, $content_categories, $content));

答案 3 :(得分:0)

将变量传递到compact,而不是$符号

$categories = ContentCategories::all()->pluck('title', 'id');
$selected_categories = $content->categories()->pluck('title', 'id');

return view('layouts.backend.pages.manage_contents.edit', 
             compact('selected_categories', 'content_categories',
                     'content'));

答案 4 :(得分:0)

尝试这种方式:

return view('layouts.backend.pages.manage_contents.edit', compact('selected_categories', 'content_categories', 'content'));