首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel分离错误的id

Laravel分离错误的id
EN

Stack Overflow用户
提问于 2020-05-12 14:24:09
回答 1查看 71关注 0票数 0

我正在用laravel构建一个微型电子商务,我在分离购物车中添加的元素时遇到了问题。我将解释:例如,如果我在购物车中添加了4个元素,当我在购物车中添加的最后一个元素上单击“移除元素”时,最后一个元素不会被删除,但反之亦然,我添加的第一个元素也会被删除。这是我在laravel中的Controller文件:

代码语言:javascript
复制
<?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文件:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
@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文件:

代码语言:javascript
复制
@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文件:

代码语言:javascript
复制
@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

我希望我已经解释清楚了,谢谢你的帮助!

EN

回答 1

Stack Overflow用户

发布于 2020-05-12 14:37:03

你需要关闭你的表单,对于每个类别,在循环中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61745132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档