This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
deroccha
Having the following table structure
clients
| id | name | |----|---------------| | 1 | some client |
materials
| id | name | |----|---------------| | 1 | material_name|
material_client_prices
| client_id | material_id | price | |-----------|-------------|-------| | 1 | 2 | 56.78 |
than try to build a many to many relationship with belongsToMany in a relation form
Client Model
class Client extends Model
{
use \October\Rain\Database\Traits\Validation;
/**
* @var string The database table used by the model.
*/
public $table = 'unysoft_ilta_clients';
/**
* @var array Guarded fields
*/
protected $guarded = ['*'];
/**
* @var array The rules to be applied to the data.
*/
public $rules = [
'name' => 'required|min:3|max:255',
'company_reg_nr' => 'required|min:3|max:255',
'county' => 'required',
//'city' => 'required',
'zip' => 'required|min:5|max:255',
'address' => 'required|min:5|max:255',
];
/**
* @var array Guarded fields
*/
protected $fillable = ['name', 'company_reg_nr', 'county', 'city', 'zip', 'address'];
/**
* @var array Relations
*/
public $belongsTo = [
'user' => ['RainLab\User\Models\User'],
'county' =>['Unysoft\Ilta\Models\County'],
'city' =>['Unysoft\Ilta\Models\City'],
];
public $belongsToMany = [
'materials' =>[
'Unysoft\Ilta\Models\Material',
'table' => 'unysoft_ilta_material_prices',
'key'=>'client_id'
],
'delivered_items' => [
'Unysoft\Ilta\Models\Delivery',
'table'=>'unysoft_ilta_delivered_items',
'key' =>'transaction_id',
'otherKey' => 'material_id',
'pivot' => ['price', 'countity'],
],
];
public $hasMany = [
'price_plans' =>['Unysoft\Ilta\Models\MaterialPrice'],
'deliveried_materials' =>['Unysoft\Ilta\Models\DeliveredItem','key'=>'client_id'],
];
public $morphTo = [
'client_id' =>[]
];
public function getCountyOptions()
{
return County::getNameList();
}
public function getCityOptions()
{
return City::getNameList($this->county_id);
}
public function scopeIsSupplier($query){
$query = $query->where('type', 'supplier')->has('price_plans');
return $query;
}
}
Material Model
/**
* Material Model
*/
class Material extends Model
{
use \October\Rain\Database\Traits\Validation;
/**
* @var string The database table used by the model.
*/
public $table = 'unysoft_ilta_materials';
public $rules = [
'name' => 'required|min:3|max:255|unique:unysoft_ilta_materials',
];
/**
* @var array Guarded fields
*/
protected $guarded = ['*'];
/**
* @var array Fillable fields
*/
protected $fillable = [
'name','materials'
];
/**
* @var array Relations
*/
public $belongsToMany =[
'clients' =>[
'Unysoft\Ilta\Models\Client',
'table' => 'unysoft_ilta_material_prices',
'key'=>'material_id'
],
];
public $hasOne = [
'material' => ['Unysoft\Ilta\Models\Material'],
];
public function scopeIsAvailable($query)
{
$query = $query->with('price_plans');
dump($query->toSql());
}
}
I can't get pivot price to show up in my fields
Last updated
1-2 of 2