Updating table column with value from another column in same table - laravel

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();
}

Related

How to fill a column in a Database with a query using Laravel?

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.

Laravel where statement

I am new in Laravel. I want to create a search filter using eloquent model.
I want to check some columns are equal to some enteries and return the results, but the problem is that if any of the enteries is empty it returns nothing.
How to make it to search if other enteries matche and return values even one of the entery is empty.
$result = person::Where('age','>=',$age)
->Where('hairColor','=',$hairColor)
->Where('height','>=',$height)
->get();
Just use orWhere
$result = person::Where('age','>=',$age)
->orWhere('hairColor','=',$hairColor)
->orWhere('height','>=',$height)
->get();
Also, check this documentation
Laravel where clauses

How to set field for ::create in Laravel?

I use this method to create new row in DB:
$input = $request->all();
return Recipient::create([$input]);
How I can add additional field to $input with an value?
I tried:
$input["user_id"] = Auth::id();
But when I display query INSERT, I can not see field user_id
The problem here is that you probably don't set $fillable property in Recipient model. You should add user_id there:
$fillable = [ ..., 'user_id'];
However merging input is usually not the best way to deal with such things. If you have set relationships properly, it should be possible to do something like this:
Auth::user()->recipients()->create($request->all());
Update
Like #Marcin Nabialek said: add 'user_id' to the $fillable array. And, his solution is cleaner.
Merge
The method merge is what you're looking for: Merge new input into the current request's input array.
$request->merge(['user_id' => Auth::user()->id]);
Recipient::create($request->all());
See the api documentation here: https://laravel.com/api/5.2/Illuminate/Http/Request.html#method_merge
I'm still not sure what exactly you are looking for. Suppose you have 2-3 fields user_id, title, content. For inserting into the database you need to do following:
$input = new Recipient(['title'=>'You title', 'content'=>'Your content']);
$input->save();
It will save two fields with the incremented user_id.
Bro, You're using wrong method,
Try this one:
$input["user_id"] =Auth::user()->id;

Laravel Collection Filter By Multiple Concatenated Unique Fields

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();

Laravel Eloquent and combing data to send to View

I'm trying to figure out a way to combine data from two tables into a single variable that I can then send to my view. I'm using Authority as my authentication bundle and so I have the following tables set up: users, roles, role_user. I want to get the following data into a single variable.
From the users table:
id, name, email
From the roles table:
name
The following returns all the user data in the users table:
$users = User::all();
But, I want to make a chain that can get the related data stored in the roles table? I want all users and all of each users roles together.
And while I can find examples that help get related data for a single record I haven't been able to find any reference to retrieving entire tables with related data.
Thanks.
$users = User::with('roles')->get()
Eager loading is what you are looking for, read the docs :)
Im not 100% sure what you are after, I think you should make relations with Eloquents methods. But if you are looking for a way to just merge the data returned by two models you could do the following:
$users = User::all();
$roles = Roles::all();
$array = array_merge($users->toArray(), $roles->toArray());
return Response::json($array);
You could make the model calls so that they return exactly what you are after and then merge them together. If you want one Model to return something specific just make a function for it.
You need to use something like that:
$user = User::get();
$roles = Roles::get();
$user .= (object) $roles; //because of that both them are object array and we have to combine them as object array...
than you can pass the data to View
return View:make('templatefile')//or Redirect to_route whatever
->with('user', $user);
But the best way is belongs_to in your case. Please read the documentation:
http://laravel.com/docs/database/eloquent#relationships

Resources