PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 15, 2022

[FIXED] How to return an updated data after updating in Laravel Model?

 November 15, 2022     eloquent, laravel, laravel-5, laravel-6, php     No comments   

Issue

I have a model Driver which have columns: name, branch, status_id, etc..Updating is actually fine and working, my problem is how can I return the updated one?

Here's what I tried so far, but it returns a boolean, resulting of returning an error in my console:

The Response content must be a string or object implementing __toString(), "boolean" given.

public function updateStatus(Driver $driver)
{
    return $driver->update($this->validateStatus());
}

public function validateStatus()
{
    return $this->validate(request(), [
        'status_id' => 'required|min:1|max:3'
    ]);
}

I expect it should return the all the columns of a driver.

I've been to this link but it doesn't helped. Someone knows how to do this?


Solution

return as object instead of boolean type

public function updateStatus(Driver $driver)
{
   $driver->update($this->validateStatus());
   return $driver;// first way
   // return tap($driver)->update($this->validateStatus()); //second way
}

public function validateStatus()
{
    return $this->validate(request(), [
        'status_id' => 'required|min:1|max:3'
    ]);
}


Answered By - Jignesh Joisar
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing