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
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.
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 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 .
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With