Laravel的save() / create()函数未存储来自表单的请求数据。

huangapple go评论65阅读模式
英文:

Laravel save() / create() function not storing requested data from the form

问题

这是您提供的代码的翻译部分:

public function updateDecision(AssetSurvey $survey, Request $request){
        $surveyId = $request->id;
        $survey = $survey->newModelQuery()->find($surveyId);
        $surveyDecisionId = $survey->decision_id;

        if($surveyDecisionId){
            $decision = AssetSurveyDecision::where('id', $surveyDecisionId)->first();
        }
        else{
            $decision = new AssetSurveyDecision();
        }

        $decisionData = [
            'asset_decision' =>  $request->input('asset_decision'),
            'rejection_remarks' => $request->input('rejection_remarks'),
            'rejected_by' => $request->input('rejected_by'),
            'rejection_date' => $request->input('rejection_date'),
            'retraction_remarks' => $request->input('retraction_remarks'),
            'retracted_by' => $request->input('retracted_by'),
            'retraction_date' => $request->input('retraction_date')
        ];

        if($surveyDecisionId){
            $decision->update($decisionData);
        }
        else{
            $decision->save($decisionData);
        }
        
        if($decision){
            if($survey){
                $data = [
                    'decision_id' => $decision->id,
                ];
                $survey->update($data);
            }
            return response()->json($survey);
        }
    }

希望这对您有所帮助。如果您有任何其他问题,请随时提出。

英文:

I have a function that will first store data in a table called 'decision' and the id will be stored in another table called 'survey'. It will first check if the id already existed in the survey table and if it does, the update() method is used however if it doesn't it will use save() function.

When I first tried saving the data, it will only create a new data in 'decision' table with only its id, which then be stored in the 'survey' table but the other requested data are not stored. However when I tried updating it, all other data will only then stored in the 'decision' table.

I think the issue is in the way I implemented save() method but I'm not quite sure. I have also tried using create() method but it returns me an error as shown below:

>"Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, string given, called in C:\xampp\htdocs\xtims\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 1022"

Here is my code. Any advice?

public function updateDecision(AssetSurvey $survey, Request $request){
        $surveyId = $request->id;
        $survey = $survey->newModelQuery()->find($surveyId);
        $surveyDecisionId = $survey->decision_id;

        if($surveyDecisionId){
            $decision = AssetSurveyDecision::where('id', $surveyDecisionId)->first();
        }
        else{
            $decision = new AssetSurveyDecision();
        }

        $decisionData = [
            'asset_decision' =>  $request->input('asset_decision'),
            'rejection_remarks' => $request->input('rejection_remarks'),
            'rejected_by' => $request->input('rejected_by'),
            'rejection_date' => $request->input('rejection_date'),
            'retraction_remarks' => $request->input('retraction_remarks'),
            'retracted_by' => $request->input('retracted_by'),
            'retraction_date' => $request->input('retraction_date')
        ];

        if($surveyDecisionId){
            $decision->update($decisionData);
        }
        else{
            $decision->save($decisionData);
        }
        
        if($decision){
            if($survey){
                $data = [
                    'decision_id' => $decision->id,
                ];
                $survey->update($data);
            }
            return response()->json($survey);
        }
    }

This is the dd() result:

array:23 [ // app\Http\Controllers\Api\Asset\SurveyController.php:587
  "id" => "26"
  "organization_id" => "1"
  "site_id" => "2"
  "site_name" => "reaaw"
  "client" => array:6 [
    "id" => "2"
    "uuid" => "404b329c-4bce-4785-8d2f-a64d91826916"
    "code" => "DEISD"
    "description" => "Digi Telecommunications Sdn. Bhd."
    "created_at" => "2022-11-14T02:59:14.000000Z"
    "updated_at" => "2022-11-14T02:59:14.000000Z"
  ]
  "primary_client_site_id" => "testttttt"
  "pic" => array:3 [
    "name" => "test"
    "phone" => "0145345345"
    "email" => "test@test.com"
  ]
  "coordinate" => array:2 [
    "longitude" => "14.234234"
    "latitude" => "11.34323"
  ]
  "telco_request_date" => "2023-04-12"
  "tentative_telco_rental_amount" => "231"
  "tentative_telco_rental_rate" => array:2 [
    "description" => "Quarterly"
    "id" => "2"
  ]
  "current_progress_status" => "1"
  "asset_decision" => array:2 [
    "description" => "Rejected by Management"
    "id" => "2"
  ]
  "rejection_remarks" => "test"
  "rejection_date" => "2023-04-15"
  "rejected_by" => "test"
  "isDirty" => "1"
  "hasErrors" => "0"
  "processing" => "0"
  "wasSuccessful" => "0"
  "recentlySuccessful" => "0"
  "__rememberable" => "1"
  "organization_name" => "XPERTS-HQ"
]

答案1

得分: 1

你之所以收到该错误是因为$request->input('asset_decision')是一个数组。

您没有将存储的数据传递给save方法。您可以使用save方法进行创建和更新。

我还认为您需要更改updateDecision方法中参数的顺序。$request应该是第一个参数。

public function updateDecision(Request $request, AssetSurvey $survey){

    $decision = AssetSurveyDecision::where('id', $survey->decision_id)->firstOrNew();
    $decision->asset_decision = $request->input('asset_decision')['id'];
    $decision->rejection_remarks = $request->input('rejection_remarks');
    $decision->rejected_by = $request->input('rejected_by');
    $decision->rejection_date = $request->input('rejection_date');
    $decision->retraction_remarks = $request->input('retraction_remarks');
    $decision->retracted_by = $request->input('retracted_by');
    $decision->retraction_date = $request->input('retraction_date');

    $decision->save();

    if ($survey->decision_id != $decision->id){
        $survey->decision_id = $decision->id;
        $survey->save();
        
        return response()->json($survey);
    }
}
英文:

You get that error because $request->input('asset_decision') is an array.

You don't pass stored data to save method. You can use save method for creating and updating.

And I also believe that you need to change the order of argument in the updateDecision method. $request should be the first argument instead

public function updateDecision(Request $request, AssetSurvey $survey){

    $decision = AssetSurveyDecision::where('id', $survey->decision_id)->firstOrNew();
    $decision->asset_decision = $request->input('asset_decision')['id'];
    $decision->rejection_remarks = $request->input('rejection_remarks');
    $decision->rejected_by = $request->input('rejected_by');
    $decision->rejection_date = $request->input('rejection_date');
    $decision->retraction_remarks = $request->input('retraction_remarks');
    $decision->retracted_by = $request->input('retracted_by');
    $decision->retraction_date = $request->input('retraction_date');

    $decision->save();

    if ($survey->decision_id != $decision->id){
        $survey->decision_id = $decision->id;
        $survey->save();
        
        return response()->json($survey);
    }
}

答案2

得分: 1

以下是翻译好的内容:

"自己做个好处,定义你的模型之间合适的关系,这将使你的生活变得更容易。

如果我没有理解错你的描述,你的关系应该是[一对一][1]。

所以你的 AssetSurveyDecision 模型应该有如下关系:

public function asset() {
    return $this->hasOne(AssetSurvey::class);
}

然后你的 AssetSurvey 模型应该有如下关系:

public function decision() {
    return $this->hasOne(AssetSurveyDecision::class); 
}

然后你只需要在其相关的模型上使用 updateOrCreate

public function updateDecision(AssetSurvey $survey, Request $request) {

    return $survey->decision()->updateOrCreate(['id' => $survey->decision_id], [
        'asset_decision' =>  request('asset_decision'),
        'rejection_remarks' => request('rejection_remarks'),
        'rejected_by' => request('rejected_by'),
        'rejection_date' => request('rejection_date'),
        'retraction_remarks' => request('retraction_remarks'),
        'retracted_by' => request('retracted_by'),
        'retraction_date' => request('retraction_date')
    ]);

}

[1]: https://laravel.com/docs/10.x/eloquent-relationships#one-to-one
"


<details>
<summary>英文:</summary>

Do yourself a favor and define a proper relationship between your models, It will make your life much easier.

If I&#39;m not mistaken based on your description, Your relationship should be [one-to-one][1]

So you `AssetSurveyDecision` model should have a relationship like this 

    public function asset() {
        return $this-&gt;hasOne(AssetSurvey::class);
    }

then your `AssetSurvey` model should have a relationship like this 

    public function decision() {
        return $this-&gt;hasOne(AssetSurveyDecision::class); 
    }

Then all you have to do is use `updateOrCreate` on its related model

    public function updateDecision(AssetSurvey $survey, Request $request) {
    
        return $survey-&gt;decision()-&gt;updateOrCreate([&#39;id&#39; =&gt; $survey-&gt;decision_id], [
            &#39;asset_decision&#39; =&gt;  request(&#39;asset_decision&#39;),
            &#39;rejection_remarks&#39; =&gt; request(&#39;rejection_remarks&#39;),
            &#39;rejected_by&#39; =&gt; request(&#39;rejected_by&#39;),
            &#39;rejection_date&#39; =&gt; request(&#39;rejection_date&#39;),
            &#39;retraction_remarks&#39; =&gt; request(&#39;retraction_remarks&#39;),
            &#39;retracted_by&#39; =&gt; request(&#39;retracted_by&#39;),
            &#39;retraction_date&#39; =&gt; request(&#39;retraction_date&#39;)
        ]);
    
    }

  [1]: https://laravel.com/docs/10.x/eloquent-relationships#one-to-one

</details>



huangapple
  • 本文由 发表于 2023年4月13日 23:09:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定