Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel save() method returning true but not updating records

I am using Eloquent to update my table Opportunity,

Opportunity Model

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Opportunity extends Model {

protected $primaryKey = 'OpportunityID';

protected $table = 'opportunitys';
// relationships
public function csfs()
{
    return $this->hasMany('App\Csf', 'opportunityID');
}

public function benefits()
{
    return $this->hasMany('App\Benefit', 'opportunityID');
}

public function risks()
{
    return $this->hasMany('App\Risk', 'opportunityID');
}

public function projects()
{
    return $this->belongsTo('App\Project', 'projectID');
}

public static function createNewOpportunity($input, $projectID)
{
    $opportunity = new Opportunity;
    $opportunity->value = $input['value'];
    $opportunity->margin = $input['margin'];
    $opportunity->duration = $input['duration'];
    $opportunity->tender_type = $input['tender_type'];
    $opportunity->likelihood_of_success = $input['likelihood_of_success'];
    $opportunity->scope_of_work = $input['scope_of_work'];
    $opportunity->deliverables = $input['deliverables'];
    $opportunity->projectID = $projectID;
    $opportunity->high_level_background = $input['high_level_background'];
    if($opportunity->save())
        {
            Opportunity::leadSalesOppComplete($projectID);
            return true;
        };

}

public static function leadSalesOppComplete($projectID)
{
    $task = Lead::where('projectID', '=', $projectID)->first();
    $task->sales_opp = true;
    return $task->save();
}

}

public function updateOpportunity(Request $request, $id) {

I get the id and find the opportunity.

$something = Opportunity::find($id);

I have died and dumped this and I get this

Opportunity {#259 ▼
 #primaryKey: "OpportunityID"
#table: "opportunitys"
#connection: null
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:12 [▼
"opportunityID" => 11
"value" => 0
"margin" => 0
"tender_type" => ""
"likelihood_of_success" => 0
"high_level_background" => ""
"scope_of_work" => ""
"deliverables" => ""
"duration" => ""
"projectID" => 6
"created_at" => "2015-03-11 17:45:47"
"updated_at" => "2015-03-11 17:45:47"
 ]
  #original: array:12 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}

Which is correct. I then update these with

    $something->margin = $request['margin'];
    $something->duration = $request['duration'];
    $something->tender_type = $request['tender_type'];
    $something->likelihood_of_success = $request['likelihood_of_success'];
    $something->scope_of_work = $request['scope_of_work'];
    $something->deliverables = $request['deliverables'];
    $something->high_level_background = $request['high_level_background'];

Now if I die and dump I get

Opportunity {#259 ▼
#primaryKey: "OpportunityID"
#table: "opportunitys"
#connection: null
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:12 [▼
"opportunityID" => 11
"value" => "25000"
"margin" => "0"
"tender_type" => "Proposal"
"likelihood_of_success" => "0"
"high_level_background" => ""
"scope_of_work" => ""
"deliverables" => ""
"duration" => ""
"projectID" => 6
"created_at" => "2015-03-11 17:45:47"
"updated_at" => "2015-03-11 17:45:47"
]
#original: array:12 [▼
  "opportunityID" => 11
  "value" => 0
  "margin" => 0
  "tender_type" => ""
  "likelihood_of_success" => 0
  "high_level_background" => ""
  "scope_of_work" => ""
  "deliverables" => ""
  "duration" => ""
  "projectID" => 6
  "created_at" => "2015-03-11 17:45:47"
  "updated_at" => "2015-03-11 17:45:47"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}

I only changed the value which shows the change.

I now run

$something->save();

It returns true when I die and dump it.

But no records are changed in the database.

Any ideas?

two images from tinker

Using Tinker to update

Using Tinker to update part 2

Var_dump after save

like image 524
Chris Townsend Avatar asked Mar 12 '15 10:03

Chris Townsend


People also ask

What is Save () in laravel?

In short save() method is used both for saving new model, and updating existing one. here you are creating new model or find existing one, setting its properties one by one and finally saves in database, while in create method you are passing array, setting properties in model and persists in database in one shot.

Which method is used to update data in laravel?

We can update the records using the DB facade with update method. The syntax of update method is as shown in the following table. Run an update statement against the database.

What is the difference between save and create in laravel?

What is difference between create and save in Laravel? Save can be used to both create a new Record and update a existing record . Whereas create is used to create a new record by providing all required field at one time .

How do you check data is inserted or not in laravel?

Check if query got executed $newUser = User::create([ 'username' => Input::get('username'), 'email' => Input::get('email') ]); //Check if user was created if ( ! $newUser) { App::abort(500, 'Some Error'); } //User was created show OK message return Response::json(array('success' => true, 'user_created' => 1), 200);


1 Answers

This line in the Opportunity model fixed the issue.

Protected $primaryKey = "opportunityID";

Although it is difficult to understand why it was still possible to retrieve the data and create a new record.

like image 194
Chris Townsend Avatar answered Oct 04 '22 21:10

Chris Townsend