我正在用laravel构建一个微型电子商务,我在分离购物车中添加的元素时遇到了问题。我将解释:例如,如果我在购物车中添加了4个元素,当我在购物车中添加的最后一个元素上单击“移除元素”时,最后一个元素不会被删除,但反之亦然,我添加的第一个元素也会被删除。这是我在laravel中的Controller文件:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Computer;
use App\Smartphone;
class FrontController extends Controller
{
public function cart()
{
$computers = Computer::all();
$smartphones = Smartphone::all();
return view('cart',compact('computers', 'smartphones'));
}
public function computers()
{
$computers = Computer::all();
return view('computers',compact('computers'));
}
public function smartphones()
{
$smartphones = Smartphone::all();
return view('smartphones',compact('smartphones'));
}
public function addpc($id)
{
$user = Auth::user();
if(!$user)
return redirect('/login');
$user->computers()->attach($id);
return redirect(route('cart'));
}
public function removepc($id2)
{
$user = Auth::user();
if(!$user)
return redirect('/cart');
$user->computers()->detach($id2);
return redirect(route('cart'));
}
public function addsmartphone($id3)
{
$user = Auth::user();
if(!$user)
return redirect('/login');
$user->smartphones()->attach($id3);
return redirect(route('cart'));
}
public function removesmartphone($id4)
{
$user = Auth::user();
if(!$user)
return redirect('/cart');
$user->smartphones()->detach($id4);
return redirect(route('cart'));
}
}这是我的web.php文件:
Route::get('/computers', 'FrontController@computers')->name('computers');
Route::post('/computers{id}/addpc','FrontController@addpc')->name('computers.addpc');
Route::post('/computers{id2}/removepc','FrontController@removepc')->name('computers.removepc');
Route::get('/smartphones', 'FrontController@smartphones')->name('smartphones');
Route::post('/smartphones{id3}/addsmartphone','FrontController@addsmartphone')->name('smartphones.addsmartphone');
Route::post('/smartphones{id4}/removesmartphone','FrontController@removesmartphone')->name('smartphones.removesmartphone');这是我的smartphones.blade.php文件:
@extends('layouts.app')
@section('content')
<main class="container">
<section class="row justify-content-center">
@foreach($smartphones as $smartphone)
<div class="col-12 col-md-5 my-5">
<h4 class="card-title text-center">{{$smartphone->name}}</h4>
<h6>{{$smartphone->description}}</h6>
<form action="{{route('smartphones.addsmartphone',['id3'=>$smartphone->id])}}" method="POST">
@csrf
<button class="btn btn-success my-1" type="submit">Add to cart!</button>
</form>
</div>
</div>
@endforeach
</section>
</main>
@endsection这是我的computers.blade.php文件:
@extends('layouts.app')
@section('content')
<main class="container">
<section class="row justify-content-center">
@foreach($computers as $computer)
<div class="col-12 col-md-5 my-5">
<h4 class="card-title text-center">{{$computer->name}}</h4>
<h6>{{$computer->description}}</h6>
<form action="{{route('computers.addpc',['id'=>$computer->id])}}" method="POST">
@csrf
<button class="btn btn-success my-1" type="submit">Add to cart!</button>
</form>
</div>
</div>
@endforeach
</section>
</main>
@endsection这是我的cart.blade.php文件:
@extends('layouts.app')
@section('content')
<main class="container">
<section class="row justify-content-center">
@php
$total = 0;
@endphp
@foreach(Auth::user()->computers()->get() as $computer)
<div class="col-12 col-md-4">
<h2 class="text-center">{{$computer->name}}</h2>
<form action="{{route('computers.removepc',['id2'=>$computer->id])}}" method="POST">
@csrf
<center><button class="btn btn-danger mb-5" type="submit">Remove</button></center>
<p class="text-center">Price: {{$computer->price}}</p>
@php
$total+= $computer->price
@endphp
</div>
@endforeach
</section>
</main>
<main class="container">
<section class="row justify-content-center">
@foreach(Auth::user()->smartphones()->get() as $smartphone)
<div class="col-12 col-md-4">
<h2 class="text-center">{{$smartphone->name}}</h2>
<form action="{{route('smartphones.removesmartphone',['id4'=>$smartphone->id])}}" method="POST">
@csrf
<center><button class="btn btn-danger mb-5" type="submit">Remove</button></center>
<p class="text-center">Price: {{$smartphone->price}}</p>
@php
$total+= $smartphone->price
@endphp
</div>
@endforeach
</section>
</main>
<main class="container">
<section class="row justify-content-center">
<h1>Total: {{$total}} €</h1>
</section>
</main>
@endsection我希望我已经解释清楚了,谢谢你的帮助!
发布于 2020-05-12 14:37:03
你需要关闭你的表单,对于每个类别,在循环中。
https://stackoverflow.com/questions/61745132
复制相似问题