【问题标题】:There May Be Problem In My Route In my Laravel Project我的 Laravel 项目中的路线可能有问题
【发布时间】:2019-02-13 02:49:43
【问题描述】:

我的页面上有一个未定义的变量错误。 我正在处理一个包喋喋不休,我正在添加一个字段以将 cmets 添加到 StackOverflow 之类的帖子中。 但是当我路由 ChatterReplyController@show 并传递 $chatterreplies 变量时,视图页面显示未定义的变量。

这是控制器的代码

    public function show($id)
    {
        $chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
        echo "<pre>"; print_r('$chatterreplies'); die();

  // or use the laravel helper 
  dd($chatterreplies);
        return view('chatter::discussion', compact('chatterreplies'));

    }

路由文件代码发布组

Route::group([
    'as'     => 'posts.',
    'prefix' => $route('post', 'posts'),
], function () use ($middleware, $authMiddleware) {

    // All posts view.
    Route::get('/', [
        'as'         => 'index',
        'uses'       => 'ChatterPostController@index',
        'middleware' => $middleware('post.index'),
    ]);

    // Create post view.
    Route::get('create', [
        'as'         => 'create',
        'uses'       => 'ChatterPostController@create',
        'middleware' => $authMiddleware('post.create'),
    ]);

    // Store post action.
    Route::post('/', [
        'as'         => 'store',
        'uses'       => 'ChatterPostController@store',
        'middleware' => $authMiddleware('post.store'),
    ]);
    //Adding Comments
    Route::post('/reply/{id}', [
        'as'         => 'store',
        'uses'       => 'ChatterReplyController@store',
        'middleware' => $authMiddleware('post.reply.store'),
    ]);
    //showing Comment
    Route::get('/reply/{id}', [
        'as'         => 'show',
        'uses'       => 'ChatterReplyController@show',
        'middleware' => $authMiddleware('post.reply.show'),
    ]);

这是我在视图页面上显示内容的方式

@foreach($chatterreplies as $chatterreply)
                        {{$chatterreply->reply}}
@endforeach

【问题讨论】:

  • 尝试为-&gt;with('chatterreplies',$chatterreplies );更改压缩包
  • 没有相同的错误再次出现@AlexanderVillalobos 请检查我的路线可能有问题,我应该分享错误屏幕
  • 你传递了两个参数,你的路由有一个
  • 在你的函数中删除这个Chatterreply $chatterreply
  • 我删除了那个@AlexanderVillalobos,但错误仍然存​​在

标签: laravel laravel-5 laravel-4 laravel-5.2 laravel-5.6


【解决方案1】:

试试这个

public function show($id)
 {
    $chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
    echo "<pre>"; print_r('$chatterreplies'); die();
    return view('chatter::discussion', compact('chatterreplies'));

 }

在你的route 中你传递了一个parameter

【讨论】:

  • 你验证了你是否通过了 id ??尝试打印dd($id);
  • 先生,如果您有时间,请您通过Teamviewer查看它,因为没有变化。
  • like this `return view('chatter::discussion', compact('chatterreplies'));`
  • @foreach($chatterreplies as $chatterreply) {{$chatterreply-&gt;reply}} @endforeach我第一次在路由中调用
  • 1077484234 通过:1410
猜你喜欢
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
相关资源
最近更新 更多