我有 2 个 Blade View 。在第一个中,我发送了一个 product
的 id
并 redirect 到另一个 view
我需要 < em>输入其他东西。在 controller 中,我无法从第一个 View 中获取 id
以便使用它来存储我的两个 View 中的所有信息数据库。
我需要获取 shoe_id/$product->id
值!
这是第一个 View :
<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
这是第二个 View :
<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>
这是 Controller :
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']);
最佳答案
简单的方法就是使用 HTTP GET 的查询参数。
重定向到/home
时添加查询参数shoe_id
return redirect()->route('home', ['shoe_id' => $shoe_id]);
然后在您的home
Controller 函数中,获取shoe_id
查询参数
public function index(Request $request) {
$shoe_id = $request->input('shoe_id');
...
}
编辑:请阅读此reference .
关于php - 如何从 View 的 URL 中获取值并在 Controller 中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68010338/