我想獲得存在的所有color_code.code和manufacturer_bundle.name的唯一組合。他們通過一個表製造商連接篩選2列以獲得獨特組合的方法!與laravel /雄辯
這是我目前的代碼。
$color_codes = ColorCode::select(['color_code.code', 'manufacturer_bundle.name'])->distinct()
->leftJoin('manufacturer_bundle', 'color_code.manufacturer_id' , '=' , 'manufacturer_bundle.id')
->get();
的問題它,是,只有選擇那些收益領域,而不是實際的模型。所以,我希望能夠做到這一點:
$color_code->manufacturer->name
這給了我
試圖讓非對象
財產的完整性:
的ColorCode:
Schema::create('color_code', function (Blueprint $table) {
$table->increments('id');
$table->string('code');
$table->index('code');
$table->integer('manufacturer_id');
$table->index('manufacturer_id');
$table->timestamps();
});
製造商
Schema::create('manufacturer', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->index('name');
$table->integer('manufacturer_bundle_id')->nullable();
$table->index( 'manufacturer_bundle_id');
$table->timestamps();
});
看到這裏http://stackoverflow.com/questions/11277251/selecting-distinct-2-columns-combination-in-mysql從 – Toskan