【问题标题】:How can I get a value from the URL of a view and use it in controller?如何从视图的 URL 中获取值并在控制器中使用它?
【发布时间】:2021-09-01 17:25:32
【问题描述】:

我有 2 个 Blade 视图。在第一个中,我发送了 productidredirect 到另一个 view 我需要 输入 其他内容的地方。在 控制器 中,我无法从第一个视图中获取 id,以便使用它将两个视图中的所有信息存储到我的数据库中。

我需要获取shoe_id/$product->id 值!

这是第一个视图:

<form action="{{ route('search') }}" method="GET">
    <input type="text" name="search" required/>
    <button class ="btn" type="submit">Search</button>
</form>
</h3>
@if($products->isNotEmpty())
    @foreach ($products as $product)
        <div class="col-md-2">
            <form action="sell/{{$product->id}}" method="POST">
                @csrf
                {{-- <a href="{{'products/sell/' . $product->id }}"> --}}
            <button name="shoe_id" value="{{ $product->id }}" class="btn">
                <img src="/imagini/{{ $product->id }}.1.jpg" style="height: 210px">
            </button>
                {{-- </a> --}}
            </form>
            <p style="text-align: center">{{ $product->title }}</p>
        </div>
    @endforeach

这是第二个视图:

<form action="/select/{product_id}" method="post">
    @csrf

    <h2>Select a size:</h2>
    <div class="row" style="margin-bottom: 50px">
        <select class="select2" name="size" style="width: 300px">
            <option>35</option>
            <option>35.5</option>
            <option>36</option>
            <option>36.5</option>
            <option>37</option>
            <option>37.5</option>
            <option>38</option>
            <option>38.5</option>
            <option>39</option>
            <option>39.5</option>
            <option>40</option>
            <option>40.5</option>
            <option>41</option>
            <option>41.5</option>
            <option>42</option>
            <option>42.5</option>
            <option>43</option>
            <option>43.5</option>
            <option>44</option>
            <option>44.5</option>
            <option>45</option>
            <option>45.5</option>
            <option>46</option>
            <option>46.5</option>
            <option>47</option>
            <option>47.5</option>
            <option>48</option>
            <option>48.5</option>
        </select>
    </div>

    <div class="row" style="margin-bottom: 50px">
        <h2>Insert the price:</h2>
        <input type="text" style="width: 300px" placeholder="$" name="price" required>
    </div>

    <input type="submit" class="btn btn-success btn-lg px-3" value="Sell!">
</form>

这里是控制器:

public function store($shoe_id){
    request()->validate(['price'=> 'required|max:255']);

    // $url = request('id');

    dd($shoe_id);

    ListedProducts::create([
        'user_id'=> auth()->id(),
        // 'shoe_id'=> ,
        'size'=>request('size'),
        'price'=>request('price')
    ]);

    return redirect('/home');
}

还有 web.php 文件:

Route::post('sell/{product_id}', [\App\Http\Controllers\ProductsController::class, 'sell'])->name('sell');
Route::get('sell/{product_id}', [\App\Http\Controllers\ProductsController::class, 'store']);
Route::post('select',[App\Http\Controllers\ProductsController::class, 'store']);

【问题讨论】:

    标签: php html laravel


    【解决方案1】:

    简单的方法就是使用 HTTP GET 的查询参数。

    重定向到/home时添加查询参数shoe_id

    return redirect()->route('home', ['shoe_id' => $shoe_id]);
    

    然后在您的home 控制器函数中,获取shoe_id 查询参数

    public function index(Request $request) {
        $shoe_id = $request->input('shoe_id');
        
        ...
    
    }
    

    编辑:请阅读此reference

    【讨论】:

      【解决方案2】:

      这是完成同样事情的另一种方法:

      return redirect("home?shoe_id={shoe_id}");
      

      然后在 index 方法中,您可以执行 @Ezra 建议的相同操作:

       public function index(Request $request) {
          $shoe_id = $request->input('shoe_id');
          
          ...
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-21
        • 2018-09-12
        • 2012-01-04
        • 1970-01-01
        • 2014-04-01
        • 2013-03-10
        • 1970-01-01
        相关资源
        最近更新 更多