Laravel 5.2 updated table now model wont work - php

I updated my notes table with 4 new columns: classification, pod, sampled, followup.
I would update my model like so (which still works like this):
$note->account_id = $account->id;
$note->name = $request->input('account-note-title');
$note->description = $request->input('account-note-description');
$note->save();
But if I try and post to the new columns it fails.
$note->account_id = $account->id;
$note->name = $request->input('account-note-title');
$note->description = $request->input('account-note-description');
$note->classification = $request->input('account-note-classification');
$note->pod = $request->input('account-note-pod');
$note->sampled = $request->input('account-note-samplebrand');
$note->followup = $request->input('account-note-followup-date');
$note->save();
Do I have to refresh the model or anything?

You can create new value using laravel Create methode like below
ModelName::create($request->all())
In this case you have to put your all input field in the $fillable variable of MODEL.
You can update item using the following method
$model = findOrFail($id)
$model->update($request->all())

Related

Update a Relationship Model with one Line - Laravel

I have a massive edit page and the values are taken from 4 tables. I have them all in a object and sorted which ones go where. Although when I call my main model, can I update its relationship by passing in the object or array?
Main Model
$officiant = Officiant::find($id);
Its relationship is the "detail" and "profile"
Example:
$officiant->fname = $request->fname;
$officiant->lname = $request->lname;
$officiant->phone = $request->phone;
$profile = new \StdClass();
$profile->gender = $request->gender;
$profile->profile = $request->profile;
$detail = new \StdClass();
$detail->street = $request->street;
$detail->city = $request->city;
I can update officiant by passing it like this
$officiant->update(array ($officiant));
Although, can I update the detail one by doing something similar, as in
$officiant->detail->update(array ($detail));
$officiant->profileupdate(array ($profile));
If you are updating via a relation ship it should be
$officiant->detail()->update(array ($detail));
Using ->detail() instead of ->detail sends a query builder instance that can be chained with update().

Update data into table from dynamically created input field

I have 2 models Tour.php
public function Itinerary()
{
return $this->hasMany('App\Itinerary', 'tour_id');
}
and
Itinerary.php
public function tour()
{
return $this->belongsTo('App\Tour', 'tour_id');
}
tours table:
id|title|content
itineraries table:
id|tour_id|day|itinerary
I have used vue js to create or add and remove input field for day and plan dynamically. And used the following code in tour.store method to insert into itineraries table:
$count = count($request->input('day'));
$temp_day = $request->input('day');
$temp_itinerary = $request->input('itinerary');
for($i = 0; $i < $count; ++$i)
{
$itinerary = new Itinerary;
$itinerary->tour_id = $tour->id;
$itinerary->plan = $temp_itinerary[$i];
$itinerary->day = $temp_day[$i];
$itinerary->save();
}
And was successful in inserting the records.And applied same code in tour.store method. Instead of updating the rows, it inserted new rows to the table. What would be the best solution for this ?
For updation try this code
$itinerary = Itinerary::find($tour_id);
$itinerary->plan = $temp_itinerary[$i];
$itinerary->day = $temp_day[$i];
$itinerary->save();
The way you are using is to insert/create new records. To update you can use.
Itinerary::find($tour_id)->update(
['column_name'=> value]
);
Where find method takes a primary key of the table.
This will update your existing record. You can update as many columns as you want just pass in array update takes. You can also use save method as mentioned in other answer.
Check Laravel Update Eloquent
EDIT
$iterneary = Itenerary::where('tour_id', $tour_id)->first();
Now you can update this iterneary object to whatever you want.
this is how i did it. First saved all the tours in $tours[] array.
foreach($tours as $tour) {
$itinerary->tour()->updateOrCreate(['id'=> $tour['id']],$tour);
}
updateOrCreate because you may need to add new tours while updating. I know this doesnt answer your issue exactly but this could atleast give you an idea.

phalcon 2.0.13 set data with magic setter to related model

