bayramgeldi's avatar

How can I run two functions on same url

Sorry for noob questions. I have pagination and entry fields, they do not work together. Here is screenshot: Screenshot

It is controller:

<?php

namespace app\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Entry;
use Illuminate\Http\Request;



/**
 *
 */
class pagesController extends Controller {

public function getWelcome()
  {
    $entry = Entry::inRandomOrder()->first();

    return view('entries.show')->with('entry', $entry);
  }

  public function Pagination()
    {
      $entry = Entry::paginate(15);

      return view('partials.left_sidebar')->with('entry', $entry);
    }


}

Here is Routes:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', 'pagesController@getWelcome');
Route::get('/', 'pagesController@Pagination');

// Route::get('home', function () {
//     return view('home');
// });
Route::resource('entries', 'EntryController');

Auth::routes();

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

0 likes
7 replies
Yamen's avatar

The problem is here:

Route::get('/', 'pagesController@getWelcome');
Route::get('/', 'pagesController@Pagination');

The get request to "/" doesn't know where to go, you are providing two functions with the same route, so you may change one of them and try again or give it query string.

Route::get('/random-entries', 'pagesController@getWelcome');
Route::get('/entries', 'pagesController@Pagination');

Or if the same url then pass a query string like that

Route::get('/?random=1', 'pagesController@getWelcome'); // or make this from the view
Route::get('/', 'pagesController@Pagination');

and in contoller

public function getWelcome()
  {
     if(request('random')) return $this->Pagination();
    $entry = Entry::inRandomOrder()->first();

    return view('entries.show')->with('entry', $entry);
  }

  public function Pagination()
    {
      $entry = Entry::paginate(15);

      return view('partials.left_sidebar')->with('entry', $entry);
    }
1 like
tisuchi's avatar

@Yamen

I believe same functions for two different routes is ok. The only problem I can see it in default route where you have redeclared. Changing that is good enough.

@BAYRAMGELDI I am not sure that what exactly you want to do? Can you explain more about it?

2 likes
bayramgeldi's avatar

Maybe I misunderstood routes wrong. I need pagination on left and random entry on center on home page. If I do changes like you say, How can I show it on home page

Snapey's avatar
Snapey
Best Answer
Level 122

You need one route that returns a view for the entire page.

Within that view, you will include the sidebar and pass it the sidebar data.

Route;

Route::get('/', 'pagesController@index');

In the controller;

class pagesController extends Controller {

public function index()
  {
    $random = Entry::inRandomOrder()->first();

    $entries = Entry::paginate(15);

      return view('entries.show')->with('entries',$entry)->with('random',$random);
    }
}

Now in your view you have access to $random (a single Entry) and $entries (a collection of Entry - paginated)

In your layout, include the sidebar and use the $entries within it.

1 like
babonday's avatar

so you make multi queries in 1 function to get the route to work? Is this a clean way to write reusable code?

Please or to participate in this conversation.