This is my query builder. But an error is shown
ERROR => count(): Parameter must be an array or an object that implements
Countable
Route::get('project', function () {
$project = DB::table('pams_project')
->join('pams_developer', 'pams_project.dev_id', '=', 'pams_developer.id')
->select(array('pams_developer.developer_name',DB::raw(count('pams_project'.'dev_id'))))
->get();
return view('projectByDeveloper', ['project' => $project]);
});
can anyone figure out my problems ?
use DB::raw("count(pams_project.dev_id) as count")
$project = DB::table('pams_project')
->join('pams_developer', 'pams_project.dev_id', '=', 'pams_developer.id')
->select(array('pams_developer.developer_name',DB::raw("count(pams_project.dev_id) as count")))
->get();
But also make sure to use GroupBy() with this.
If you want to get the count of collection u can simply use $project->count()
Hope this helps
Related
$orders = Orders::select('orders.id as order_id', 'collection_color.color_name as color', 'collection_color.id as collection_color_id', DB::raw('SUM(order_piece.piece) As piece'))
->join('order_piece', 'order_piece.order_id', '=', 'orders.id')
->join('collection_color_size_barcode', 'collection_color_size_barcode.id', '=', 'order_piece.collection_color_size_barcode_id')
->join('collection_color', 'collection_color.id', '=', 'collection_color_size_barcode.collection_color_id')
->whereIn('orders.id', $request->order_id)
->groupBy('order_piece.order_id')
->orderBY('orders.delivery_date', 'ASC')
->get();
return $orders; => [{"order_id":30,"color":"Kahverengi","collection_color_id":21,"piece":"500"}]
return $ccfc = CollectionColorFabricColor::whereIn('collection_color_id', $orders->collection_color_id)->get();
Property [collection_color_id] does not exist on this collection instance. i am getting error can you help me
The error is most likely due to this in your second code snippet: $orders->collection_color_id. $orders is a collection, so the property doesn't exist in that object. What you actually need is to pluck those values from that collection like so:
return $ccfc = CollectionColorFabricColor::query()
->whereIn('collection_color_id', $orders->pluck('collection_color_id'))
->get();
Because your $orders is collection, you need to get collection_color_id array like this :
$arrayColors = $orders->pluck('collection_color_id')->toArray();
then
return $ccfc = CollectionColorFabricColor::whereIn('collection_color_id', $arrayColors)->get();
I have a get formule that returns some nested relationships in an array. I was wondering how to access them in a where statement.
The initial get
$taken = UserWork::with('work.place')
->with('user')
->with('work.timeslot')
->get();
I tried something like this
$taak = $taken->where('work.timeslot[0].start_hour',"17:00:00")->first();
json result from $taken
Using with will endup with two queries. if you want to bring the user with timeslot null then there no need to add whereHas
$callback = function($query) {
$query->where('start_hour',"17:00:00");
};
$taken = UserWork::whereHas('work.timeslot', $callback)
->with(
['work.place', 'user', 'work.timeslot' => $callback]
)->get();
I'm doing the simple query
$offers = Offer::join('offer_speciality', 'offers.id', 'offer_speciality.offer_id')
->join('specialities', 'offer_speciality.speciality_id', 'specialities.id')
->when($speciality, function($query, $speciality) {
return $query->where('specialities.number', $speciality)->get();
})
->orderBy('created_at','desc')->paginate(9);
and then error:
Method Illuminate\Database\Eloquent\Collection::orderBy does not
exist
I'm not sure what is it?
At the point of running ->get() a collection is being returned and ->orderBy() is not a function you can run on a collection with all the functions available being listed in the documention.
You've got your ->get() before your ->orderBy() and that wont work. Plus you don't need ->get() as you've got ->paginate(9)
You need:
$offers = Offer::join('offer_speciality', 'offers.id', 'offer_speciality.offer_id')
->join('specialities', 'offer_speciality.speciality_id', '=', 'specialities.id')
->when($speciality, function($query, $speciality) {
return $query->where('specialities.number', $speciality)
})
->orderBy('created_at','desc')->paginate(9)
I have 2 models (User & UsersDetails) in relation with Eloquent Laravel.
Model "User" Realation With 'SelDetails'
public function SelDetails {
return $this->belongsTo('App\UsersDetails', 'users_id');
}
I want to get only two column 'created_by_id', 'created_by_name' from SelDetails
UserController
$users = User::with(['SelDetails' => function($query){
$query->select(['created_by_id', 'created_by_name']);
}])
->where('is_active', '=', 0)
->orderBy('id', 'desc')
->get();
I am getting data from User but getting blank in SelDetails
[relations:protected] => Array
(
[SelDetails] =>
)
Please correct me.
guys thanks for your precious response on this issue. i got the solution via following way.
changes in "User" model Relation With 'SelDetails'
public function SelDetails {
#return $this->belongsTo('App\UsersDetails', 'users_id');// the old one
return $this->hasOne('App\UsersDetails', 'users_id');
}
changes in "UserController"
$users = User::with('SelDetails')->where('is_active', '=', 0)->orderBy('id', 'desc')->get();
that's the changes that I have made and got the result
Thanks
I might be wrong but it might be the array in select, try
$query->select('created_by_id', 'created_by_name');
Have you tried something like
$users = App\Book::with('author:id,name')->get(); from the documentation?
In your case it should looks like:
User::with('SelDetails:created_by_id,created_by_name')
->where('is_active', '=', 0)
->orderBy('id', 'desc')
->get();
with() method can take string (or array filled by strings) eg. relationName:column1,column2.
I just discovered Laravel Scout and I wanted to make a search with where clausure. The code shown below
$notes = Note::search($request->lecturer_search)->where([
['course_id','=',$course_id],
['course_code_number', '=', $request->course_code_number_search]
])->orderBy('created_at','desc')->paginate(5);
But I'm getting this error:
Type error: Too few arguments to function Laravel\Scout\Builder::where(), 1 passed in /home/vagrant/www/Bee/app/Http/Controllers/SearchController.php on line 36 and exactly 2 expected
When I remove where clausure, there is no problem.
Scout has it's own where() method which accepts just two parameters: field and value. So do this:
->where('course_id', $course_id)
->where('course_code_number', $request->course_code_number_search)
Instead of this:
->where([
['course_id','=',$course_id],
['course_code_number', '=', $request->course_code_number_search]
])
You can look at the source code of the where() method here.
I generate to queries.
One via relational search. And on with scout.
Then I reduce the relational results with the results of the scout search.
In your case:
$notes = Note::where([
['course_id','=',$course_id],
['course_code_number', '=', $request->course_code_number_search]
]);
$notesSearchResults = Note::search($request->lecturer_search);
$searchResultIds = $notesSearchResults->map(function ($item, $key) {
return $item['id'];
});
$notes = $notes->whereIn('notes.id', $searchResultIds)->get();