I have a problem with phalcon model magic getter and setter.
I want to update like this tutorial :
https://docs.phalconphp.com/en/latest/reference/models.html#storing-related-records
But the thing is my proj is multi module and separated models folder.
So I have to use alias for hasOne and belongsTo
$this->hasOne('user_id', '\Models\UserProfile', 'user_id', array('alias' => 'UserProfile'));
and
$this->belongsTo('user_id', '\Models\CoreUser', 'user_id', array('alias' => 'CoreUser'));
What i want to do is like this.
$CoreUser = new CoreUser();
$user = $CoreUser->findFirst(array(
//...condition here to find the row i want to update
));
$user->assign($newUserData);
$user->setUserProfile($newProfileData);
$user->update();
But above this code only save user data and don't save Profile data at all. (have profile data -- confirmed)
So do you have any idea what the error is? if u know, Please help me or give me a tip.
I got it now.. when assigning like $user->UserProfile = $newUserProfile;
$newUserProfile should b a Model Object.
So my new code is
$CoreUser = new CoreUser();
$user = $CoreUser->findFirst(array(
//...condition here to find the row i want to update
));
$profile = $user->UserProfile; //$profile is now model object which related to $user
//assign new array data
$profile->assign($newProfileData);
$user->assign($newUserData);
/*
* can also assign one by one like
* $user->first_name = $newProfileData['first_name'];
* but cannot be like $profile = $newProfileData or $user->UserProfile = $newProfile
* since it's gonna override it the model with array
*/
$user->UserProfile = $profile;
$user->update(); // it's working now
Thanks to #Timothy for the tips too .. :)
Instead of doing
$profile = $user->UserProfile;
You should instantiate a new UserProfile object
// find your existing user and assign updated data
$user = CoreUser::findFirst(array('your-conditions'));
$user->assign($newUserData);
// instantiate a new profile and assign its data
$profile = new UserProfile();
$profile->assign($newProfileData);
// assign profile object to your user
$user->UserProfile = $profile;
// update and create your two objects
$user->save();
Note that this will always create a new UserProfile. If you want to use the same code to update and create a UserProfile, you can maybe do something like:
// ...
// instantiate a (new) profile and assign its data
$profile = UserProfile::findFirstByUserId($user->getUserId());
if (!$profile) {
$profile = new UserProfile();
}
$profile->assign($newProfileData);
// ...

Laravel eloquent firstOrNew method doesn't update or insert

I have a problem with firstOrNew() method of the Laravel eloquent class. the problem is that when I give the method some attributes and values and call save() on the model, it doesn't get saved (inserted or updated).
PHP :
<?
$flightModel = Flight::firstOrNew([
'arrival' => $flightData['arrival'],
'plane' => $flightData['plane']
]);
$flightModel->departure = $flightData['departure'];
$flightModel->save();//this doesn't save any data
But when I try this with a new model it gets saved :
<?
$flightModel = new Flight();
$flightModel->arrival = $flightData['arrival'];
$flightModel->plane = $flightData['plane'];
$flightModel->departure = $flightData['departure'];
$flightModel->save();
Thanks for helping.

Laravel 5: how to insert data in database and return field back

Sorry if it's a stupid question, but I am new in framework programming, and also in Laravel. I want to insert my data in a database and return id back, but in the database the field in the table is not id, but owner_id. How i can return it back? I'm using PostgreSQL and set it auto-incrementing ready.
This is my code:
$regis = new ltd_owner;
$regis->owner_name = $register['firstname'] . ' ' . $register['lastname'];
$regis->represent_company_id = $register['isRepresent'];
$regis->gender = $register['sex'];
$regis->address_id = '7598';
$regis->nationality_id = '24';
$regis->save();
Please help me to fix it, thanks
You need to set a different primary key in your Model:
protected $primaryKey = "owner_id";
Then, after you do the save(), the field should be populated automatically, and you can access it:
var_dump($regis->owner_id);
If you use different id name, you should define it in the Model.
Add the following line to the ltd_owner model:
protected $primaryKey = 'owner_id';
Then if you create a new model, you can get back the id.
$regis = new ltd_owner;
$regis->owner_name = $register['firstname'] . ' ' . $register['lastname'];
$regis->represent_company_id = $register['isRepresent'];
$regis->gender = $register['sex'];
$regis->address_id = '7598';
$regis->nationality_id = '24';
$regis->save();
//inserted model id:
$regis->owner_id;
After passing $regis->save();
$regis will hold your new data.
You can get those variable by calling them $regis->column_name for example $regis->owner_id;, $regis->address_id; $regis->gender
Hope you understand what's happening

Categories