B
B
bpGusar2017-08-03 20:57:21
Laravel
bpGusar, 2017-08-03 20:57:21

Why does the view not see the value of the variable?

Controller

<?php

namespace App\Http\Controllers;

use DB;
use App\Posts;
use App\CategoryNews;
use Illuminate\Http\Request;

class DashPosts extends Controller
{
//остальные функции

    public function create()
    {
        $category = DB::table('post_category')->pluck('cat_name', 'id_cat');
        // если прописать dd($category); то всё нормально показывает, всё загружается
        return view('admin.posts.createpost')->withPost($category);
    }

//остальные функции
}

In view I do this
<div class="form-group">
            {!! Form::label('id_cat', 'Категория') !!}
            {!! Form::select('id_cat', $category, null, ['class' => 'form-control']) !!}
        </div>

but it throws an error

(1/2) ErrorException
Undefined variable: category

and
(2/2) ErrorException
Undefined variable: category (View: view_path)

The controller itself was created to add news to the site, this function was created in order to unload category names from the database in select.
What's wrong, can you understand?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Melikhov, 2017-08-03
@bpGusar

You write
which is tantamount to

return view('admin.posts.createpost', [
  'post' => $category,
]);

that is, in the view, the $category variable will be named $post
Do this
return view('admin.posts.createpost', [
  'category' => $category,
]);

or so
or so
return view('admin.posts.createpost')->withCategory($category);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question