I want to merge example_1 and example_2 values into array.
example_1 and example_2 are of type int.
User::select('example_1','example_2')->where('id',Auth::user()->id)->get();
// The result: [{"example_1":"1","example_2":"2"}]
example_1 and example_2 if has value 1 and 2 respectively.
I want to have an array : [1,2]
You can do one of this
$result = array_only(auth()->user()->toArray(), ['example_1','example_2']);
// Or
$result = User::where('id', auth()->id())->first(['example_1','example_2'])->toArray();
// Finally
$data = array_values($result);
All Eloquent queries return Collection objects. Those can be modified easier (through collections methods).
You can use first() to get the first object out of this collection.
Now you can access the variables of the Eloquent User model by calling them like you'd call public class properties.
Your final code would look like:
$user = User::select('example_1','example_2')
->where('id',Auth::user()->id)->get()->first();
$array = [ $user->example_1, $user->example_2 ];
// The result: [1,2]
Related
i have 2 tables User and subadmin consider user have 3 columns and subadmin has 2 column i want to get 3+2 (5 column) data into a single veriable array
the technique i want to use is that in user table i have id which is same in subadmin table with sub_admin_id(column) how i can use eloquent model to first link id with sub_admin_id and then into a single query get 5 column in single veriable array
here i am using to get data
$subadmindata = User::find($id)->get(); // [column1 ,column2 ,column3 ]
$subadmindata1 = SubAdmin::find($id)->get(); // [column1 ,column2 ]
output should be
$data = // [column1 ,column2 ,column3 , column4 ,column5 ]
note i dont want to use array merge or combine method i want to use eloquent model for my learning
you could use concat like this
$subadmindata = User::find($id)->get();
$subadmindata1 = SubAdmin::find($id)->get(); // it will return array of collections
$data = $subadmindata->concat($subadmindata1);
Notice when you use get after find it stop it's jobs so there is no need to find here
get() method will give you a collection not array, so you can merge two collection as follows.
$subadmindata = User::find($id)->get();
$subadmindata1 = SubAdmin::find($id)->get();
$data = $subadmindata->merge($subadmindata1);
You can't use find with get(Assuming that you need a one result not all of the users). Try this. But looks like you need build the relationships correctly first. Anyway, quick answer is below.
$userCols = User::select('column1','col2','col3')->find($id);
$subAdminCols = SubAdmin::select('col4','col5')->find($id);
$cols = array_merge($userCols->toArray(), $subAdminCols->toArray());
try this
in user model
public function subadmin(){
return $this->hasOne(SubAdmin::class,'sub_admin_id');
}
in controller
$sub_admins = User::find($id);
dd($sub_admins->subadmin->sub_admin_id)
you can use php ... operator to to push data
example like this
$subadmindata = User::find($id)->get()->toArray(); // [column1 ,column2 ,column3 ]
$subadmindata1 = SubAdmin::find($id)->get()->toArray(); // [column1 ,column2 ]
array_push($subadmindata, ...$subadmindata1);
return $subadmindata
ref link https://wiki.php.net/rfc/spread_operator_for_array
Can we get the total record count and the first record only in the same eloquent query?
I know we can get the total records via below eloquent queries:
1. Model::count();
2. Collection Method
Model::all()->count();
And we can get the first record like this:
Model::first();
I have one solution for the same i.e.
$data = Model::all();
1. $count = count($data);
2. $count = $data->count();
And for the first row/data
1. $firstRecord = $data[0];
2. $firstRecord = $data->first();
When you create a query, Laravel under the hood, creates a new instance of the query builder with the method newQuery(), this query can be reused as much times as you want
$query = Model::where('...')->whereHas('...')->orderBy('...');
[
'count' => $query->count(),
'collection' => $query->get(),
'first' => $query->first()
]
Something similar happens with the LengthAwarePaginator's paginate() method. Have a look at the source code.
$projects = Project::find(collect(request()->get('projects'))->pluck('id')); // collect(...)->pluck('id') is [2, 1]
$projects->pluck('id'); // [1, 2]
I want the result to be in the original order. How do I achieve this?
Try $projects->order_by("updated_at")->pluck("id"); or "created_at" if that's the column you need them ordered by.
Referencing MySQL order by field in Eloquent and MySQL - SELECT ... WHERE id IN (..) - correct order You can pretty much get the result and have it order using the following:
$projects_ids = request()->get('projects'); //assuming this is an array
$projects = Project::orderByRaw("FIELD(id, ".implode(',', projects_ids).")")
->find(projects_ids)
->pluck('id'));
#Jonas raised my awareness to a potential sql injection vulnerability, so I suggest an alternative:
$projects_ids = request()->get('projects');
$items = collect($projects_ids);
$fields = $items->map(function ($ids){
return '?';
})->implode(',');
$projects = Project::orderbyRaw("FIELD (id, ".$fields.")", $items->prepend('id'))
->find($projects_ids);
The explanation to the above is this:
Create a comma separated placeholder '?', for the number of items in the array to serve as named binding (including the column 'id').
I solve this by querying the data one by one instead mass query.
$ids = collect(request()->get('projects'))->pluck('id');
foreach($ids as $id){
$projects[] = Project::find($id);
}
$projects = collect($projects);
$projects->pluck('id');
I have to do this manually because laravel collection maps all the element sorted by using ids.
I am developing a php project using Laravel 5.2. In my app I am retrieving records from database using manual query. But I am having a problem with retrieving records by using where in statement with csv.
Example how I am retrieving
$csv = "1,3,5";
$sql = "SELECT * FROM `items` WHERE `id` IN (?)";
$rows = DB::select($sql,[$csv]);
As you can see above I am retrieving three rows. But it returns only one row where id is 1. Why is that?
You can't do it like that. Each entry in your csv is a separate parameter, so for your code you would actually need IN (?, ?, ?), and then pass in the array of values. It would be pretty easy to write the code to do this (explode the string to an array, create another array of question marks the same size, put it all together).
However, you are using Laravel, so it would be easier to use the functionality Laravel provides to you.
Using the query builder, you can do this like:
$csv = "1,3,5";
// turn your csv into an array
$ids = explode(",", $csv);
// get the data
$rows = DB::table('items')->whereIn('id', $ids)->get();
// $rows will be an array of stdClass objects containing your results
dd($rows);
Or, if you have an Item model setup for your items table, you could do:
$items = Item::whereIn('id', $params)->get();
// $items will be a Collection of Item objects
dd($items);
Or, assuming id is the primary key of your items table:
// find can take a single id, or an array of ids
$items = Item::find($params);
// $items will be a Collection of Item objects
dd($items);
Edit
If you really want to do it the manual way, you could use a loop, but you don't need to. PHP provides some pretty convenient array methods.
$csv = "1,3,5";
// turn your csv into an array
$ids = explode(",", $csv);
// generate the number of parameters you need
$markers = array_fill(0, count($ids), '?');
// write your sql
$sql = "SELECT * FROM `items` WHERE `id` IN (".implode(',', $markers).")";
// get your data
$rows = DB::select($sql, $ids);
$pidArray contains product ID's, some of those product ID's can be the same. I.E: 34 34 56 77 99 34. As is, it appears the whereIn method does not return results for a productId it has already found in $pidArray, even if it has a different index.
$productDataForOrder = Product::whereIn('id', $pidArray)->get(['id','price']);
$totalAmount = $productDataForOrder->sum('price');
$productDataForOrder now contains product data, but only for unique ProductID's in $pidarray. So when sum function is run, the sum is wrong as it does not take into account the price for multiple instances of the same productID.
The following code also does not return objects for every product ID in the array which are the same. So if $pidArray contains three identical product ID's, the query will only return a collection with one object, instead of three.
$query = Product::select();
foreach ($pidArray as $id)
{
$query->orWhere('id', '=', $id);
}
$productDataForOrder = $query->get(['id','price']);
$totalAmount = $productDataForOrder->sum('price');
You're not going to be able to get duplicate data the way that you're trying. SQL is returning the rows that match your where clause. It is not going to return duplicate rows just because your where clause has duplicate ids.
It may help to think of it this way:
select * from products where id in (1, 1)
is the same as
select * from products where (id = 1) or (id = 1)
There is only one record in the table that satisfies the condition, so that is all you're going to get.
You're going to have to do some extra processing in PHP to get your price. You can do something like:
// First, get the prices. Then, loop over the ids and total up the
// prices for each id.
// lists returns a Collection of key => value pairs.
// First parameter (price) is the value.
// Second parameter (id) is the key.
$prices = Product::whereIn('id', $pidArray)->lists('price', 'id');
// I used array_walk, but you could use a plain foreach instead.
// Or, if $pidArray is actually a Collection, you could use
// $pidArray->each(function ...)
$total = 0;
array_walk($pidArray, function($value) use (&$total, $prices) {
$total += $prices->get($value, 0);
});
echo $total;
The whereIn method only limits the results to the values in the given array. From the docs:
The whereIn method verifies that a given column's value is contained within the given array
Id make a query variable and loop through the array adding to the query variable in each pass. Something like this:
$query = Product::select();
foreach ($pidArray as $id)
{
$query->where('id', '=', $id);
}
$query->get(['id','price']);
Here is a code that would work for your use case expanding on #patricus
You first fetch an array of key as id and value as price from the products table
$prices = Product::whereIn('id', $pidArray)->lists('price', 'id');
$totalPrice = collect([$pidArray])->reduce(function($result, $id) use ($prices) {
return $result += $prices[$id];
}, 0);