with(' i', ($request->input ('page' , 1) - 1) * 5); ”-6ren"> with(' i', ($request->input ('page' , 1) - 1) * 5); ”-请原谅我问一个可能是初学者的问题。只是我真的在网上搜索了没有找到答案! SOF 似乎是我发疯之前的最后选择。 我的问题是,我根本无法理解这行代码的作用: ->with('i', ($request->-6ren">
gpt4 book ai didi

php - 理解 Laravel : Please explain "->with(' i', ($request->input ('page' , 1) - 1) * 5); ”

转载 作者:行者123 更新时间:2023-12-04 02:56:14 38 4
gpt4 key购买 nike

请原谅我问一个可能是初学者的问题。只是我真的在网上搜索了没有找到答案! SOF 似乎是我发疯之前的最后选择。

我的问题是,我根本无法理解这行代码的作用:

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

它位于我的 Controller index(request $Request) 方法中。

整体看起来是这样的:
public function index(Request $request)
{
$books = Book::indexBooks()->paginate(20);
return view('bookCRUD.index', compact('books'))
->with('i', ($request->input('page', 1) - 1) * 5);
}

另一个用户写了这个,但我无法理解。

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.



我会很高兴收到 build 性的答案!

最佳答案

with()方法用于向 View 发送数据。

documentation让它更清楚:

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


如您所见, with()接受两个参数:
  • 第一个(字符串)定义将返回到 View
  • 的变量的名称。
  • 第二个参数指定此变量将获得的实际值。

  • 因此,在您提供的代码中:

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

    这意味着在 View bookCRUD.index.blade.php 中, $i变量将可用,其值将是 $request->input('page', 1) - 1) * 5 的结果.

    所以,你可以用它做一些事情,比如:

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

    额外的:

    以下语句是等效的:

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

    与另一个:

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

    这两个语句都将使变量可用,在本例中为 $manager在 View 中使用。

    更新

    与您问题的第二部分相关,他/她所说的是该行将返回 5 个产品。对于它的外观,他/她指的是 compact('books')部分。这将返回给 View 一个变量(我可以假设它是 Book 对象的集合)。

    使用所说的其余内容只是对您所问内容的解释。唯一的细节是用户正在解释 $i 的值变量会得到。他/她正在使用 $request->input('field', 'default_variable')检索输入。检查 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');


    所以,正如你所看到的……这样做 $request->input('page', '1')表示如果 page字段在请求中定义,它将获取该值,如果不存在,则默认值为 1 .只是。

    这样看:

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

    关于php - 理解 Laravel : Please explain "->with(' i', ($request->input ('page' , 1) - 1) * 5); ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52996299/

    38 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com