pranayaryal
4/6/2017 - 7:57 PM

Joins using laravel

Joins using laravel

$donors = \Donor::join('contacts', 'contacts.id', '=', 'donors.contact_id')
			->join('states', 'states.id', '=', 'donors.state_id')
			->join('chapters', 'chapters.id', '=', 'donors.chapter_id')
                        ->join('donations', 'donations.donor_id', '=', 'donors.id')
                        ->join('campaigns', 'campaigns.id', '=', 'donations.campaign_id')
                        ->join('funds', 'funds.id', '=', 'donations.fund_id')
			->whereIn('donors.id', $donor_ids)
			->where(function ($query) {
				if (\Input::get('donor_type_id')) {
					$query->where('donors.type_id', '=', \Input::get('donor_type_id'));
				}
				if (\Input::get('state_id')) {
					$query->where('donors.state_id', '=', \Input::get('state_id'));
				}
				if (\Input::get('chapter_id')) {
					$query->where('donors.chapter_id', '=', \Input::get('chapter_id'));
				}

			})->select('donors.*',
			'contacts.full_name',
			'contacts.prefix',
			'contacts.first_name',
			'contacts.middle_name',
			'contacts.last_name',
			'contacts.suffix',
			'contacts.address',
			'contacts.address_2',
			'contacts.city',
			'contacts.state',
			'contacts.postal_code',
			'states.name AS society_name',
			'chapters.name AS chapter_name',
                        'donations.anonymous',
                        'funds.account_number',
                        'campaigns.name as campaign_name'
		)->orderBy('contacts.full_name', 'ASC')
			->get();