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; ?>
. 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);
}