I want to remove array from arrays but array is in $variable. I did not find any example which has array in array for $variable.
Function:
public function firstHourTrades()
{
$user_id = Auth::user()->id;
$first_hour = DB::table('finaltrade')
->select(DB::raw('count(*) as first'))
->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
->where('finaltrade.user_id', $user_id)
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '>=', DB::raw('exchanges.start_time'))
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '<=', DB::raw("ADDTIME(exchanges.start_time, '01:00:00')"))
->get();
$last_hour = DB::table('finaltrade')
->select(DB::raw('count(*) as last'))
->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
->where('finaltrade.user_id', $user_id)
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '<=', DB::raw('exchanges.close_time'))
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '>=', DB::raw("SUBTIME(exchanges.close_time, '01:00:00')"))
->get();
$other_hours = DB::table('finaltrade')
->select(DB::raw('count(*) as other'))
->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
->where('finaltrade.user_id', $user_id)
->whereRaw('finaltrade.created_at NOT BETWEEN exchanges.start_time AND DATE_ADD(exchanges.start_time, INTERVAL 1 HOUR)')
->whereRaw('finaltrade.created_at NOT BETWEEN exchanges.close_time AND DATE_SUB(exchanges.close_time, INTERVAL 1 HOUR)')
->get();
$data = [$first_hour,$last_hour,$other_hours];
return response()->json($data );
}
OUTPUT of above function :
[[{"first":1}],[{"last":0}],[{"other":4}]]
I want to remove array in array, what i do in my function?
If your queries result as a single row which i guess yes because you are not using group by then you can use first() instead of get()
Output will be
[{"first":1},{"last":0},{"other":4}]
<?php
$prefs =
[
['fruit' => 'banana'],
['nut' => 'pecan'],
['pasta' => 'fusili']
];
$flatter = array_merge(...$prefs);
var_export($flatter);
Output:
array (
'fruit' => 'banana',
'nut' => 'pecan',
'pasta' => 'fusili',
)
Related
here my code :
$prestations = Prestation::with(
[
'service' => function($service) use($searchService) {
$service->select(['id','name'])->where('name', 'regexp', "/$searchService/i");
},
'facility' => function($facility) use($searchPartenaire) {
$facility->select(['id','name'])->where('name', 'regexp', "/$searchPartenaire/i");
}
]
)
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
The problem is that in the output of all datas where "service" and "facility" names are not equal on the $searchService and $searchPartenaire the values are replaced by "null".
So i don't want to have values in the output where the search variables are not equals.
Thank you.
you can try like this
$prestations = Prestation::with('service','facility');
$prestations->whereHas('service', function ($query) use ($searchPartenaire) {
$query->Where('name', 'like', '%' . $searchPartenaire . '%');
});
$prestations->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->Where('name', 'like', '%' . $searchPartenaire . '%');
});
$prestations->where('name', 'like', '%'.$search.'%')
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
return $res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
1st create instances of Prestation $prestations = Prestation::with('service','facility')
then apply the condtion this is good approach in seach
Here my code after Kamlesh Paul suggestion :
$prestations = Prestation::with('service','facility');
$prestations->whereHas('service', function ($query) use ($searchService) {
$query->where('name', 'regexp', "/$searchService/i");
});
$prestations->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->where('name', 'regexp', "/$searchPartenaire/i");
});
$prestations->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
return $res;
But there is an infinite calls of http request, i think that the problem is when the where don't find an equal name, anyone have a suggest ?
Thank's.
I finnaly found a solution very similar :
$prestations = Prestation::with('service','facility')
->whereHas('service', function ($query) use ($searchService) {
$query->where('name', 'regexp', "/$searchService/i");
})
->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->where('name', 'regexp', "/$searchPartenaire/i");
})
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
Thank's for your help.
I want to select from 2 table a data to all users and merge this data to object that I want to return
example
{
user:{
id:1,
name:bla,
saved cards:[
{id:1, name:test},
{id:2, name:test2}
]
},
{id:2, name:bla1,
saved cards:[
{id:1, name:test},
{id:2, name:test2},
{id:3, name:test3}
]
}
}
public function getalluser(Request $request)
{
$User_data = User::where('users.role', '=', 0)
->get();
$count = count($User_data);
for ($i = 0; $i < $count; $i++) {
$json_data[] = [
'user' => User::where('users.role', '=', 0)
->where( 'users.id', $User_data->id[$i])
->leftJoin('webrole', 'users.role', '=', 'webrole.id')
->get(),
'saved cards' => User::where('users.role', '=', 0)
->where( 'credit_cards.user_id', $User_data->id[$i])
->leftJoin('credit_cards', 'users.id', '=', 'credit_cards.user_id')
->get()
];
}
return response()->json($User_data);
}
foreach($User_data as $data) {
$json_data[] = [
'user' => User::where('users.role', '=', 0)
->where( 'users.id', $data->id)
->leftJoin('webrole', 'users.role', '=', 'webrole.id')
->get(),
'saved cards' => User::where('users.role', '=', 0)
->where( 'credit_cards.user_id', $data->id)
->leftJoin('credit_cards', 'users.id', '=', 'credit_cards.user_id')
->get()
];
}
Hello Stack overflow community
I currently have the following code (example 1), which will return the list of restaurants with their food type and area, using additional joins.
I need to add more WHERE clauses, but only if there is any to be passed like (example 2)
The search may only have a name or part of a name, maybe they just want to search by area or food type.
example 1
$q = Input::get ( 'q' );
if($q != ""){
$restaurants = DB::table('restaurant')
->where ( 'restaurant.name', 'LIKE', '%' . $q . '%' )
->join ( 'area', 'restaurant.area_id', '=', 'area.id' )
->join ( 'food_type', 'restaurant.food_type_id', '=', 'food_type.id' )
->select( 'restaurant.*', 'food_type.name as food_type_name', 'area.slug as area_slug', 'area.name as area_name')
->paginate(2);
$pagination = $restaurants->appends ( array (
'q' => Input::get ( 'q' )
) );
} else {
$restaurants = DB::table('restaurant')
->join ( 'area', 'restaurant.area_id', '=', 'area.id' )
->join ( 'food_type', 'restaurant.food_type_id', '=', 'food_type.id' )
->select( 'restaurant.*', 'food_type.name as food_type_name', 'area.slug as area_slug', 'area.name as area_name')
->paginate(15);
}
example 2
if ($q){
->where ( 'restaurant.name', 'LIKE', '%' . $q . '%' )
}
if ($request->has('area')){
->where('area', $request->input('area'));
}
if ($request->has('foodtype')){
foreach(foodtype as name => value)
->where('foodtype', $value);
}
I feel there should be a way to do this, rather than writing out a different query for each possible outcome
I have tested the response by Unamata Sanatarai, (Example 3
but this came with an error:
Call to undefined method Illuminate\Database\Query\Builder::appends()
I have tried replacing ->paginate() with ->get() (as mentioned in another post), but i receive the same error
Example 3
$q = Input::get ( 'q' );
if($q != ""){
$restaurants = DB::table('restaurant');
$restaurants->where ( 'restaurant.name', 'LIKE', '%' . $q . '%' );
$restaurants->join ( 'area', 'restaurant.area_id', '=', 'area.id' );
$restaurants->join ( 'food_type', 'restaurant.food_type_id', '=', 'food_type.id' );
$restaurants->select( 'restaurant.*', 'food_type.name as food_type_name', 'area.slug as area_slug', 'area.name as area_name');
$restaurants->paginate(15);
$pagination = $restaurants->appends ( array (
'q' => Input::get ( 'q' )
) );
} else {
$restaurants = DB::table('restaurant')
->join ( 'area', 'restaurant.area_id', '=', 'area.id' )
->join ( 'food_type', 'restaurant.food_type_id', '=', 'food_type.id' )
->select( 'restaurant.*', 'food_type.name as food_type_name', 'area.slug as area_slug', 'area.name as area_name')
->paginate(15);
}
Example 4 Will explain my latest error
Undefined property: Illuminate\Database\MySqlConnection::$main_image
$main_image is one of the fields in the database - this worked fine up until i changed the query to example 3
Example 4 shows the new code with links, this produces the error
Example 4
$restaurants = DB::table('restaurant');
$restaurants->join ( 'area', 'restaurant.area_id', '=', 'area.id' );
$restaurants->join ( 'food_type', 'restaurant.food_type_id', '=', 'food_type.id' );
$restaurants->select( 'restaurant.*', 'food_type.name as food_type_name', 'area.slug as area_slug', 'area.name as area_name');
$restaurants->paginate(15)->links();
You can do it just about as you've described in the example 2.
Until you call the last method, you can continue to manipulate your Builder.
You can modify your Query Builder until you call ->paginate() or ->get() or ->first()...
$restaurants = DB::table('restaurant');
if ($q){
$restaurants->where ( 'restaurant.name', 'LIKE', '%' . $q . '%' )
}
if ($request->has('area')){
$restaurants->where('area', $request->input('area'));
}
if ($request->has('foodtype')){
foreach(foodtype as name => value)
$restaurants->where('foodtype', $value);
}
return $restaurants->paginate();
In order to answer your Second question, the one about the appends() error.
The appends() method can be called on paginate() results. At the moment, you are continuing to call it on the query builder.
The function ->paginate(), returns pagination from the $restaurants query builder.
// ...
$pagination = $restaurants->paginate();
$pagination->appends(array (
'q' => Input::get ( 'q' )
) )->links();
https://laravel.com/docs/5.5/pagination#displaying-pagination-results
I'm building a Products API. I need to return a collection of products and a variable telling me if I have more results starting from the last the answer has just returned me to show or hide a load more button. This is all I have until now:
$query = Product::query();
$query->where('category_id', $request->get('category_id'));
$query->orderBy('order', 'asc')
->orderBy('name', 'asc')
->skip($skip)
->take($take);
And this is how I return it:
return [
'products' => $query->get(['id', 'name', 'front_image', 'back_image', 'slug']),
'has_more' => ????
];
How can I calculate the has_more?
The easiest approach would be to query the database twice:
$query = Product::query()
->where('category_id', $request->get('category_id'))
->orderBy('order', 'asc')
->orderBy('name', 'asc');
$count = $query->count();
$hasMore = $skip + $take < $count;
$models = $query->skip($skip)
->take($take)
->get(['id', 'name', 'front_image', 'back_image', 'slug']);
return [
'products' => $models,
'has_more' => $hasMore
];
You can just get the count of the entire records and then simply do the check for has more like so:
<?php
$query = Product::query()
->where('category_id', $request->get('category_id'));
->orderBy('order', 'asc')
->orderBy('name', 'asc');
$count = $query->count();
return [
'products' => $query->skip($skip)
->take($take)
->get(['id', 'name', 'front_image', 'back_image', 'slug']),
'has_more' => ($hm = ($count - ($take + $skip))) > 0 ? $hm : false
];
I have this query:
Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->update(array(
'status' => 'stopped',
));
The problem is that the amount of records it has to go through to do the update causes it to take around 15 minutes or so to finish.
I would like to split it up so the select and update queries are separate entities. Something sort of like this:
$pending = Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->get();
$pending->update(array(
'status' => 'stopped',
));
How would I go about doing this? Or is there an easier way?
Thanks!
I wasn't thinking, I figured out the answer. I had to run the second part in a foreach like so:
$records = Sendqueue::select()
->where('userID', Session::get('uid'))
->where('campaign', $id)
->where('status', 'pending')
->get();
foreach ($records as $record) {
DB::table('sendqueue')
->where('ID', $record->ID)
->update(['status' => 'stopped']);
}
protected $table="user";
public function updateUser($id,$username)
{
$resultData = array();
$updateArray = array('user_name'=>$username);
$update=DB::table('user')
->where('user_id', $id)
->update($updateArray);
return $resultData['status'] = true;
}
$my_id = preg_replace ('#[^0-9]#', '', $request->id);
if (! empty ($my_id)) {
$this->c->where ('id', $my_id )->update ( [
'first_name' => $request->get ( 'first_name' ),
'last_name' => $request->get ( 'last_name' ) ,
'phone' => $request->get ( 'phone' )
] );`enter code here`
\Session::flash ('message', 'Update Successful');
return redirect ('customer');
}
$this->edit ();
http://developer.e-power.com.kh/update-query-in-laravel-5-2/