繁体   English   中英

如何从所选用户的数据透视表更新列值?

[英]How to update a column value from a pivot table of a selected user?

我是数据透视表和laravel的新手,我很难更新数据透视表的特定列。 我尝试在线搜索,但似乎无济于事。 我正在尝试更新所选用户的状态列。 请注意,作为管理员,我可以查看客户资料并更新他们的信息。 这是我所拥有的:

客户表

id | 名称| 预算

产品表

id | 名称| 价钱

customer_product数据透视表

product_id | customer_id | 数量 状态

产品型号

public function customers()
{
    return $this->belongstoMany('App\Customer', 'product_customer')
    ->withPivot('status','qty');

}  

客户模型

public function products()
{
    return $this->belongsToMany('App\Product', product_customer)
    ->where('status', '=', '1')
    ->withPivot('qty', 'status');
}

控制者
这是我遇到的麻烦,在updateExistingPivot需要$ id之后,我该如何放置我当前正在查看的当前客户资料的$ id或我以错误的方式理解它。 该代码可以运行,但不会执行任何操作。 它不会更新状态列。 我希望能够将状态从1更改为0。

public function updateProductStatus($id)
 {
  Product::find($id)->customers()->updateExistingPivot($id, array('status' => 0), false);
 }  

public function getCustomerProfile($id)
{
  $customers = Customer::find($id);
  foreach ($customers->products as $product) 
  {
    $product->pivot->product_customer;
  } 
  $products = Product::lists('name');
  return view('admin.customer-profile', ['customers' => $customers], compact('products') );
}  

public function index()
{
  $customers = DB::table('customers')
  ->select('name')
  ->get();
  return view('admin.index', compact('customers'));
}  

路线

Route::put('updateProductStatus/{id}', ['as' => 'updateProductStatus',
        'uses' => 'AdminController@updateProductStatus']);
Route::get('getCustomerProfile/{id}', ['as' => 'getCustomerProfile',
        'uses' => 'AdminController@getCustomerProfile']);

<table class="table">
        <thead>
         <tr>   
          <th>Name</th>
            <th>Qty</th>
            <th>Status</th>                                         
         </tr>
        </thead>
            @foreach($customers->products as $product)
            <tbody>
             <tr>
              <td>{{$product->name}}</td>
              <td>{{$product->pivot->qty}}</td>
              <td>{!!Form::open(array('route'=>['updateProductStatus', $product->pivot->status], 'method'=>'PUT'))!!}
              {!!Form::button('Update', ['class'=>'btn btn-danger', 'type'=>'submit'])!!}
              {!!Form::close()!!}<td>
              </tr>
              </tbody>
            @endforeach
</table>

main.blade.php

<table id="datable_1" class="table table-hover display  pb-30" >
  <thead>
   <tr>
    <th>Name</th>
    <th>Action</th>
   </tr>
  </thead>
   @foreach($customers as $customer)
    <tbody>
     <tr>
      <td>{{$customer->name}}</td>
       <td>{!!Form::open(array('route'=>['deleteCustomerProfile', $customer->id], 'method'=>'DELETE'))!!}
           {{ link_to_route('getCustomerProfile', 'Edit', [$customer->id] , ['class' => 'btn btn-primary'])}}|
           {!!Form::button('Delete', ['class'=>'btn btn-danger', 'type'=>'submit'])!!}
           {!!Form::close()!!}
           </td>
          </tr>
         </tbody>
        @endforeach
       </table>

上方刀片的URL

/ admin / getCustomerProfile / 1

错误

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException thrown with message ""

Stacktrace:
#19 Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:218
#18 Illuminate\Routing\RouteCollection:methodNotAllowed in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:205
#17 Illuminate\Routing\RouteCollection:getRouteForMethods in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:158
#16 Illuminate\Routing\RouteCollection:match in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Routing\Router.php:821
#15 Illuminate\Routing\Router:findRoute in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Routing\Router.php:691
#14 Illuminate\Routing\Router:dispatchToRoute in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Routing\Router.php:675
#13 Illuminate\Routing\Router:dispatch in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:246
#12 Illuminate\Foundation\Http\Kernel:Illuminate\Foundation\Http\{closure} in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:52
#11 call_user_func in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:52
#10 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php:44
#9 Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode:handle in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:136
#8 call_user_func_array in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:136
#7 Illuminate\Pipeline\Pipeline:Illuminate\Pipeline\{closure} in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:32
#6 call_user_func in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:32
#5 Illuminate\Routing\Pipeline:Illuminate\Routing\{closure} in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:103
#4 call_user_func in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:103
#3 Illuminate\Pipeline\Pipeline:then in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:132
#2 Illuminate\Foundation\Http\Kernel:sendRequestThroughRouter in G:\xampp\htdocs\theshop\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:99
#1 Illuminate\Foundation\Http\Kernel:handle in G:\xampp\htdocs\theshop\public\index.php:54
#0 require_once in G:\xampp\htdocs\theshop\server.php:21

我将发布完整的解释和解决方案。

强烈建议您将方法名称“ belongstoMany更改为“ belongsToMany ,但这是可选的,这要感谢@amphetamachine检查注释以获取解释。

public function customers()
{
    return $this->belongsToMany('App\Customer', 'customer_product')
               ->withPivot('status', 'qty');

}

您的控制器代码

use App\Customer;
use App\Product;
...
...
public function updateProductStatus(Customer $customer, Product $product)
{
  $pivotProduct = $customer->products()->wherePivot('product_id', $product->id);
  $pivotProduct->updateExistingPivot($customer->id, array('status' => $pivotProduct->pivot->status == 0 ? 1:0));
}

public function getCostumerProfile(Customer $customer)
{
  return view('admin.costumer-profile', ['customer' => $customer]);
}

public function index()
{
  $customers = Customer::all();
  return view('admin.index', ['customers' => $customers]);
}

您的路由文件现在包括您正在接收的这两个参数,它们是customerproduct并且通过路由模型绑定,它们将自动解析为各自的对象

Route::put('updateProductStatus/{customer}/{product}', ['as' => 'updateProductStatus',
    'uses' => 'AdminController@updateProductStatus']);

Route::get('getCostumerProfile/{customer}', ['as' => 'getCostumerProfile',
    'uses' => 'AdminController@getCostumerProfile']);

您的刀片文件应首先遍历所有客户,对于每个客户,将遍历其所有产品并将其显示在表格中,请考虑您可以更改表格的设计

<table class="table">
      <thead>
         <tr>   
            <th>Name</th>
            <th>Qty</th>
            <th>Status</th>                                         
         </tr>
      </thead>
      <tbody>
      @if($customer->products()->count() > 0)
      @foreach($customer->products as $product)
         <tr>
            <td>{{$product->name}}</td>
            <td>{{$product->pivot->qty}}</td>
            <td>{!!Form::open(array('route' => ['updateProductStatus', ['customer' => $customer, 'product' => $product]], 'method'=>'PUT'))!!}
            {!!Form::button('Update', ['class'=>'btn btn-danger', 'type'=>'submit'])!!}
            {!!Form::close()!!}<td>
         </tr>
      @endforeach
      @else
         <tr>
            <td colspan="3">No products found for this customer.</td>
         </tr>
      @endif
      </tbody>
</table>

最后您的main.blade.php应该看起来像这样

<table id="datable_1" class="table table-hover display  pb-30" >
   <thead>
     <tr>
        <th>Name</th>
        <th>Action</th>
     </tr>
  </thead>
  @foreach($customers as $customer)
  <tbody>
      <tr>
        <td>{{$customer->name}}</td>
        <td>{!!Form::open(array('route'=>['deleteCostumerProfile', $customer->id], 'method'=>'DELETE'))!!}
        {{ link_to_route('getCostumerProfile', 'Edit', ['customer' => $customer->id] , ['class' => 'btn btn-primary'])}}|
        {!!Form::button('Delete', ['class'=>'btn btn-danger', 'type'=>'submit'])!!}
        {!!Form::close()!!}
        </td>
      </tr>
  </tbody>
  @endforeach
</table>

我在使用updateExistingPivot()函数时遇到了麻烦,因为它会更新具有相同客户ID的所有行的状态。 所以相反,我只是使用数据库更新。 这是我的解决方案

public function updateProductStatus($id)
{
 DB::table('customer_product')
 ->where('product_id', $id)
 ->update(['status' => 0]);
 return redirect()->back();
}

在我的刀片中,我只是换了

{!!Form::open(array('route'=>['updateProductStatus', $product->pivot->status], 'method'=>'PUT'))!!}

进入

{!!Form::open(array('route'=>['updateProductStatus', $product->pivot->product_id], 'method'=>'PUT'))!!}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM