Probably my mistake but i couldn't realize where my mistake is.
I have customer model which has schedule_registers() relation
public function schedule_registers()
{
return $this->hasMany('App\Models\ScheduleRegister', 'customer_id', 'id');
}
I also have ScheduleRegister model which has teacher() relation
public function teacher()
{
return $this->belongsTo('App\Models\User', 'teacher_id', 'id');
}
I get data in CustomerController like this (i don't use ->get() because i'm sending this data to datatable);
$customer_index = Customer::with([
'company',
'schedule_registers',
'schedule_registers.teacher',
]);
This query gives me that output;
https://jsonblob.com/915606370225766400
and when i try to use group by teacher_id and change query to this;
$customer_index = Customer::with([
'company',
'schedule_registers.teacher',
'schedule_registers' => function ($q) {
$q->groupBy('schedule_registers.teacher_id');
},
]);
output turns to this;
https://jsonblob.com/915605962518446080
In brief, when i try to use groupBy, i doesn't get any error, but get wrong output. If you could check outputs, when i use groupBy, some schedule_registers data becomes empty but some doesn't.
What is the right way to use groupBy in that situation?
Related
Consider the following:
$posts = $this->model->newQuery()
->whereIn('user_id', $user->following) // specifically this line
->orWhere('user_id', $user->id)
->get();
The problem with the above is that there are two queries:
Get following: $user->following
Get posts: Above
This would be much more efficient with the use of a subquery, however, I cannot actually remember the correct way to do it...
I have tried all of the following:
// This was a long-shot...
...->whereIn('user_id', function ($query) use ($user) {
$query->raw($user->following()->toSql());
});
// This works but pretty sure it can be done better with eloquent...
...->whereIn('user_id', function ($query) use ($user) {
$query->select('follow_id')
->from('user_follows')
->where('user_id', $user->id);
});
Is there a way that this can be achieved by using the previously defined relationship $user->following() instead of manually defining the relationship query like the last example above?
Reference
The following relationship is defined as follows:
/**
* Get the users that the user follows.
*/
public function following()
{
return $this->belongsToMany('SomeApp\User\Models\User', 'user_follows', 'user_id', 'follow_id')
->withTimestamps();
}
Use this:
->whereIn('user_id', $user->following()->getQuery()->select('id'))
I am trying to use Eloquent in Laravel in order to run a query. I am using the with() function in order to get the results from relationships, however this is always returning null.
This is my code:
Posts::query()
->select('id')
->with([
'author:id',
'category.images:url',
]);
Dumping the query using the getQueryLog() function and if I run the query inside of a SQL client with the same bindings I get a result back, however the response looks like this...
{
id: 1,
category: {
id: 1,
category_images: null
}
}
I've tried googling this issue and can't really find anything on it.
Thanks in advance.
Use your query like this:
Posts::query()->select('id')->with([ 'author', 'category' ])->get();
and in Posts model
public function author()
{
return $this->belongsTo('Authors::class', 'author_id', 'id')->select('id');
}
public function category()
{
return $this->belongsTo('Categories::class', 'category_id', 'id')->select('id', 'images.id', 'images.url');
}
You have to select your foreign keys in select statement to let Eloquent query your relationships, something like this:
Posts::query()
->select('id', 'author_id', 'category_id')
->with([
'author:id',
'category.images:url',
]);
Hello i have below query
Deal::with(array('deal_category'=>function($query){
$query->select('name as dealcategory');
}))->get()
when i try to retrieve dealcategory it does not return any value. I have defined relationship in model like
Deal Model
public function deal_category()
{
return $this->belongsTo('App\DealCategory', 'deal_category_id', 'id');
}
And Deal category model like
public function deals(){
return $this->hasMany('App\Deal','deal_category_id');
}
can anyone help how can i get categoryname?
You have to select the primary key to retrieve the necessary results.
Deal::with(array('deal_category'=>function($query){
$query->select('id', 'name as dealcategory');
}))->get()
Use facade DB.
You can try something like this:
DB::table('deal as d')
->join('deal_category as dc', 'd.id', '=', 'dc.deal_id')
->select('d.name as dealname', 'dc.categoryname')
->get();
I'm trying to query a model with a relation.
My method:
public function getLabel($layerId)
{
$labelGroups = Forum_label_group::
join('forum_layer_to_labels', function ($join) use ($layerId) {
$join->on('forum_layer_to_labels.layerId', '=', 'forum_label_groups.id');
})->with('labels')->get()->toJson();
return $labelGroups;
}
The output:
[{"id":4,"name":"Demogruppe","description":"bla","required":1,"created_at":"2016-10-22 12:29:27","updated_at":"2016-10-22 12:29:27","labelGroupId":2,"layerId":2,"labels":[]},{"id":5,"name":"Demogruppe 2","description":"bla 2","required":0,"created_at":"2016-10-22 12:29:27","updated_at":"2016-10-22 12:29:27","labelGroupId":2,"layerId":3,"labels":[]}]
As you can see, the label relation is empty.
Now I'm trying to query a single model instead off all:
public function getLabel($layerId)
{
return Forum_label_group::with('labels')->first()->toJson();
}
the new output:
"{"id":2,"name":"Demogruppe","description":"bla","required":1,"created_at":"2016-10-22 12:29:27","updated_at":"2016-10-22 12:29:27","labels":[{"id":5,"title":"Demo rot","name":"demo-rot","typeId":3,"groupId":2,"created_at":"2016-10-22 12:29:47","updated_at":"2016-10-22 12:29:47"},{"id":6,"title":"Demoblau","name":"demoblau","typeId":1,"groupId":2,"created_at":"2016-10-22 12:30:03","updated_at":"2016-10-22 12:30:03"}]}"
And as you can see now, everything is fine. The whole relation exists. Is there a problem with the initial query? The relation seems to be ok.
And of course it was an small issue ;)
I forgot to add a select() on my query. The original id has been overwritten by the join(). So the method tried to query an labelGroup that doesn't exist.
The correct query:
public function getLabel($layerId)
{
$labelGroups = Forum_label_group::
join('forum_layer_to_labels', function ($join) use ($layerId) {
$join->on('forum_layer_to_labels.layerId', '=', 'forum_label_groups.id');
})->select('forum_label_groups.*')->with('labels')
->get();
return $labelGroups;
}
I'm trying to give ability on user to see his orders. How can I query the database.. I'm trying something like this but I got empty page.. I mean nothing from database. May be my query ins't correct.
This is my controller
public function viewOrders() {
$user_order = self::$user->user_id;
$orders = Order::where('user_id', '=', $user_order);
return View::make('site.users.orders', [
'orders' => $orders
]);
}
Am I getting correctly user_id here? I'm not sure...
Update: I have this in my User model
public function orders() {
return $this->hasMany('Order', 'user_id', 'user_id');
}
Ok, so based on your route+button+model do it like this
$orders = self::$user->orders()->orderBy('order_id', 'asc')->get();
return View::make('site.users.orders', [
'orders' => $orders
]);
this should work.. You can remove orderBy clause if you don't need it.
If you have Authentication set properly you can do the following.
public function viewOrders(){
$user = Auth::user();
return view('site.users.orders',[
'orders' => $user->orders
]);
}
When you use the relationship without using the calling parentheses you get a collection of models which are queried if they're not already loaded. This is called lazy loading, if you want to load a relationship before accessing it you can use eager loading. In this case, it is not necessary though.