I want to sort result from database by few colums.
I trying like this:
Video::select('videoss.name as videoname', 'videos.*')->join('users', 'users.id', '=', 'videos.user_id')
->orderBy('videos.name', 'desc')
->orderBy('videos.views', 'desc')
->paginate(15);
but it is sorted only by name. How add sorting by views after sort by name.
Have you tried in an array as explained in the comments here like this:
$user->orders = array(array('column' => 'name', 'direction' => 'desc'), array('column' => 'email', 'direction' => 'asc'));
Related
I have a query which returns names of categories and its subcategories.
$subcategories = Subcategory::select('subcategories.name as subcatname', 'categories.name as catname')
->join('categories', 'subcategories.idCategory', '=', 'categories.id')
->get();
And now I get results like:
'Music' => 'Jazz',
'Music' => 'Rock',
'Music' => 'Pop',
'Movie' => 'Action'
How can I group it to have something like this:
'Music' => array('Jazz', 'Rock','Pop'),
'Movies' => array('Action')
Is it possible without making to much looping iterations and checking which subcategory belongs to which category?
You can use laravel collection
First you need to group by groupBy method, then map each group and merge every child array.
$result = collect($subcategories)
->groupBy('catname')
->map(function ($item) {
return array_merge($item->toArray());
})->all();
I am having some trouble in Laravel using the query builder to select users based on two where clauses.
The first where clause would contain multiple where statements to select a user based on some conditions. If this fails in the OR part I would like to check simply if the user ID is in the array.
Would be grateful for some advice on how I could achieve this.
Thanks.
$rs = DB::table('tblUser')
->select('name')
->where(function($q) {
$q->where(function($q){
$q->where('fid', '=', $this->argument('fid'))
->where('type', '<', 10)
})
->orWhereIn('id', $userIdSArray);
})
->get();
$userIdSArray = [ '2', '4', '5' ];
$rs = DB::table( 'Users' )
->select( 'name' )
->where( function ( $q ) use ( $userIdSArray ) {
$q->where( function ( $q ) {
$q->where( 'fid', '=', $this->argument( 'fid' ) )
->where( 'type', '<', 10 );
})
->orWhereIn( 'id', $userIdSArray );
})
->get();
I think you need to pass $userIdSArray in Query builder . And You also forgot (;) after [->where( 'type', '<', 10 )]
i have this
public function deals(){
$matchThese = [ 'suspended' => 0, 'status' => 1, 'approved' => 1 ];
$deals = ListsDeals::where( $matchThese )->orderBy( 'start_date' )->get();
$list = DB::table( 'lists' )
->join( 'list_has_deals', 'lists.id', '=', 'list_has_deals.list_id' )
->where( 'list_has_deals.deal_id', '=', 12)
->select( 'lists.id' )
->get();
$categories = DB::table( 'lists_categories' )
->join( 'list_has_categories', 'lists_categories.id', '=', 'list_has_categories.category_id' )
->where( 'list_has_categories.list_id', '=', $list[0]->id )
->select( 'lists_categories.title' )
->get();
return view( "deals" )
->with( "deals", $deals )
->with( "categories", $categories );
}
I want to obtain the value of title for the categories in lists, but first i need to obtain the deals for the lists in less words
I have
Deals - Lists - lists_has_deals
Categories - Lists - lists_has_deals
Both are belongsToMany, i want obtain in the deals the categories belonging to the list that is associated with the offer.
In my example that works fine but the 12 need to be a foreach of the $deals not be number static, thanks for the help.
Try using a has many through or intermediate pivot table on lists_has_deals. If I'm understanding your question, lists should have the necessary reference to both deals and categories.
https://laravel.com/docs/5.4/eloquent-relationships#has-many-through
https://laravel.com/docs/5.4/eloquent-relationships#many-to-many
specifically withPivot()
NOTE Please do not suggest using Eloquent, this is specifically for the Laravel query builder.
For performance reasons we are using Query Builder to retrieve results from a table:
DB::table('posts')->get();
If we then want to join a relation onto that query:
DB:table('posts')
->leftJoin('comments', 'posts.id', '=', 'comments.post_id')
->get();
The results are merged into the array of each post:
[
'id' => 1,
'title' => 'My Blog Post',
'content' => '<h1>This is a post</h1><p>hello world</p>',
'post_author' => 'Billy',
'comment' => 'This is a comment',
'comment_author' => 'Andrew',
]
How can we have the joined results placed into a nested array? Such as:
[
'id' => 1,
'title' => 'My Blog Post',
'content' => '<h1>This is a post</h1><p>hello world</p>',
'post_author' => 'Billy',
'comment' => [
'id' => 22,
'comment' => 'This is a comment',
'comment_author' => 'Andrew',
],
]
Dont think its doable out of the box without Eloquent.
You can go the primitive route:
$results = DB:table('posts')
->leftJoin('comments', 'posts.id', '=', 'comments.post_id')
->select('posts.*', 'comments.*', 'comments.id as comments_id')
->get();
foreach($results as &$result)
{
$result['comment'] = [
'id' => $result['comment_id'],
'comment' => $result['comment'],
'comment_author' => $result['comment_author']
];
unset($result['comment_author'], $result['comment_id']);
}
Since you work with DB facade and not Eloquent, and cannot use built-in with() method, you have to implement it yourself:
$posts = DB::table('posts')->get()->toArray();
$comments = DB::table('comments')->get()->toArray();
foreach($posts as &$post)
{
$post->comments = array_filter($comments, function($comment) use ($post) {
return $comment->post_id === $post->id;
});
}
return $posts;
If you want to get rid of post_id for comments entries, you can do:
$posts = DB::table('posts')->get()->toArray();
$comments = DB::table('comments')->get()->toArray();
foreach($posts as &$post)
{
$comments = array_filter($comments, function($comment) use ($post) {
return $comment->post_id === $post->id;
});
$post->comments = array_map(function ($comment) {
unset($comment->id);
return $comment;
}, $comments);
}
return $posts;
(I guess the runtime would be similar to with(), since after-all MySql does not provide this functionality out-of-the-box).
Here some new information:
$data= $DB->select("select posts.*,comments from posts left join comments on posts.id = comments.post_id");
I am not sure it is work or not but you can try
You can try with this
$data= $DB->select("select *,(select json_agg(datas) from (select * from comments where posts.id=comments.post_id) as datas) as comments from posts;");
But you may need to decode at comments as well
For example I have the following query which updates a new row:
$query->where('email', '=', $user->email)->where('card_uid', '=', $k)
->update( array('email' => $user->email, 'card_uid' => $k, 'have_quantity' => ($card->have_quantity+$v)) );
I'd like to retrieve the updated (or inserted) row. I tried the following but it doesn't work:
$row = $query->where('email', '=', $user->email)->where('card_uid', '=', $k)
->update( array('email' => $user->email, 'card_uid' => $k, 'have_quantity' => ($card->have_quantity+$v)) )
->get();
How can I retrieve the row I am updating or inserting easily?
You can try using the same query builder instance for the update first and then run the select query. (You can't do it the way you tried because update() returns the number of affected rows)
$query->where('email', '=', $user->email)->where('card_uid', '=', $k);
$query->update(array('email' => $user->email, 'card_uid' => $k, 'have_quantity' => ($card->have_quantity+$v)))
$row = $query->get();