最后 Laravel 控制器函数丢失数据

Last Laravel controller function loses data

我正在从一个参数发送到另一个参数,而不是通过路由将它们传递给另一个安全方式,但最后一个函数的问题是它丢失了参数,甚至 dd() 也没有工作,它 returns 视图只有一个未定义的变量 $data.

控制器

// Pass id to test.auth
public function show_auth($id)
{
    return view('test.auth', compact('id'));
}

// Here it gets id and dd($data) works
public function post_auth(Request $request)
{
    $id = $request->id;

    $data = DB::table('kuponai')->where('id', $id)->get();
    //dd($data);
    
    return view('test.view', compact('data'));
}

// Function from before dump doesn't work and returns view
public function view($data)
{
    dump($data);

    return view('test.view');
}

改了也不管用...

public function check(Request $request)
    {
      $id = $request->id;

      //
      
      return view('test.auth',compact('id'));
      
    }
    public function show_auth($id)
    {
        return view('test.auth',compact('id'));
        
    }
    public function post_auth(Request $request)
    {
      $id = $request->id;
      $pin = $request->pin;
      $mail = $request->elpastas;

      $data = DB::table('kuponai')
      ->where('id', $id)
      ->get();
      //dd($data);

      return route('view.show',compact('data'));
    }
    public function view($data)
    {
      $d = $this->post_auth($data);
      //dd($data);
        return view('test.view');
    }

这可能是因为您传递的数据变量直接发送到 test.view 视图。所以永远不要传递你做的视图函数($data)dump($data).

($ 数据不是通过路由发送的,而是直接发送到视图中的)!!

我似乎知道解决方案,但请告诉我您的 routes.php

片段

路线:

Route::group(['domain' => 'coupons.domain.com'], function()
{

        Route::get('/', 'App\Http\Controllers\IsoreController@index')->name('view.klientas');
        Route::post('/check', 'App\Http\Controllers\IsoreController@check')->name('check')->middleware('checkid');
        // view authenticated coupon
        Route::get('/view', 'App\Http\Controllers\IsoreController@view')->name('view.show')->middleware('checkpin');
        // show coupon authenticate form
        Route::get('/auth','App\Http\Controllers\IsoreController@show_auth')->name('show.auth')->middleware('checkid');
        // handle user input, authenticate coupon
        Route::post('/authenticate','App\Http\Controllers\IsoreController@post_auth')->name('post.auth')->middleware('checkpin');
});

尝试替换return view('test.auth', compact('data'));

有: return redirect('/view')->with(compact('data'));

我的中间件:

校验码:

class KuponoId {

  public function handle($request, Closure $next)
  {
        $exists =Kuponas::where('id', $request->id)->exists();
        if (!$exists) {
            return \Redirect::to(url('/'))->with('pin','Duomenys neteisingi, patikrinkite ');
        }
        return $next($request);
  }
}

检查针:

class CheckPin {

  public function handle($request, Closure $next)
  {
        $exists = Kuponas::where('patvirtinimo_kodas', $request->pin)->where('elpastas', $request->elpastas)->where('id', $request->id)->exists();
        //dd($exists);
        if (!$exists) {
          //return \Redirect::to(url('/'))->with('pin','Duomenys neteisingi, patikrinkite ');
          return \Redirect::to(url('/'))->with('pin','Duomenys neteisingi! ');
        }
        return $next($request);
  }
}

我只需要在用户正确验证时显示一次信息视图

好的,这个怎么样?

return redirect()->action( 'TheController@view', ['id' => 1] );