2014-08-28 35 views
0

我嘗試使用diff-Function使用數據透視表來獲取與多個關係中某個項目無關的所有項目。

這就是我想出了: 我想使用diff($collection)功能,但我總是得到以下錯誤:

Argument 1 passed to Illuminate\Support\Collection::__construct() must be of the type array, object given, called in */vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 157 and defined

見的功能在這裏一個例子:我加入了http://www.neontsunami.com/post/new-collection-methods-in-laravel-4-1

以下兩行提供給我的供應商陣列中的app.php

'Illuminate\Support\Collection', 
'Illuminate\Database\Eloquent\Collection', 

這是我的代碼User.php

public function scopeNotProject($query, $project_id) 
    { $all_users = Users::all(); 
     $participants = Project::find($project_id)->users; 
     $nonparticipants = $all_users->diff($participants); 
     return $nonparticipants; 
    } 

和我的觀點:(?)

@foreach(User::notProject($project_id)->get() as $nonparticipant) 
        <option value="{{ $nonparticipant->id }}">{{ $nonparticipant->firstname . " " . $nonparticipant->lastname }}</option> 
@endforeach 

據我所看到的,通過項目應該是一個集合,而不是一個數組。我是laravel的新手,所以任何幫助表示讚賞!謝謝。

回答

0

這是因爲第一個參數需要是一個數組,它告訴你。而且由於用戶是一個集合,這是一個對象,你不能將一個數組與一個對象進行比較。

變化

$participants = Project::find($project_id)->users; 

$participants = Project::find($project_id)->users->toArray(); 
0

我找到了一種方法來解決我的問題:):

@foreach (User::all()->diff(Project::find($project_id)->users) as $nonparticipant) 
    <option value="{{ $nonparticipant->id }}">{{ $nonparticipant->firstname . " " . $nonparticipant->lastname }}</option> 
@endforeach