Laravel 5.6不同的功能返回相同的视图

时间:2018-04-05 08:14:50

标签: php laravel laravel-5

我的laravel中有不同的功能

 public function index()
{
    $YourData = DB::table('users')
        ->join('packages', 'users.id', '=', 'packages.user_id')
        ->select('users.*','packages.name as packageName',DB::raw('SUM(packages.entry_nr) as entries'))
        ->where('users.id', Auth::id())
        ->get();
        return view('home', compact('YourData'));

}

public function LeMiePrenotazioni(){

    $id =Auth::user()->id;
    $lesson = DB::select("
        SELECT *
        FROM lessons t
        where (t.date > now()) 
       and NOT EXISTS 
        (SELECT 1 FROM bookings b
        WHERE b.lessons_id = t.id AND b.user_id = ".$id.");
        ");
        return view('home', compact('lesson'));

}

我怎样才能在home.blade.php中使用这两个功能 我的路线是

Route::get('/home', 'HomeController@index')->name('home');

4 个答案:

答案 0 :(得分:2)

你可以这样做

public function index()
{
    $YourData = DB::table('users')
        ->join('packages', 'users.id', '=', 'packages.user_id')
        ->select('users.*','packages.name as packageName',DB::raw('SUM(packages.entry_nr) as entries'))
        ->where('users.id', Auth::id())
        ->get();
   $LeMiePrenotazioni = $this->LeMiePrenotazioni();

   return view('home', compact('YourData','LeMiePrenotazioni'));

}

public function LeMiePrenotazioni(){

    $id =Auth::user()->id;
    $lesson = DB::select("
        SELECT *
        FROM lessons t
        where (t.date > now()) 
       and NOT EXISTS 
        (SELECT 1 FROM bookings b
        WHERE b.lessons_id = t.id AND b.user_id = ".$id.");
        ")->get();
        return $lesson;

}

并在视图中按名称调用变量

{{$ YourData}}和{{$ LeMiePrenotazioni}}

答案 1 :(得分:0)

您可以将两个功能合并为一个。 并且可以返回带有两个对象的视图。

return view('home', compact('YourData,lesson'));

答案 2 :(得分:0)

你应该试试这个:

public function index()
{

   $id =Auth::user()->id;

   $YourData = DB::table('users')
       ->join('packages', 'users.id', '=', 'packages.user_id')
       ->select('users.*','packages.name as packageName',DB::raw('SUM(packages.entry_nr) as entries'))
       ->where('users.id', $id)
       ->get();

   $lesson = DB::select("
       SELECT *
       FROM lessons t
       where (t.date > now())
      and NOT EXISTS
       (SELECT 1 FROM bookings b
       WHERE b.lessons_id = t.id AND b.user_id = ".$id.");
       ");

   return view('home', compact('lesson', 'YourData'));

}

答案 3 :(得分:0)

public function index()
{
    $YourData = DB::table('users')
        ->join('packages', 'users.id', '=', 'packages.user_id')
        ->select('users.*','packages.name as 
           packageName',DB::raw('SUM(packages.entry_nr) as entries'))
        ->where('users.id', Auth::id())
        ->get();

    $id =Auth::user()->id;
    $lesson = DB::select("
        SELECT * FROM lessons t where (t.date > now()) and NOT EXISTS 
            (SELECT 1 FROM bookings b WHERE b.lessons_id = t.id AND b.user_id = ".$id.");
    ");

    return view('home')->with(['yourData' => $yourData, 'lesson' => $lesson]);

}