How to do join query in laravel - php

Business Model
public function groupTag()
{
return $this->belongsTo('GroupTag');
}
Group Tag Model
public function tag()
{
return $this- >belongsToMany('Tag','group_tag_tags','group_tag_id','tag_id')
->withTimestamps();
}
public function business()
{
return $this->hasOne('Business');
}
Tag Model
public function groupTag()
{
return $this->belongsToMany('Group','group_tag_tags','group_tag_id','tag_id')->withTimestamps();
}
Now how do i run this query into a laravel project
SELECT * FROMbusinesses` as b,
group_tags as gt,
group_tag_tags as gtt,
tags as t
where b.group_tag_id = gt.id and gt.id = gtt.group_tag_id and gtt.tag_id = t.id and t.id = 36 or b.name like '%a%' and b.city_id = 5 group by b.id'

DB::table('businesses')
->join('group_tags','group_tags.id','=','businesses.group_tag_id')
->join('group_tag_tags','group_tag_tags.group_tag_id','=','group_tags.id')
->join('tags','tags.id','=','group_tag_tags.tag_id')
->where('tags.id',"=",36)
->where('b.name',"LIKE",'%a%')
->where('b.city_id',"=",5)
->select('businesses.id','businesses.name','businesses.description','businesses.image')
->groupBy('businesses.id')

Related

Left Join with Multiple on clause Eloquent Laravel

I have the following mysql query
select * from operatories o
left join apts ap on o.location = ap.Location and o.operatory = ap.Operatory;
I also have two models
class Operatories extends Model
{
public function operatories()
{
return $this->hasMany('App\Models\Appointment', 'Location', 'location');
}
}
Appointments:
class Appointment extends Model
{
public function operatories()
{
return $this->belongsTo('App\Models\Operatories', 'operatory', 'Operatory');
}
}
In my controller I am trying to write the mysql query,
this is what I have so far
$arrOperators = Operatories::leftJoin('appointment', function ($join) {
$join->on('appointments.location','=','operatories.location');
})
does anyone know how to write multiple on clause?

laravel 5 get data from 3 tables with group by

I have 3 tables schools, classes, attendances which are
schools
public function classes() {
return $this->hasMany('App\Models\Classes');
}
classes
public function school() {
return $this->belongsTo('App\Models\School');
}
public function attendances() {
return $this->hasMany('App\Models\Attendance');
}
attendances
public function classes() {
return $this->belongsTo('App\Models\Classes');
}
Now I want to get data of attendances group by date which consist of classes for each school, like following
date1{
total attendance: totalNumber,
classes{
class1{},
class2{},
}
},
date2{
....
}

Laravel Eloquent model JOIN a MAX record

I'm new to Laravel and I'd like to know how to use eloquent model to join a max(date) record...
App\SchemasGroup::find(7)->schemas()->get()->max('schemasVersion')->get();
I find a group of schemas (schemas_groups table) based on ID, then get the schemas from that group (schemas table), and I want to join the 'date' and 'version' field from schemas_versions table with the last version (so, max date or version field) ...
Relations are defined as:
class SchemasGroup extends Model
{
public function schemas() { return $this->hasMany('App\Schema'); }
}
class Schema extends Model
{
public function group() { return $this->belongsTo('App\SchemasGroup'); }
public function versions() { return $this->hasMany('App\SchemasVersion'); }
}
class SchemasVersion extends Model
{
public function schema() { return $this->belongsTo('App\Schema'); }
public function updatedBy() { return $this->belongsTo('App\User','updated_by'); }
}
Getting the user name who updated that last version would also be lovely...
Apparently it was easy with defining chaining models.
class Schema extends Model
{
public function group() { return $this->belongsTo('App\SchemasGroup'); }
public function versions() { return $this->hasMany('App\SchemasVersion'); }
public function latestVersion() { return $this->hasOne('App\SchemasVersion')->latest(); }
}
and then fetching the data with:
App\SchemasGroup::with('schemas.latestVersion.updatedBy')->find($schemaGroupId);

yii active record via issue

Please help;
i have models:
class Service extends \yii\db\ActiveRecord{
public function getCategory()
{
return $this->hasOne(ServiceCategory::className(), ['id' => 'service_category_id']);
}
}
class ServiceOffer extends \yii\db\ActiveRecord
{
public function getService()
{
return $this->hasOne(Service::className(), ['id' => 'service_id']);
}
public function getCategory()
{
return $this->hasOne(ServiceCategory::className(), ['id' => 'service_category_id'])->via('service');
}
public function getProperties()
{
return $this->hasMany(ServiceProperty::className(), ['service_category_id' => 'id'])
->via('category');
}
}
When i do query:
$query = ServiceOffer::find()->with(['properties'])->all();
or:
$query = ServiceOffer::find()->joinWith(['properties'])->all();
i have error:
Getting unknown property: app\models\ServiceOffer::service_category_id
but if i do this for see query sql:
$query = ServiceOffer::find()->joinWith(['properties']);
echo $query->prepare(\Yii::$app->db->queryBuilder)->createCommand()->rawSql;
sql:
SELECT "service_offer".* FROM "service_offer"
LEFT JOIN "service" ON "service_offer"."service_id" = "service"."id"
LEFT JOIN "service_category" ON "service"."service_category_id" = "service_category"."id"
LEFT JOIN "service_property" ON "service_category"."id" = "service_property"."service_category_id"
query without problem and execute
Why ActiveRecord find service_category_id property in ServiceOffer model?

Laravel return property from third table

I have three tables:
products
product_types
product_categories
products belongs to product_types and product_types belongs to product_categories.
How can I access a column from product_categories from products?:
ProductTypes model:
class ProductTypes extends Eloquent {
public function category() {
return $this->belongsTo('ProductCategories');
}
}
Products model:
class Product extends Eloquent {
protected $table = 'products';
public function brands() {
return $this->belongsTo('ProductBrands', 'brand_id', 'id');
}
public function ages() {
return $this->belongsTo('ProductAges', 'age_id', 'id');
}
public function types() {
return $this->belongsTo('ProductTypes', 'type_id', 'id');
}
public function images() {
return $this->hasMany('ProductImages');
}
public function reviews() {
return $this->hasMany('ProductReviews');
}
public function toArray() {
$ar = $this->attributes;
$ar['brand'] = $this->brand;
$ar['age'] = $this->age;
$ar['type'] = $this->type;
return $ar;
}
public function getBrandAttribute() {
$brands = $this->brands()->first();
return (isset($brands->brand) ? $brands->brand : '');
}
public function getAgeAttribute() {
$ages = $this->ages()->first();
return (isset($ages->age) ? $ages->age : '');
}
public function getTypeAttribute() {
$types = $this->types()->first();
return (isset($types->type) ? $types->type : '');
}
}
I have tried:
$productData->types()->category()->category
But this gives an error saying the method doesn't exist.
Sorry about the title, couldn't think of one.
The problem is, that you're not executing the query when doing types() and therefore you're calling category() on the query builder instance and not the ProductTypes model.
You can use the dynamic properties for accessing the result of the relationship.
$productData->types->category->category
Also consider renaming your relationships. Currently you have "types" for example but it only returns one type, because its a one-to-many relation. "type" would make more sense.

Categories