James Moore
James Moore

Reputation: 39

Laravel 5.6 different functions return to same view

I have different function in my 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'));

}

How can I make so that I can use the two function inside my home.blade.php my route is

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

Upvotes: 2

Views: 236

Answers (4)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You should try this:

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'));

}

Upvotes: 0

Chirag Patel
Chirag Patel

Reputation: 1642

You can do like this

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;

}

And call the variables simply by name in the view

{{ $YourData }} and {{ $LeMiePrenotazioni}}

Upvotes: 2

Joe
Joe

Reputation: 4738

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]);

}

Upvotes: 0

zain qayyum
zain qayyum

Reputation: 49

you can merge both functions into one. and can return view with two objects.

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

Upvotes: 0

Related Questions