我不熟悉数据透视表和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>
上方刀片的网址
/ 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
答案 0 :(得分:0)
我将发布完整的解释和解决方案..
强烈建议您将方法名称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]);
}
您的路线文件现在包含您接收的那两个参数,它们是customer
和product
,并且由于Route Model Binding,它们将自动解析为各自的对象
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>
答案 1 :(得分:0)
我遇到了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'))!!}