Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:检查特定列透视表_Php_Laravel 4 - Fatal编程技术网

Php Laravel:检查特定列透视表

Php Laravel:检查特定列透视表,php,laravel-4,Php,Laravel 4,我有一个多对多关系,表上有一个附加值列。如何选择该值?所以 pivot_table - user_id - something_id - value 型号用户: return $this->belongsToMany('Something', 'user_something')->withPivot('value'); 现在,我想得到一个用户的值,其中something id是2 大概是这样的: $user = User::find(1); $user->something

我有一个多对多关系,表上有一个附加值列。如何选择该值?所以

pivot_table
- user_id
- something_id
- value
型号
用户

return $this->belongsToMany('Something', 'user_something')->withPivot('value');
现在,我想得到一个用户的值,其中something id是2

大概是这样的:

$user = User::find(1);
$user->something->whereSomethingId(2)->pivot->value;
你可以试试这个:

$user->something->find(2)->pivot->value;
由于
something
是一个集合,因此我们可以使用
find($id)
方法通过它的
id
获取项目。此外,您还可以使用
filter(callback)
方法使用循环过滤集合,并在每个循环中运行回调(作为参数传入
filter
方法),例如:

$user->something->filter(function($item){
    // $item is an object from the collection
    // So, you may use: $item->pivot->value
});
你也可以阅读