I have multiple entries corresponding to a single attribute. I need to retrieve the last inserted record for a particular id. I tried like this,
$this->db->select('*');
$this->db->from('tbl_appverification');
$this->db->where('app_no',$appno);
$query = $this->db->get();
return $query_result();
but i get all the values where app_no='5665'
it's $query->result()
if you want the last inserted value you should have a column in the table with the timestamp, or order by an auto incremented id (desc) and pick the first record.
for example:
$this->db->order_by('id', 'desc');
or
$this->db->order_by('created', 'desc');
add column in database date_inserted
and add this code
$this->db->order_by('date_inserted','desc/asc');
$this->db->limit(1);
Related
I filled a column in my database with some information, but is filling all records with all elements in each space of the column, and I would like that each element take a place in the column forming a kind of list. This is the query that I'm using.
$data = DB::table('incidencias')
->join('clientes_incid', 'clientes_incid.rif', '=', 'incidencias.rif')
->select('incidencias.rif')
->distinct()
->get();
DB::table('incidencias')->update(['resultado' => $data]);
I want to store each element of the array in a diferent row but in the same column
Can somebody help me?
I think you want every index in $data should be save as new record in database. To do that you can do this using loop.
Here edited
foreach ($data as $val) {
$exist=DB::table('incidencias')->where('resultado', $val)->count();
if($exist ==0){
DB::table('incidencias')->insert(['resultado' => $val]);
}
}
As in you query You are using update. To update you need to specify which record you want to update? You should provide some key to find record and update that. In your query you are geting record and updating all records.
I need to make duplicate of a column, but also I don't want to delete the old one. What would be the right way to copy value from one column into another? I have tried something like this, but it's not working:
Users::update([
'from_status' => 'status'
]);
I have to mention that, using MySQL Workbench I have already added new column into table (it's column from_status).
Also is it possible to update multiple columns at once this way? Column status is not the only one I am trying to duplicate.
There are several ways to accomplish this task. And one of the simplest and the most straight way to do this is like following:
$users = User::all();
foreach($users as $user){
$user->from_status = $user->status;
$user->save();
}
Also is it possible to update multiple columns at once this way?
Yes. The same way by adding one more line for each column:
$users = User::all();
foreach($users as $user){
$user->from_status = $user->status;
$user->column2 = $user->some_column;
$user->save();
}
I have two database tables items and measurement_units - item has measurement unit.
Now the problem is I want to select a particular column from items and some column from measurement_unit. I want to use Eager loading
e.g.
$items_with_mu = Item::with("measurement_unit")->select(["item_name", "item_stock"])->first();
When accessing measurement_unit. It returns null. Without the select function it returns data(measurement_unit).
$items_with_mu->measurement_unit;
can anyone help me and sorry for my English.
Try this
Item::with(['measurement_unit' => function($q) {
$q->select('id','unit_column'); //specified measurement_unit column
}])
->select('id','measurement_unit_id','item_name')
->get();
If your laravel version is >=5.5 then you can write in a single line
Item::with('measurement_unit:id,unit_column')
->select('id','measurement_unit_id','item_name')
->get()
You have to select the primary column of the main model like below.
items_with_mu = Item::with("measurement_unit")->select(["item_name", "item_stock", "primary_key"])->first();
I am having 2 tables, users and profiledetails
I am able to run Join query and access the data and send to view.
But when I am manipulation the field 'dob' (date format) in profiledetails table. No Success, Please check the code below ,
webpagesConroller:
$users = DB::table('users')
->join('profiledetails', 'users.id', '=', 'profiledetails.user_id')
->select('users.*', 'profiledetails.dob')
->get();
$age = $users->dob->diffInYears(Carbon::now());
return view('webpages.index',compact('users'))->with($age);
View:
<li class="cate_head">Age : {{ $age}}</li>
Error:
Trying to get property of non-object
I have model Profiledetails added the mutators as below,
public function getAge(){
return $this->dob->diffInYears(Carbon::now());
}
public function getDOB(){
return $this->dob->format('d-m-Y');
}
Can I not use this method on another controller for Ex- webpagesController, If yes How.
Since you are using a raw query, the data returned is not an object but an array with the data.
Also you did not limit the query, meaning it could return multiple rows.
You'll probably need to get the data from the $users as:
//with the possibily of multiple rows. Index is the row which has to be used
$users[index]['dob']
//when you are sure it only returns one row
$users[0]['dob'] // or $users['dob'] not sure as of the moment
You want to check the differences with Carbon.
So you will probably need to make a Carbon object of the dob result and check it against the Carbon::now.
$age = Carbon::createFromFormat('Y-m-d', $users[0]['dob'])->diffForHumans();
// diffForHumans() uses the local time
Note that this only gets the age for one row (the index or the first because of [0]).
If you want to do it for every row use a for or foreach where you set the $users->age for every $user. But as these are not object/models but raw data, this will not work.
Hope this helps your question a bit
I have a model download which has two three properties. Those properties are 'id' , 'user_id' , 'file_id'.
Whenever a user downloads a file a download record is created.
I want to grab all downloads that have both a unique file_id and unique user_id.
How is this accomplished.
A collection might come from a relationship and needs filtering afterward. Below is a tested working solution in laravel 5.7 and most probably in later versions also:
$unique = $collection->unique(function ($item)
{
return $item['brand'] . $item['model'];
});
Here is an article about this: http://laravel.at.jeffsbox.eu/laravel-5-eloquent-collection-methods-unique
You can use the groupBy method.
$downloads = DB::table('downloads')
->groupBy('file_id')
->groupBy('user_id')
->get();
OR
You can use the distinct method.
$downloads = DB::table('downloads')->distinct()->get();