Reputation: 31
I have 2 Blade views. In the first one I send out an id
of a product
and redirect to another view
where I need to input other stuff. In the controller I can't get the id
from the first view in order to use it to store all the information from both views in my database.
I need to get the shoe_id/$product->id
value!
Here is the first 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
Here is the second 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>
Here is the 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');
}
And the web.php file:
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']);
Upvotes: 3
Views: 386
Reputation: 567
Here is another way you could accomplish the same thing:
return redirect("home?shoe_id={shoe_id}");
then in the index method you could do the same thing as @Ezra suggested:
public function index(Request $request) {
$shoe_id = $request->input('shoe_id');
...
}
Upvotes: -1
Reputation: 416
The simple way is just to use query params for HTTP GET.
When redirecting to /home
add the query param shoe_id
return redirect()->route('home', ['shoe_id' => $shoe_id]);
Then in your home
controller function, get the shoe_id
query param with
public function index(Request $request) {
$shoe_id = $request->input('shoe_id');
...
}
Edit: please read this reference.
Upvotes: 3