?创建修改模版 报错。

"Undefined variable: categories (View: /home/vagrant/Code/larabbs/resources/views/topics/create_and_edit.blade.php)"![](![Uploading file...]()http://)

走靠谱的路,做靠谱人。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

@ayauper 错误很明显了,高亮的23行,$request不存在
需要将$request依赖注入到控制器中

public function index(Topic $topic,Request $request)
5年前 评论
讨论数量: 7

看看控制器对应方法返回页面的时候有没有绑定categories

5年前 评论
  public function show(Topic $topic)
    {
      $categories = Category::all();
      return view('topics.create_and_edit', compact('topic', 'categories'));
    }

    public function create(Topic $topic)
    {
        return view('topics.create_and_edit', compact('topic','categories'));
    }

代码如上,是这里吗?求大神指点。

5年前 评论

感谢指点,找到问题了。 忘记加
$categories = Category::all();

5年前 评论

@Fykex 点击 话题后报错!求解。。

/home/vagrant/Code/larabbs/app/Http/Controllers/TopicsController.php

use App\Models\Topic;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserRequest;
use App\Http\Requests\TopicRequest;
use App\Models\Category;
use Auth;

class TopicsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth', ['except' => ['index', 'show']]);
    }

    public function index(Topic $topic)
    {
   $categories = Category::all();
        $topics = $topic->withOrder($request->order)->paginate(20);
        return view('topics.index', compact('topics'));
    }

    public function show(Topic $topic)
    {
      $categories = Category::all();
      return view('topics.create_and_edit', compact('topic', 'categories'));
    }

    public function create(Topic $topic)
    {
    $categories = Category::all();
        return view('topics.create_and_edit', compact('topic','categories'));
    }

  public function store(TopicRequest $request, Topic $topic)
   {
       $topic->fill($request->all());
       $topic->user_id = Auth::id();
       $topic->save();
Arguments
"Undefined variable: request"
5年前 评论

把没用的楼层删除掉吧,另外可以编辑已经发布的楼层,不用一直发新的
debug要贴出具体的报错代码,同时贴出相关的路由、控制器、模型等代码

5年前 评论

file
控制器如下

<?php

namespace App\Http\Controllers;

use App\Models\Topic;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserRequest;
use App\Http\Requests\TopicRequest;
use App\Models\Category;
use Auth;

class TopicsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth', ['except' => ['index', 'show']]);
    }

    public function index(Topic $topic)
    {
     $categories = Category::all();
        $topics = $topic->withOrder($request->order)->paginate(20);
        return view('topics.index', compact('topics'));
    }

    public function show(Topic $topic)
    {
      $categories = Category::all();
      return view('topics.create_and_edit', compact('topic', 'categories'));
    }
5年前 评论

@ayauper 错误很明显了,高亮的23行,$request不存在
需要将$request依赖注入到控制器中

public function index(Topic $topic,Request $request)
5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!