未定义的变量:刀片层级5.6中的类别

I am trying to show my categories in site map web page this is my CategoryController.php

namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{

public function site()
   {
    $categories = Category::all();
    #   return view('template.sitemap', compact('categories'));
    #   return view('template.sitemap',['categories' => $categories]);
     return view('template.sitemap')->with(compact('categories'));
    #   return view('template.sitemap')->withData($categories);
    #   return view('template.sitemap')->with('categories',$categories);
    #   return view('template.sitemap', compact('categories'));
  }
}

The # that i use show i use different method of passing variables to view i use every method alone but none of them didn't work.

and this is the part my sitemap blade file in template folder that i use categories variable template.sitempa.blade.php

        <div id="content" class="col-sm-12">
                <h1 class="title"> site map</h1>
                <div class="row">
                    <div class="col-sm-3 hidden-xs hidden-sm sitemap-icon"><i class="fa fa-sitemap"></i></div>
                    <div class="col-md-5 col-sm-6">
                        <ul class="sitemap">
                                <ul>
                                    @foreach($categories as $category)
                                        <li><a href="category.html">{{$category['name']}}</a></li>
                                    @endforeach

                                </ul>
                        </ul>
                    </div>
                </div>
            </div>

i use every way of get variable and passing but none the didn't work do you have any suggestion?

your blade file should be in the directory template/sitemap.blade.php, not template.sitempa.blade.php

public function site()
{
    $categories = Category::all();
    return view('template.sitemap', compact('categories'));
}

and

@foreach($categories as $category)
    <li><a href="category.html">{{ $category->name }}</a></li>
@endforeach

Try this:

public function site()
{
    return view('template.sitemap')->withCategories(Category::all());
}

Then in your view, do:

dd($categories);

To know if you are receiving the data. If nothing is wrong, use it as desired:

@foreach($categories as $category)
    <li><a href="category.html">{{$category['name']}}</a></li>
@endforeach

Try this

public function site()
{
$this->data['categories'] = Category::all();
return view('template.sitemap',$this->data);
}

In view

@foreach($categories as $category)
<li><a href="category.html">{{$category->name}}</a></li>
@endforeach