I need to find the duplicates in the database entries using laravel.But my result is incorrect please help to find it.
$query = DB::table('tbl_documents as td')
->leftjoin('tbl_document_types as tc','td.document_type_id','=','tc.document_type_id')
//->leftjoin('tbl_documents_columns as tdc','td.document_id','=','tdc.document_id')
->leftjoin('tbl_departments as tdp','td.department_id','=','tdp.department_id')
->leftjoin('tbl_stacks as ts','ts.stack_id','=','td.stack_id')
->select('td.document_name','td.document_file_name','tc.document_type_id','td.document_type_id','td.stack_id','tdp.department_id')->havingRaw('count(*)>1')
->where('td.document_type_id',$doctypeid)
->get();
You should try this
$results = DB::table('tbl_documents')->whereIn('document_file_name', function ( $query ) {
$query->select('document_file_name')->from('tbl_documents')->groupBy('document_file_name')->havingRaw('count(*) > 1');
})->get()->toArray();
return $results;
Related
I want to search by customer code in collection table. I get the following error. Can you help with this
Eror message;
Property [Collection] does not exist on this collection instance.
$cut = Cut::with('cutStatus', 'Collection', 'CollectionOrder', 'CollectionOrder.collectionColor')
->orderBy('end_date', 'DESC')
->where('cut_status_id', 3)
->get();
if ($request->search){
//i am getting the error here
$cut = $cut->Collection->where('customer_code', 'LIKE', "%".request('search')."%");
}
$cut_list = [];
foreach ($cut as $cut_item){
foreach ($cut_item->CollectionOrder as $collection_order_item){
$cut_list['cut_list'][$cut_item->id]['cut_info'] = $cut_item;
$cut_list['cut_list'][$cut_item->id]['color'][$collection_order_item->collectionColor->color_name] = $collection_order_item;
$cut_list['cut_list'][$cut_item->id]['cut_piece'] = $this->CutPiece($cut_item->id);
}
}
You are calling Collection relation on an array that's why you are facing this issue. You have to use use loop for specific result or get signle entry and apply relation.
Using Loop.
foreach($cut as $c)
{
dump($c->->Collection->where('customer_code', 'LIKE',
"%".request('search')."%"));
}
I have products table that each of the product has many orders,product.php
public function orders()
{
return $this->hasMany(Order::class,'product_id');
}
I can get products that order by order_count with this code:
$products = Product::withCount('orders')->orderBy('orders_count', 'desc')->get();
Now I want to get products in controller that their orders count is bigger than 5,
How I can do it?
One way to do this would to use whereHas():
$products = Product::withCount('orders')
->whereHas('orders', null, '>', 5)
->orderBy('orders_count', 'desc')
->get();
I don't think it's mentioned in the docs but you don't have to pass a closure as the 2nd param, and the 3rd and 4th can be used to pass the operator and the count.
Alternatively, you could use having() instead:
$products = Product::withCount('orders')
->having('orders_count', '>', 5)
->orderBy('orders_count', 'desc')
->get();
You can use mapping on your collection to filter out records e.g. :`
$collection = collect($products)->map(function ($product) {
return $product->orders->count() > 5;
})
->reject(function ($name) {
return $product->orders->count() < 5;
});`
This is just an example, you can put more conditions if required.
Hope this helps.
The below code should give you just the orders with a count greater than 5
$products = Product::withCount('orders')->where('orders_count', '>', 5)->orderBy('orders_count', 'desc')->get();
Adding ->where() should do it.
Hope that helps.
Update
Try
->whereRaw('count("orders.id") > 5')
This should work to get products with more than 5 orders.
Using Laravel eloquent how do I make a query like this:
select * from branches where user_id =(select id from users where name ='sara' )
Assuming that you have a user relationship in your Branch model you could use whereHas:
$branches = Branch::whereHas('user', function ($query) {
$query->where('name', 'sara');
})->get();
Update
If you're using v8.57.0 or above, you can now use the whereRelation() method instead:
Branch::whereRelation('user', 'name', 'sara')->get();
$id = Users::select('id')->where('name','sara')->first();
$barnches = branches::where('id',$id)->get();
Here Users and branches are models , first is using for 1 row and get for many rows
I would split it into two queries. First getting the id, then getting the list. Expecting your models to be called "User" and "Branches"
$user = User::where('name', 'sara');
$id = $user->id;
$branches = Branch::where('id', $id);
This site may help you Link
Try this.
$name = 'sara';
$results = BranchModel::whereIn("user_id", function ($query) use ($name) {
$query->select("id")
->from((new UserModel)->getTable())
->where("name", $name);
})->get();
You can use this:
$users = User::whereName("sara")->get()->pluck('id');
Branch::whereIn('user_id',$users)->get();
Several questions have been asked about this but none provided a satisfactory explanation for this problem.
I run the following query:
$return = Vacation::whereHas('dates', function ($query) use ($from, $to) {
$query->whereBetween('start_date', [$from, $to]);
})->get();
According to the query log, this produces the following SQL. The returned result is correct but there is a problem with the JOINed data.
select * from `vacations` where exists (select * from `vacation_dates` where `vacations`.`id` = `vacation_dates`.`vacation_id` and `start_date` between '2017-08-01 00:00:00' and '2017-08-30 00:00:00')
Since there's no JOIN, the related records are added afterwards through eager loading and the constraint is lost.
The scenario involves Vacations that have multiple start / stop dates and I want to check which Vacations have a start_date within the $start and $end date range.
If there's no occurrences, no records are returned.
When there is an occurrence, the Vacation is returned with ALL the dates and not just the ones in the constraint.
I understand how / what it's doing but don't see how I can get what I need: the record with the joined data that follows the constraint.
Anyone?
Solution:
$callback = function($query) use($from, $to) {
$query->whereBetween('start_date', [$from, $to]);
};
$return = Vacation::whereHas('dates', $callback)->with(['dates' => $callback])->get();
Solution is from this SO post
Same answer as #stef gave, but better looking syntax (I believe)
$return = Vacation::query()
->whereHas('dates', $datesFilter = function($query) use ($from, $to) {
$query->whereBetween('start_date', [$from, $to]);
})
->with(['dates' => $datesFilter])
->get();
public function get_task_threads_first_last($task_id)
{
$this->db->select("t.nt_id, t.nt_responsed_by, t.nt_detail 'task_detail', t.nt_response_date, u.ui_full_name 'user_name', u.ui_designation 'user_designation', u.ui_phone 'extension', nt_response_date 'response_date'");
$this->db->from('tm_task_detail t');
$this->db->order_by("nt_response_date");
$this->db->join('ac_user_info u', 'u.ui_id = t.nt_responsed_by');
$this->db->where('t.nt_task_id', $task_id);
$query = $this->db->get();
return $result = $query->result();
}
I need to fetch the first and last row from above query. How is this possible ?
Also i am thinking that it is useless to select all rows than get the first and last rows out from it. I'm new in programming need advice thanks.
Thanks