Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to create Relationships Many To Many #733

Open
mikehsieh0305 opened this issue Oct 27, 2023 · 2 comments
Open

how to create Relationships Many To Many #733

mikehsieh0305 opened this issue Oct 27, 2023 · 2 comments

Comments

@mikehsieh0305
Copy link

I have response success but relationship can't create

I have another question how can i update relationship

This is my odata api use POST

{{APP_API}}/odata/Events(2)

BODY

{
    "title": "阿瑪斯號貨輪油污事件",
    "description": "2001年1月14日,希臘籍三萬五千噸貨輪阿瑪斯號(Αμοργός)滿載礦砂,由印度駛往中國大陸,行經台灣南部海域時,阿瑪斯號失去動力。漂流12小時後,當晚20時左右,在墾丁海域擱淺。台灣交通部和國軍搜救中心接報後,立即展開救援行動,於23時將船上25名船員全數救出。1月15日,花蓮港務局成立災害處理中心,準備防止漏油的相關措施,並要求船東及保險公司儘快出面處理。[2][3]\n\n18日,阿瑪斯號船身出現破裂情形,並開始漏油;環保署立即依2000年10月通過的《海洋污染防治法》規定,函請花蓮港務局限制所有阿瑪斯號船員出境",
    "date_occurred": "2001-01-14",
    "created_at": "2023-10-24T16:43:27+08:00",
    "updated_at": "2023-10-25T12:58:34+08:00",
    "EventFeatures": [
        {
            "name": "油污範圍",
            "description": "由於受污染的龍坑生態保護區交通不便,且是珊瑚礁地形,油污遍布礁石及岩縫,加上時值東北季風期,海上風力強勁,導致海象惡劣,海上作業無法進行。到場協助處理的中油公司也因船隻無法接近阿瑪斯號貨輪,而表示無力協助處理。中油無法協助,環保署便函請交通部動員軍方人力和設備協助。但是,軍方船隻仍受限於海象而無法出海,僅能以人力在岸上協助搶救。另一方面,由船東所僱請的救難船,至2月3日總共抽取217.6公噸海上燃油。\r\n\r\n2月6日,環保署正式組成跨部會應變小組,協調內政部、交通部、國防部、海巡署、農委會、屏東縣政府、中油公司等單位,分別進行搶救工作的進度規劃和後續相關事宜。\r\n\r\n海岸油污撈除工作進行至2月16日,總計投入近一萬人次,撈除油污達四百六十二公噸。2月17日至3月24日進行油污清除工作,總計動員超過兩萬一千人次,清除油污513公噸。礁岩的除污及清洗工作,在3月25日至5月18日,以高壓水槍方式進行,投入人力接近三萬五千人次,清除油污達549公噸,清理的廢棄物超過三千五百公噸。[2][3]\r\n\r\n由船東負責的船上殘油抽取工作,分別在三月和五、六月進行,總計清除148.8公噸油污,至6月12日完成。清除工作未完成之前,屏東縣政府每日對船公司開出新台幣一百五十萬元的罰單,共計九十八日。\r\n\r\n油污清除及殘油抽取工作完成後,交通部於7月2日,在事故現場組成貨船移除小組,進行移除工作,清除船上礦砂,移至水深1000公尺處沉放,沉放工作於10月16日完成。然而,船體殘骸移除工作受限於颱風的連續侵襲而無法進行。",
            "geometry_scope": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            363772.44397377,
                            6475237.695186
                        ],
                        [
                            507130.09052262,
                            6586310.2519129
                        ],
                        [
                            594086.76482554,
                            6472829.928795
                        ],
                        [
                            555189.87205692,
                            6377675.3832085
                        ],
                        [
                            344825.61558758,
                            6327083.6251123
                        ],
                        [
                            306100.7061326,
                            6353253.7526714
                        ],
                        [
                            363772.44397377,
                            6475237.695186
                        ]
                    ]
                ]
            },
            "time": "2001-01-14T09:30:55+08:00",
            "created_at": "2023-10-24T16:49:06+08:00",
            "updated_at": "2023-10-25T11:04:21+08:00"
        },
        {
            "name": "油污範圍",
            "description": null,
            "geometry_scope": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            363772.44397377,
                            6475237.695186
                        ],
                        [
                            507130.09052262,
                            6586310.2519129
                        ],
                        [
                            594086.76482554,
                            6472829.928795
                        ],
                        [
                            555189.87205692,
                            6377675.3832085
                        ],
                        [
                            344825.61558758,
                            6327083.6251123
                        ],
                        [
                            306100.7061326,
                            6353253.7526714
                        ],
                        [
                            363772.44397377,
                            6475237.695186
                        ]
                    ]
                ]
            },
            "time": "2001-01-14T00:00:00+08:00",
            "created_at": "2023-10-24T20:40:49+08:00",
            "updated_at": "2023-10-24T20:40:49+08:00"
        },
        {
            "name": "油污範圍",
            "description": null,
            "geometry_scope": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            363772.44397377,
                            6475237.695186
                        ],
                        [
                            507130.09052262,
                            6586310.2519129
                        ],
                        [
                            594086.76482554,
                            6472829.928795
                        ],
                        [
                            555189.87205692,
                            6377675.3832085
                        ],
                        [
                            344825.61558758,
                            6327083.6251123
                        ],
                        [
                            306100.7061326,
                            6353253.7526714
                        ],
                        [
                            363772.44397377,
                            6475237.695186
                        ]
                    ]
                ]
            },
            "time": "2001-01-14T00:00:00+08:00",
            "created_at": "2023-10-24T20:46:35+08:00",
            "updated_at": "2023-10-24T20:46:35+08:00"
        },
        {
            "name": "油污範圍",
            "description": null,
            "geometry_scope": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            363772.44397377,
                            6475237.695186
                        ],
                        [
                            507130.09052262,
                            6586310.2519129
                        ],
                        [
                            594086.76482554,
                            6472829.928795
                        ],
                        [
                            555189.87205692,
                            6377675.3832085
                        ],
                        [
                            344825.61558758,
                            6327083.6251123
                        ],
                        [
                            306100.7061326,
                            6353253.7526714
                        ],
                        [
                            363772.44397377,
                            6475237.695186
                        ]
                    ]
                ]
            },
            "time": "2001-01-14T00:00:00+08:00",
            "created_at": "2023-10-24T20:49:12+08:00",
            "updated_at": "2023-10-24T20:49:12+08:00"
        }
    ]
}

Response

{
    "@context": "http://127.0.0.1:8000/odata/$metadata#Events/$entity",
    "id": 2,
    "title": "阿瑪斯號貨輪油污事件",
    "description": "2001年1月14日,希臘籍三萬五千噸貨輪阿瑪斯號(Αμοργός)滿載礦砂,由印度駛往中國大陸,行經台灣南部海域時,阿瑪斯號失去動力。漂流12小時後,當晚20時左右,在墾丁海域擱淺。台灣交通部和國軍搜救中心接報後,立即展開救援行動,於23時將船上25名船員全數救出。1月15日,花蓮港務局成立災害處理中心,準備防止漏油的相關措施,並要求船東及保險公司儘快出面處理。[2][3]\n\n18日,阿瑪斯號船身出現破裂情形,並開始漏油;環保署立即依2000年10月通過的《海洋污染防治法》規定,函請花蓮港務局限制所有阿瑪斯號船員出境",
    "date_occurred": "2001-01-14",
    "EventFeatures": [
        {
            "id": 45,
            "name": "油污範圍",
            "description": "由於受污染的龍坑生態保護區交通不便,且是珊瑚礁地形,油污遍布礁石及岩縫,加上時值東北季風期,海上風力強勁,導致海象惡劣,海上作業無法進行。到場協助處理的中油公司也因船隻無法接近阿瑪斯號貨輪,而表示無力協助處理。中油無法協助,環保署便函請交通部動員軍方人力和設備協助。但是,軍方船隻仍受限於海象而無法出海,僅能以人力在岸上協助搶救。另一方面,由船東所僱請的救難船,至2月3日總共抽取217.6公噸海上燃油。\r\n\r\n2月6日,環保署正式組成跨部會應變小組,協調內政部、交通部、國防部、海巡署、農委會、屏東縣政府、中油公司等單位,分別進行搶救工作的進度規劃和後續相關事宜。\r\n\r\n海岸油污撈除工作進行至2月16日,總計投入近一萬人次,撈除油污達四百六十二公噸。2月17日至3月24日進行油污清除工作,總計動員超過兩萬一千人次,清除油污513公噸。礁岩的除污及清洗工作,在3月25日至5月18日,以高壓水槍方式進行,投入人力接近三萬五千人次,清除油污達549公噸,清理的廢棄物超過三千五百公噸。[2][3]\r\n\r\n由船東負責的船上殘油抽取工作,分別在三月和五、六月進行,總計清除148.8公噸油污,至6月12日完成。清除工作未完成之前,屏東縣政府每日對船公司開出新台幣一百五十萬元的罰單,共計九十八日。\r\n\r\n油污清除及殘油抽取工作完成後,交通部於7月2日,在事故現場組成貨船移除小組,進行移除工作,清除船上礦砂,移至水深1000公尺處沉放,沉放工作於10月16日完成。然而,船體殘骸移除工作受限於颱風的連續侵襲而無法進行。",
            "geometry_scope": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            363772.44397377,
                            6475237.695186
                        ],
                        [
                            507130.09052262,
                            6586310.2519129
                        ],
                        [
                            594086.76482554,
                            6472829.928795
                        ],
                        [
                            555189.87205692,
                            6377675.3832085
                        ],
                        [
                            344825.61558758,
                            6327083.6251123
                        ],
                        [
                            306100.7061326,
                            6353253.7526714
                        ],
                        [
                            363772.44397377,
                            6475237.695186
                        ]
                    ]
                ]
            },
            "time": "2001-01-14T09:30:55+08:00"
        },
        {
            "id": 46,
            "name": "油污範圍",
            "description": null,
            "geometry_scope": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            363772.44397377,
                            6475237.695186
                        ],
                        [
                            507130.09052262,
                            6586310.2519129
                        ],
                        [
                            594086.76482554,
                            6472829.928795
                        ],
                        [
                            555189.87205692,
                            6377675.3832085
                        ],
                        [
                            344825.61558758,
                            6327083.6251123
                        ],
                        [
                            306100.7061326,
                            6353253.7526714
                        ],
                        [
                            363772.44397377,
                            6475237.695186
                        ]
                    ]
                ]
            },
            "time": "2001-01-14T00:00:00+08:00"
        },
        {
            "id": 47,
            "name": "油污範圍",
            "description": null,
            "geometry_scope": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            363772.44397377,
                            6475237.695186
                        ],
                        [
                            507130.09052262,
                            6586310.2519129
                        ],
                        [
                            594086.76482554,
                            6472829.928795
                        ],
                        [
                            555189.87205692,
                            6377675.3832085
                        ],
                        [
                            344825.61558758,
                            6327083.6251123
                        ],
                        [
                            306100.7061326,
                            6353253.7526714
                        ],
                        [
                            363772.44397377,
                            6475237.695186
                        ]
                    ]
                ]
            },
            "time": "2001-01-14T00:00:00+08:00"
        },
        {
            "id": 48,
            "name": "油污範圍",
            "description": null,
            "geometry_scope": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            363772.44397377,
                            6475237.695186
                        ],
                        [
                            507130.09052262,
                            6586310.2519129
                        ],
                        [
                            594086.76482554,
                            6472829.928795
                        ],
                        [
                            555189.87205692,
                            6377675.3832085
                        ],
                        [
                            344825.61558758,
                            6327083.6251123
                        ],
                        [
                            306100.7061326,
                            6353253.7526714
                        ],
                        [
                            363772.44397377,
                            6475237.695186
                        ]
                    ]
                ]
            },
            "time": "2001-01-14T00:00:00+08:00"
        }
    ]
}
@27pchrisl
Copy link
Contributor

Hi, can you confirm you said you had a success response but the relationship was not actually created? Could you provide some more info about your models and their relationships?

You might find some help on this in the unit tests, for example https://github.com/flat3/lodata/blob/5.x/tests/Navigation/Navigation.php#L216

@mikehsieh0305
Copy link
Author

This is Event Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Event extends Model
{
    public $timestamps = true;

    protected $guarded = [];

    // 子事件編號
    public function EventFeatures()
    {
        return $this->belongsToMany(EventFeature::class);
    }

    // 類別編號
    public function EventTypes()
    {
        return $this->belongsToMany(EventType::class);
    }

    // 權限編號
    public function EventPermissions()
    {
        return $this->belongsToMany(EventPermission::class);
    }
}

This is EventFeature Model

<?php

namespace App\Models;

use App\Casts\PointCast;
use App\Casts\PolygonCast;
use Illuminate\Database\Eloquent\Model;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
use Grimzy\LaravelMysqlSpatial\Types\Geometry;

class EventFeature extends Model
{
    use SpatialTrait;

    public $timestamps = true;
    protected $guarded = [];


    protected $casts = [
        'geometry_scope' => PolygonCast::class
    ];

    protected $spatialFields = [
        'geometry_scope'
    ];

    // 父事件編號
    public function Events()
    {
        return $this->belongsToMany(Event::class);
    }

    // 影像編號
    public function EventImages()
    {
        return $this->belongsToMany(EventImage::class);
    }

    public function setGeometryScopeGeojsonAttribute($value)
    {
        if (!empty($value))
            $this->attributes['geometry_scope'] = Geometry::fromJson($value);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants