i am trying to get object from table orders with related attributes from table order_status_history. From table order_status_history, i need only last record with status 'Reviewing', so i use sort asc by field created_at. My code so far is , but i get error.
$orders = Order::GetOrderHistoryReviewing()->get();
public function scopeGetOrderHistoryReviewing($query)
{
return $query->whereHas('statusHistory', function($q) {
$q->where('status', 'Reviewing')->orderBy('created_at','desc')->first();
});
}
i need one object with relation from second table
this is my error
[2016-07-27 08:37:26] dev.ERROR: exception 'PDOException' with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "orders"
LINE 1: ...ries" where "order_status_histories"."order_id" = "orders"."...
From your question, you're apparently trying to create a scope that returns orders with status, 'Reviewing' and sorts the results in descending order.
You need to remove the first() call from the $q subquery and move orderBy to the end of your $query. Your code should look like.
public function scopeGetOrderHistoryReviewing($query)
{
return $query->whereHas('statusHistory', function($q) {
$q->where('status', 'Reviewing');
})->orderBy('created_at','desc');
}
Then you can do:
$orders = Order::GetOrderHistoryReviewing()->get();
//OR to get the first record
$first_order = Order::GetOrderHistoryReviewing()->first();
Related
I got two tables. Both have a relationship to each other. I´m trying to query both to get the matching results. This results get checked if they also have an column which matches with a parameter value.
I´m trying it with a scope and it work. I only need one column from the second table and I´m trying to use it as column in my first table when I got my result.
So the code works and I got an result but I´m trying to filter to select only one column from the second table.
My code look like that.
My controller:
public function test()
{
$UID='LQuupgYvnuVzbEoguY4TF8bnHUU2';
$event=Events::withState($UID)->get();
echo $event;
}
My model scope function:
public function scopeWithState($query,$UID){
return $query->with(['EventLiked' => function($query) use($UID) {
$query
->where('EventLiked.UID', $UID)
;
}]);
}
My hasMany relationship function:
public function EventLiked()
{
return $this->hasMany(EventLiked::class,'EID','ID')->select('State','UID','EID');
}
I would go for specifying columns inside closure.
New scope:
public function scopeWithState($query,$UID){
return $query->with(['EventLiked' => function($query) use($UID) {
$query
->where('EventLiked.UID', $UID)
->select('State');
}]);
}
Calling scope:
$event=Events::withState($UID)->get();
You're not getting expected results because Laravel splits it into 2 queries:
First, for selecting events.
Then it plucks EID
Second, when it looks for EventLiked where matching ID's is found (from second step) and loads as relationships.
So you want to change select statement only in 2nd query. Not in a first one
I wanna question about how to use order by inside eager load laravel eloquent, I already have a query like this :
$getData = StockIn::select(
StockIn::raw('group_concat(stock_ins.id_stock_in) as id_stock_ins'),
'stock_in_id_type'
)
->with(['type_of_items' => function ($query) {
$query->orderBy('type_of_item');
}])
->orderBy('type_of_items.type_of_item')
->groupBy('stock_ins.stock_in_id_type')
->get();
But when I compile the query and look to the result, the result of my query didn't make result with order by query, Am I making a mistake in my query so that the result is matching with my expectation? Thanks before
Here for my model :
Stock In :
public function type_of_items() {
return $this->belongsTo('App\TypeOfitem', 'stock_in_id_type');
}
Type Of Item :
public function stock_ins() {
return $this->hasMany('App\StockIn');
}
when I try to look on the console, the result of my query like this :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'type_of_items.type_of_item' in 'order clause' (SQL: select group_concat(stock_ins.id_stock_in) as id_stock_ins, `stock_in_id_type` from `stock_ins` group by `stock_ins`.`stock_in_id_type` order by `type_of_items`.`type_of_item` asc)
You're currently using order by on eager loaded data.
Instead, you need to call it on the model itself.
$getData = StockIn::select(
StockIn::raw('group_concat(stock_ins.id_stock_in) as id_stock_ins'),
'stock_in_id_type'
)
->with(['type_of_items' => function ($query) {
$query->orderBy('type_of_item');
}])
->orderBy('type_of_items.type_of_item')
->groupBy('stock_ins.stock_in_id_type')
->get();
You can also try it out without groupBy first, to be sure you're getting the correct results
I'm trying to do that , get list of topics order by who has new post so I create a relation in topic model like that
public function latest_post()
{
return $this->hasOne(Post::class)->latest();
}
then I used the query like that
Topic::where('locale',$this->locale)->with('latest_post')->paginate(15)->sortByDesc('latest_post.created_at');
but it's giving me an error
Collection::render does not exist
so I change the sort to orderBy like that
Topic::where('locale',$this->locale)->with('latest_post')->orderBy('latest_post','DESC')->paginate(15);
but this also gives me another error
Unknown column 'latest_post' in 'order clause'
how can solve this issue?
Hmmm. Try this
$topic = Post::orderBy('created_at','desc')->first()->join('topics', 'topics.id', '=', 'posts.topic_id')->first();
Or in your post model:
public function Topic()
{
return $this->belongsTo(Topic::class, 'topic_id');
}
To get the last active topic:
$topic = Post::orderBy('created_at','desc')->first()->Topic;
I'm trying to retrieve all records from my orders table where an order is associated with a supervisor. Orders and Supervisors are associated with a belongsToMany relationship and have a pivot table.
So my code looks something like this:
$supervisor = User::where('phone_number', $request->msisdn)->first();
$orders = Order::with(['supervisors' => function ($query) {
$query->where('supervisor_id', $supervisor->id);
}])->get();
I'm expecting to get a collection of orders which I'm then passing to a notification but instead get:
[2017-12-12 14:25:27] local.ERROR: Undefined variable: supervisor
{"exception":"[object] (ErrorException(code: 0): Undefined variable: supervisor
Not sure what I'm doing wrong?
You need to use USE to pass $supervisor into the function
$orders = Order::with(['supervisors' => function ($query) use($supervisor) {
$query->where('supervisor_id', $supervisor->id);
}])->get();
For those googling something similar, I used this and it worked:
$supervisor = User::where('phone_number', $request->msisdn)->first();
$orders = Order::whereHas('supervisors', function ($query) use($supervisor) {
$query->where('supervisor_id', $supervisor->id);
})->where('status', '=', 'pending')->get();
Big thanks to p.wright for the help!
I got this query:
$users = DB::table('users')->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('galleries')
->whereRaw('galleries.user_id = users.id');
})->get();
This query selects all users who have gallery. Problem is that I can't use eloquent releationships now. Whenever i try to loop like this:
#foreach ($user->gallery as $gallery)
{{$gallery->name}}
#endforeach
I get error:
Undefined property: stdClass::$gallery
It happens with all other tables. What am I doing wrong here? My realationships are defined and they work just fine, i got problems only in this query. Thanks.
EDIT
Since it's not eloquent query, could you show me example how to write query, into few tables with eloquent. For example, I need all users who have their status approved in example table
First, determine a relationship in the User class, like this:
class User {
// Determine relation to Example table
public function examples() {
return $this->hasMany('Example', 'user_id', 'id'); // second parameter is the foreign key
}
}
Then the query:
User::whereHas('examples', function( $query ) {
$query->where('status','approved');
})->get();