When I update my row with the query below, it changes not only the updated_at column for the row, but also the created_at column. Why? How can I prevent this so that it only changes the updated_at column
Post::where('id', Input::get('post_id'))
->where('user_id', getUserID())
->update(array('message' => Input::get('message')));
From Laravel documentation at
Insert-update-delete
You can use following method to perform update
$x = Post::find(Input::get('post_id')); // may need to use intval()
$x->message = Input::get('message');
$x->save();
This should update the record with only updating 'updated_at' timestamp.
Related
Please someone help me.How can I fetch a single column value from database and then update this column value?
let's say you want to update the name of the id 1 from the names table .Here Name is the name of model and 'name' is the column inside it. You can use pluck method to fetch a particular column from a table.
$user = Name::where('id' , $id)->pluck('name');
$previous_name = $user->name;
$user->name = 'New Value';
$user->save();
Retrieve using Query Builder:
DB::table('table_name')->select('name');
Update:
Model::where('id', $id)->update(array('field_name' => 'value'));
I have jobs table in the database with the default columns created_at and updated_at.
I want to return the number of jobs created and the number updated.
However as the updated_at column is populated at the point of creation, I want to exclude any jobs where the updated_at value is the same as the created_at value (in order to get just the jobs that have been updated since being created).
I use the following code to get the number of updated jobs between a time range.
$jobsUpdated = Job::where('id', $id)
->whereBetween('updated_at', [$value['fromDate'], $value['tillDate']])
->count();
How can I add another where clause to exclude those results which have the same value as created_at.
In theory something like:
->andWhere('updated_at', '!=' , 'created_at')
But andWhere does not exist, so this is just to show my thinking.
Thanks!
$jobsUpdated = Job::where('id', $id)
->whereBetween('updated_at', [$value['fromDate'], $value['tillDate']])
->whereColumn('updated_at', '!=' , 'created_at')
->count();
to compare column value use whereColumn() function referenced by official doc
I'm using Laravel Query Builder to get the last updated record from the database. However, it displays a different id with a different update_date. The update_date is correct, and it shows me last updated_date, but every time I'm getting the first id.
$results = DB::table('customer_info as cust_info')
->join('client_emp as client_info', 'cust_info.id', '=', 'client_info.cust_id')
->select('cust_info.id', DB::raw('MAX(cust_info.updated_at)'))
->orderBy('cust_info.id','DESC')
->first();
In the above query, I write select to get the id of last updated record.
->select('cust_info.id', DB::raw('MAX(cust_info.updated_at)'))
However, it shows me the last updated_date only with the first id from the table every time.
Try this:
orderBy('updated_at','DESC')->first()->id
Here you are getting the id of the latest updated record. When you remove the first() method, you will get the whole collection sorted by 'updated_at'
I have the following 2 tables:
Cars
----
id | created_at
and
Repaired
id | cars_id | updated_at
I want to display all cars, ordered by created_at desc, BUT also ordered by updated_at from the Repaired pivot table. Repaired can have many entries of specific cars_id.
So if carA is created_at 2017-02-20, and carB is created_at 2016-01-10, but the updated_at in Repaired for carB is on 2017-02-22, i want to display carB BEFORE carA, because the relation is "newer".
I also don't want do load the data from the relation table.
I tried the follwing:
$cars = $model->load(['repaired' => function($query) {
return $query->orderBy('updated_at', 'desc');
}])->orderBy('created_at', 'desc');
and
$model->join('repaired', 'repaired.cars_id', '=', 'cars.id')
->orderBy('repaired.updated_at', 'desc')
->orderBy('cars.created_at', 'desc')
->select('cars.*');
^ this query return all items from the Repaired table, even if the updated_at field is NULL. And i can't group them.
You can use the touch parent timestamps feature in Eloquent, to update the updated_at for the car, every time Repaired is updated.
In your Repaired model just add this line:
protected $touches = ['car'];
This is assuming your relation method is named car() of course.
Then you can do this:
Cars::orderBy('updated_at');
Every time a row in the Repaired table is added or updated, the car specified in the car relation will have its updated_at set to the same time. That way you can order by it.
$popular_posts = Post::withCount('favorite_to_users')
->orderBy('view_count', 'desc')
->orderBy('favorite_to_users_count', 'desc')
->take(5)->get();
I'm having some trouble with my pivot table. I've recognized too late, that it is possible, that some pivot rows doesn't have unique values in my project, means I've to add an auto_increment ID field to my pivot table.
This is my structure:
Order.php
public function items()
{
return $this->belongsToMany('Item', 'orders_items', 'order_id', 'item_id')->withPivot(['single_price', 'hours', 'prov', 'nanny_id', 'vat', 'vat_perc', 'invoice_id','id']);
}
orders_items
id, order_id, item_id, nanny_id, hours
I've conntected Orders and Items through a pivot table ('orders_items)'. It is possible, that one order has 2 or more same items in the pivot table. So I've to add an unique ID to identify and update them.
Now I try to update a pivot row. Problem is, if I have 2 or more items, he updates them all, not only one. This is my update command:
$order = Order::find($orderId);
$items = $order->items()->whereNull('nanny_id');
$free_item = $items->first();
$free_item->pivot->nanny_id = 123;
$free_item->pivot->save();
With this command, he updates all pivot rows from the order. I know the problem: Laravel uses here the wrong identifiers (it uses order_id and item_id as defined in my belongsToMany relationship - and they aren't unique). For example, Laravel tries to execute this code on save():
UPDATE orders_items SET [...] WHERE order_id = 123 AND item_id = 2;
I want, that Laravel changes the query to this one:
UPDATE orders_items SET [...] WHERE order_id = 123 AND item_id = 2 AND id = 45;
// Edit
Okay, this solution works:
$free_item->pivot->where('id',$free_item->pivot->id)->update('nanny_id',123);
But is there an easier way, f.e. adding a custom pivot model that adds the id automatically to save() and update() methods?