1

In controller when i try to send multiple value to view such as this codes:

$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]));

i get error for this variables into view:

$categories
$selected_categories
$content

result of dd(['content' => $content], $content_categories, $selected_categories); is:

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

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

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

whats problem of my code to parse them on view or send them with correct way

my view:

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

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

Errors:

"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)"
3
  • Can you post your error? Commented Dec 7, 2017 at 10:34
  • 1
    compact() takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; compact() handles it recursively. php.net/manual/en/function.compact.php Commented Dec 7, 2017 at 10:37
  • Take a look at my answer here ! Commented Dec 7, 2017 at 10:45

5 Answers 5

1

You can try this:

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

Hope this helps you!

Sign up to request clarification or add additional context in comments.

1 Comment

Have you checked this?
1

You need to change your code something like this to pass value.

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

Second point you getting undefined $categories because you are not passing categories into compact function. you need to pass categories variable into compact function.

I think you need to change select tag code like this:

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

Hope can help you. Good Luck!

Comments

1

Try this way

$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));

Comments

0

Pass the variables to the compact without the $ sign

$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 Comment

i get this error: Undefined variable: categories
0

Try this way:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.