sort posts that have the largest number of similar tags












2















I know that the following code can select each post in Post model that has at least one of tags by id:1,2,3



$ids = [1,2,3];
$posts = Post::with('tags')->whereHas('tags', function ($query) use ($ids) {
$query = $query->whereIn('id', $ids);
})->get();


output:



Collection {
#items: array:3 [
0 => Post {
...
#attributes: array: [
'id' => 1,
'title' => 'title',
'content' => 'content'
]
...
#relations: array:1 [▼
"tags" => Collection {#828 ▼
#items: array:3 [▼
0 => Tag {#806 ▶}
1 => Tag {#807 ▶}
2 => Tag {#808 ▶}
]
}
]
...


Now, My question is how can I sort this posts that have the largest number of similar tags. I mean, posts that have more similar tags in my $ids have more priority, first posts that have all 3 tags then 2 and finally 1 of the tags in my list?










share|improve this question

























  • Can you please post your output?

    – Sachin Aghera
    Nov 22 '18 at 6:11











  • this a list of a collection that has at least one of tag ids

    – Kamran_dev
    Nov 22 '18 at 6:24











  • $collection = collect([ ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $sorted = $collection->sortBy(function ($product, $key) { return count($product['colors']); }); dd($sorted->values()->all());

    – Sachin Aghera
    Nov 22 '18 at 6:26











  • May be help you above example

    – Sachin Aghera
    Nov 22 '18 at 6:27











  • @SachinAghera - You should post it as an answer.

    – Magnus Eriksson
    Nov 22 '18 at 6:27
















2















I know that the following code can select each post in Post model that has at least one of tags by id:1,2,3



$ids = [1,2,3];
$posts = Post::with('tags')->whereHas('tags', function ($query) use ($ids) {
$query = $query->whereIn('id', $ids);
})->get();


output:



Collection {
#items: array:3 [
0 => Post {
...
#attributes: array: [
'id' => 1,
'title' => 'title',
'content' => 'content'
]
...
#relations: array:1 [▼
"tags" => Collection {#828 ▼
#items: array:3 [▼
0 => Tag {#806 ▶}
1 => Tag {#807 ▶}
2 => Tag {#808 ▶}
]
}
]
...


Now, My question is how can I sort this posts that have the largest number of similar tags. I mean, posts that have more similar tags in my $ids have more priority, first posts that have all 3 tags then 2 and finally 1 of the tags in my list?










share|improve this question

























  • Can you please post your output?

    – Sachin Aghera
    Nov 22 '18 at 6:11











  • this a list of a collection that has at least one of tag ids

    – Kamran_dev
    Nov 22 '18 at 6:24











  • $collection = collect([ ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $sorted = $collection->sortBy(function ($product, $key) { return count($product['colors']); }); dd($sorted->values()->all());

    – Sachin Aghera
    Nov 22 '18 at 6:26











  • May be help you above example

    – Sachin Aghera
    Nov 22 '18 at 6:27











  • @SachinAghera - You should post it as an answer.

    – Magnus Eriksson
    Nov 22 '18 at 6:27














2












2








2


3






I know that the following code can select each post in Post model that has at least one of tags by id:1,2,3



$ids = [1,2,3];
$posts = Post::with('tags')->whereHas('tags', function ($query) use ($ids) {
$query = $query->whereIn('id', $ids);
})->get();


output:



Collection {
#items: array:3 [
0 => Post {
...
#attributes: array: [
'id' => 1,
'title' => 'title',
'content' => 'content'
]
...
#relations: array:1 [▼
"tags" => Collection {#828 ▼
#items: array:3 [▼
0 => Tag {#806 ▶}
1 => Tag {#807 ▶}
2 => Tag {#808 ▶}
]
}
]
...


Now, My question is how can I sort this posts that have the largest number of similar tags. I mean, posts that have more similar tags in my $ids have more priority, first posts that have all 3 tags then 2 and finally 1 of the tags in my list?










share|improve this question
















I know that the following code can select each post in Post model that has at least one of tags by id:1,2,3



$ids = [1,2,3];
$posts = Post::with('tags')->whereHas('tags', function ($query) use ($ids) {
$query = $query->whereIn('id', $ids);
})->get();


output:



Collection {
#items: array:3 [
0 => Post {
...
#attributes: array: [
'id' => 1,
'title' => 'title',
'content' => 'content'
]
...
#relations: array:1 [▼
"tags" => Collection {#828 ▼
#items: array:3 [▼
0 => Tag {#806 ▶}
1 => Tag {#807 ▶}
2 => Tag {#808 ▶}
]
}
]
...


Now, My question is how can I sort this posts that have the largest number of similar tags. I mean, posts that have more similar tags in my $ids have more priority, first posts that have all 3 tags then 2 and finally 1 of the tags in my list?







php mysql laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:21







Kamran_dev

















asked Nov 22 '18 at 6:03









Kamran_devKamran_dev

14310




14310













  • Can you please post your output?

    – Sachin Aghera
    Nov 22 '18 at 6:11











  • this a list of a collection that has at least one of tag ids

    – Kamran_dev
    Nov 22 '18 at 6:24











  • $collection = collect([ ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $sorted = $collection->sortBy(function ($product, $key) { return count($product['colors']); }); dd($sorted->values()->all());

    – Sachin Aghera
    Nov 22 '18 at 6:26











  • May be help you above example

    – Sachin Aghera
    Nov 22 '18 at 6:27











  • @SachinAghera - You should post it as an answer.

    – Magnus Eriksson
    Nov 22 '18 at 6:27



















  • Can you please post your output?

    – Sachin Aghera
    Nov 22 '18 at 6:11











  • this a list of a collection that has at least one of tag ids

    – Kamran_dev
    Nov 22 '18 at 6:24











  • $collection = collect([ ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $sorted = $collection->sortBy(function ($product, $key) { return count($product['colors']); }); dd($sorted->values()->all());

    – Sachin Aghera
    Nov 22 '18 at 6:26











  • May be help you above example

    – Sachin Aghera
    Nov 22 '18 at 6:27











  • @SachinAghera - You should post it as an answer.

    – Magnus Eriksson
    Nov 22 '18 at 6:27

















Can you please post your output?

– Sachin Aghera
Nov 22 '18 at 6:11





Can you please post your output?

– Sachin Aghera
Nov 22 '18 at 6:11













this a list of a collection that has at least one of tag ids

– Kamran_dev
Nov 22 '18 at 6:24





this a list of a collection that has at least one of tag ids

– Kamran_dev
Nov 22 '18 at 6:24













$collection = collect([ ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $sorted = $collection->sortBy(function ($product, $key) { return count($product['colors']); }); dd($sorted->values()->all());

– Sachin Aghera
Nov 22 '18 at 6:26





$collection = collect([ ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $sorted = $collection->sortBy(function ($product, $key) { return count($product['colors']); }); dd($sorted->values()->all());

– Sachin Aghera
Nov 22 '18 at 6:26













May be help you above example

– Sachin Aghera
Nov 22 '18 at 6:27





May be help you above example

– Sachin Aghera
Nov 22 '18 at 6:27













@SachinAghera - You should post it as an answer.

– Magnus Eriksson
Nov 22 '18 at 6:27





@SachinAghera - You should post it as an answer.

– Magnus Eriksson
Nov 22 '18 at 6:27












1 Answer
1






active

oldest

votes


















0














$collection = collect([
['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],
['name' => 'Chair', 'colors' => ['Black']],
['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],
]);

$sorted = $collection->sortBy(function ($product, $key) {
return count($product['colors']);
});

dd($sorted->values()->all());





share|improve this answer
























  • thank u, But If you look at my code Post model have m2m relation to Tags, and I haven't such form of data

    – Kamran_dev
    Nov 22 '18 at 6:50











  • So can you post your output?

    – Sachin Aghera
    Nov 22 '18 at 6:55











  • It is a little complicating, I want posts that have more similar tags in my $ids have more priority but by your answer posts that have more tag become earlier not necessarily more similar tags in my $ids ...

    – Kamran_dev
    Nov 22 '18 at 7:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424762%2fsort-posts-that-have-the-largest-number-of-similar-tags%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














$collection = collect([
['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],
['name' => 'Chair', 'colors' => ['Black']],
['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],
]);

$sorted = $collection->sortBy(function ($product, $key) {
return count($product['colors']);
});

dd($sorted->values()->all());





share|improve this answer
























  • thank u, But If you look at my code Post model have m2m relation to Tags, and I haven't such form of data

    – Kamran_dev
    Nov 22 '18 at 6:50











  • So can you post your output?

    – Sachin Aghera
    Nov 22 '18 at 6:55











  • It is a little complicating, I want posts that have more similar tags in my $ids have more priority but by your answer posts that have more tag become earlier not necessarily more similar tags in my $ids ...

    – Kamran_dev
    Nov 22 '18 at 7:40
















0














$collection = collect([
['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],
['name' => 'Chair', 'colors' => ['Black']],
['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],
]);

$sorted = $collection->sortBy(function ($product, $key) {
return count($product['colors']);
});

dd($sorted->values()->all());





share|improve this answer
























  • thank u, But If you look at my code Post model have m2m relation to Tags, and I haven't such form of data

    – Kamran_dev
    Nov 22 '18 at 6:50











  • So can you post your output?

    – Sachin Aghera
    Nov 22 '18 at 6:55











  • It is a little complicating, I want posts that have more similar tags in my $ids have more priority but by your answer posts that have more tag become earlier not necessarily more similar tags in my $ids ...

    – Kamran_dev
    Nov 22 '18 at 7:40














0












0








0







$collection = collect([
['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],
['name' => 'Chair', 'colors' => ['Black']],
['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],
]);

$sorted = $collection->sortBy(function ($product, $key) {
return count($product['colors']);
});

dd($sorted->values()->all());





share|improve this answer













$collection = collect([
['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],
['name' => 'Chair', 'colors' => ['Black']],
['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],
]);

$sorted = $collection->sortBy(function ($product, $key) {
return count($product['colors']);
});

dd($sorted->values()->all());






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 6:29









Sachin AgheraSachin Aghera

39027




39027













  • thank u, But If you look at my code Post model have m2m relation to Tags, and I haven't such form of data

    – Kamran_dev
    Nov 22 '18 at 6:50











  • So can you post your output?

    – Sachin Aghera
    Nov 22 '18 at 6:55











  • It is a little complicating, I want posts that have more similar tags in my $ids have more priority but by your answer posts that have more tag become earlier not necessarily more similar tags in my $ids ...

    – Kamran_dev
    Nov 22 '18 at 7:40



















  • thank u, But If you look at my code Post model have m2m relation to Tags, and I haven't such form of data

    – Kamran_dev
    Nov 22 '18 at 6:50











  • So can you post your output?

    – Sachin Aghera
    Nov 22 '18 at 6:55











  • It is a little complicating, I want posts that have more similar tags in my $ids have more priority but by your answer posts that have more tag become earlier not necessarily more similar tags in my $ids ...

    – Kamran_dev
    Nov 22 '18 at 7:40

















thank u, But If you look at my code Post model have m2m relation to Tags, and I haven't such form of data

– Kamran_dev
Nov 22 '18 at 6:50





thank u, But If you look at my code Post model have m2m relation to Tags, and I haven't such form of data

– Kamran_dev
Nov 22 '18 at 6:50













So can you post your output?

– Sachin Aghera
Nov 22 '18 at 6:55





So can you post your output?

– Sachin Aghera
Nov 22 '18 at 6:55













It is a little complicating, I want posts that have more similar tags in my $ids have more priority but by your answer posts that have more tag become earlier not necessarily more similar tags in my $ids ...

– Kamran_dev
Nov 22 '18 at 7:40





It is a little complicating, I want posts that have more similar tags in my $ids have more priority but by your answer posts that have more tag become earlier not necessarily more similar tags in my $ids ...

– Kamran_dev
Nov 22 '18 at 7:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424762%2fsort-posts-that-have-the-largest-number-of-similar-tags%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin