skip to Main Content

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:

"IlluminateDatabaseGrammar::parameterize(): Argument #1 ($values) must be of type array, string given, called in C:xampphtdocsxtimsvendorlaravelframeworksrcIlluminateDatabaseQueryGrammarsGrammar.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 [ // appHttpControllersApiAssetSurveyController.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" => "[email protected]"
  ]
  "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"
]

2

Answers


  1. 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);
        }
    }
    
    Login or Signup to reply.
  2. Do yourself a favor and define a proper relationship between your models, It will make your life much easier.

    If I’m not mistaken based on your description, Your relationship should be one-to-one

    So you AssetSurveyDecision model should have a relationship like this

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

    then your AssetSurvey model should have a relationship like this

    public function decision() {
        return $this->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->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')
        ]);
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search