I have an enum column in my table Schedule. I want to be update the column, but I cannot. My type consists of 3 options: common, template, revision.
This does NOT work. The process goes through fine, but the type does not change:
$schedule = Schedule::find($id);
$schedule->type = 'common';
$scehdule->save();
return $schedule;
But this DOES work:
$schedule = Schedule::find($id);
$schedule->fill(['type' => 'common']);
$scehdule->save();
return $schedule;
This also works:
$schedule = Schedule::find($id);
unset($schedule->type);
$schedule->type = 'common';
$scehdule->save();
return $schedule;
Problem is that I have validators that need to be satisfied if I were to use fill(). I only want to change one column, so I'd like to use the first approach.
Could someone advice?
I ran into the same problem with Laravel 5.3. When you are using an enum in the database you can set the enum by using an integer. 1 = common, 2 = template, 3 = revision.
$schedule = Schedule::find($id);
$schedule->type = 1;
$scehdule->save();
return $schedule;
This will set the schedule type to "common"
Related
Hello I am a beginner in using Codeigniter. What I want is to update a data in Codeigniter with an OR condition to the ID.
Here is my MySQL query that I want to make happen:
Update message_received SET is_read = 1 WHERE msg_id = 3 OR parent_id = 3
I tried something like this:
$this->query->update_array('message_received', array('msg_id' => $_POST['read_my_message'],'parent_id' => $_POST['read_my_message']) , array('is_read'=>1));
You can use or_where to apply OR condition in CodeIgniter, I've written a possible solution for your question(Reference).
See if it helps you.
$msg = $this->input->post('read_my_message'); // get the post data
$this->db->set('is_read', 1); // set the value of column
$this->db->where('msg_id', $msg); // first condition
$this->db->or_where('parent_id', $msg); // second contion
$this->db->update('message_received'); // table name
// Produces:
/* UPDATE `message_received` SET `is_read` = 1 WHERE `msg_id` = '$msg' OR `parent_id` = '$msg' */
You can apply OR. The references https://www.codeigniter.com/userguide3/database/query_builder.html
I am using a separate table to generate invoice numbers.
The code i am using is :
Branch::where('id', $branch_id)->update([
'piref' => DB::raw('piref+1')
]);
$piref = Branch::where('id', $branch_id)->get('piref');
$piref = $piref[0]['piref'];
My question is if the code is wrapped in a transaction. Is this approach safe ?
I tried to use Laravel Increments() but it does not return the newly generated number.
Model method increment() increments property of model and column value in database.
$branch = Branch::find($branch_id);
$branch->increment('piref');
$piref = $branch->piref;
I'm trying to merge multiple objects (like Receipts, Reports, etc) with Collection->merge().
This is the code I used:
$receipts = Receipt::all();
$reports = Report::all();
$collection = $receipts->merge($reports);
This is the result:
The above screenshot shows two elements, but the third element is missing because it has the same id (id: "1") as the first one. What I'm trying to achieve is to display all three of them as a collection.
EDIT:
I need the result to be objects (collection) because I also use the code on my view, where I check the class to determine what to display. Also, I use this function to sort the objects in the collection.
$collection->sort(function($a, $b)
{
$a = $a->created_at;
$b = $b->created_at;
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
});
I know that this is an old question, but I will still provide the answer just in case someone comes here from the search like I did.
If you try to merge two different eloquent collections into one and some objects happen to have the same id, one will overwrite the other. I dunno why it does that and if that's a bug or a feature - more research needed. To fix this just use push() method instead or rethink your approach to the problem to avoid that.
Example of a problem:
$cars = Car::all();
$bikes = Bike::all();
$vehicles = $cars->merge($bikes);
// if there is a car and a bike with the same id, one will overwrite the other
A possible solution:
$collection = collect();
$cars = Car::all();
$bikes = Bike::all();
foreach ($cars as $car)
$collection->push($car);
foreach ($bikes as $bike)
$collection->push($bike);
Source: https://medium.com/#tadaspaplauskas/quick-tip-laravel-eloquent-collections-merge-gotcha-moment-e2a56fc95889
I know i'm bumping a 4 years old thread but i came across this and none of the answers were what i was looking for; so, like #Tadas, i'll leave my answer for people who will come across this. After Looking at the laravel 5.5 documentation thoroughly i found that concat was the go-to method.
So, in the OP's case the correct solution would be:
$receipts = Receipt::all();
$reports = Report::all();
$collection = $receipts->concat($reports);
This way every element in the Report collection will be appended to every element in the Receipts collection, event if some fields are identical.
Eventually you could shuffle it to get a more visual appealing result for e.g. a view:
$collection->shuffle();
Another way to go about it is to convert one of your collections to a base collection with toBase() method. You can find it in Illuminate\Support\Collection
Method definition:
/**
* Get a base Support collection instance from this collection.
*
* #return \Illuminate\Support\Collection
*/
public function toBase()
{
return new self($this);
}
Usage:
$receipts = Receipt::all();
$reports = Report::all();
$collection = $receipts->toBase()->merge($reports);
You could put all collections in an array and use this. Depends on what you want to do with the collection.
$list = array();
$list = array_merge($list, Receipt::all()->toArray());
$list = array_merge($list, Report::all()->toArray());
I am using laravel 4. But I am facing problem with finding the difference between two date: one coming from database table and another one is current datetime. From the date difference I am expecting 1 hour or 1 day. I've tried few solution but can't fix this yet. And also I don't know the better way to solve it. If you guys have any solution, please provide me an example. Please tell me if I need any library. Here is my code:
$lecture_id = Input::get('lecture_id');
$delegate_id = Input::get('delegate_id');
// $newDate = new Datetime();
$lecture = Lecture::find($lecture_id);
// $lec_date = Date::forge($lecture->start_time);
// $lec_date = new Datetime($lecture->start_time);
$lec_date = $lecture->start_time->diffForHumans(Carbon::now());
if ( $lec_date > 1) {
LectureDelegate::create(array(
'lecture_id' => Input::get('lecture_id'),
'delegate_id'=> Input::get('delegate_id')
));
return Redirect::to('/')->with('message', 'Your are successfully apply to the lecture');
}
Should be:
$lec_date = Carbon::createFromTimeStamp( strtotime( $lecture->start_time ) )->diffForHumans();
or possibly:
$lec_date = $lecture->start_time->diffForHumans();
If you add this to your Lecture.php model:
public function getDates()
{
return array('created_at', 'updated_at', 'deleted_at', 'start_time');
}
From the documentation:
By default, Eloquent will convert the created_at, updated_at, and
deleted_at columns to instances of Carbon...
You may customize which fields are automatically mutated, and even
completely disable this mutation, by overriding the getDates method of
the model.
As for diffForHumans the documentation states:
The lone argument for the function is the other Carbon instance
to diff against, and of course it defaults to now() if not specified.
update
If the timestamp from the database being passed to diffForHumans is in the future, Carbon automatically makes the return like:
When comparing a value in the future to default now:
1 hour from now
5 months from now
When comparing a value in the past to another value:
1 hour before
5 months before
I'm having an issue with CI Active Record's "Where Not In". I am trying to exclude a series of ID's. I couldn't understand why everything worked fine and dandy with one record, but not with multiple.
My Query
$this->db->where_not_in('crm.user_id', $ignore);
The problem is when I profile the the Query is wrong.
With a string of ID's
// $ignore = "12,13";
SELECT *
FROM (`crm`)
WHERE `crm`.`user_id` NOT IN ('16,13')
AND `survey` = 1
With a string of Quotes ID's
// $ignore = "'12','13'";
SELECT *
FROM (`crm`)
WHERE `crm`.`user_id` NOT IN ('\'16\',\'13\'')
AND `survey` = 1
Am I forced to do a loop of "or_where_not_in" or something like that?
where_in and where_not_in expect you to pass an array, not a string as the 2nd parameter.
$ignore = array(12, 13);
$this->db->where_not_in('crm.user_id', $ignore);
Link to the docs: http://www.codeigniter.com/userguide2/database/active_record.html