简体   繁体   中英

Understanding Laravel: Please explain “->with('i', ($request->input('page', 1) - 1) * 5); ”

Please forgive me for asking a probably beginners-question. It's only that I really searched the net without finding answers! SOF seems like my last option before turning crazy.

My problem is, that I simply can't understand what this line of code does:

->with('i', ($request->input('page', 1) - 1) * 5);

It lays in my Controller index(request $Request) method.

The whole looks like this:

public function index(Request $request)
{
    $books = Book::indexBooks()->paginate(20);
    return view('bookCRUD.index', compact('books'))
        ->with('i', ($request->input('page', 1) - 1) * 5);
}

Another User wrote this, but I cant make sense of it.

That code will get the top 5 of all products, ordered by the id of products in descending order. Then the products data are passed into the view named index.blade.php inside ProductCRUD directory. You could find that directory on yourproject/resources/views. It also flashes a session variable named i (on the view you could access the variable using $i), which have the value of the form input / query string named page, if it exists. Otherwise, the $request->input('page', 1) = 1. From the usage of that variable, the $i will act as starting row number of each page on the grid.

I would be the happiest to receive constructive answers!

The with() method is used to send data to the view.

The documentation makes it clearer:

Passing Data To Views

As you saw in the previous examples, you may pass an array of data to views:

 return view('greetings', ['name' => 'Victoria']); 

When passing information in this manner, the data should be an array with key / value pairs. Inside your view, you can then access each value using its corresponding key, such as <?php echo $key; ?> <?php echo $key; ?> . As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view:

 return view('greeting')->with('name', 'Victoria'); 

As you can see, the with() accept two parameters:

  • The first one (a string) defines the name of the variable that will be returned to the view
  • The second parameter specifies the actual value that this variable will get.

So, in the code you provided:

return view('bookCRUD.index', compact('books'))
         ->with('i', ($request->input('page', 1) - 1) * 5);

This means that in the view bookCRUD.index.blade.php , the $i variable will be available and that its value will be the result of $request->input('page', 1) - 1) * 5 .

So, you could do something with it, like:

<p> The interesting value is: {{ $i }} </p>

Additional:

The following statements are equivalent:

return view('a_nice_view')->with('manager', $user);

with this other one:

return view('a_nice_view')->withManager($user); // sugared.

This both statements will make available the variable, in this case, $manager to be used in the view.


Update

Related to the second part of your question, what he/she is saying is that that line will return 5 products. For the way it looks, he/she is referring to the compact('books') part. This will return to the view a variable (that I can assume is a collection of Book objects).

The rest of what the use said is just an explanation of what you asked. The only detail is that the user is explaining tha value that the $i variable will get. He/she is using the $request->input('field', 'default_variable') to retrieve an input. Check the docs .

Retrieving An Input Value

Using a few simple methods, you may access all of the user input from your Illuminate\\Http\\Request instance without worrying about which HTTP verb was used for the request. Regardless of the HTTP verb, the input method may be used to retrieve user input:

 $name = $request->input('name'); 

You may pass a default value as the second argument to the input method. This value will be returned if the requested input value is not present on the request:

 $name = $request->input('name', 'Sally'); 

So, as you can see.. doing this $request->input('page', '1') means that if the page field is defined in the request, it will get that value, if not present, the default value will be 1 . Just that.

Look it in this way:

public function index(Request $request)
{
    $books = Book::indexBooks()->paginate(20);
    $value = ($request->input('page', 1) - 1) * 5; // this resolves the value to be retuned
    // so, if 'page' is defined in the request it will get the value.
    // if not, it will be '1', so doing the math: $value = 0.

    return view('bookCRUD.index', compact('books'))
        ->with('i', $value);
}

The with() method, when used on a View instance, makes variables available to a view: https://laravel.com/docs/5.7/views#passing-data-to-views

That code is making a variable $i available to the bookCRUD.index view and filling it with a value computed from the current request's input.

Another way to write that controller code for comparison (which should behave exactly the same) would be:

public function index(Request $request)
{
    $books = Book::indexBooks()->paginate(20);
    $i = ($request->input('page', 1) - 1) * 5);
    return view('bookCRUD.index', compact('books', 'i'));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM