HOW THE ROUTE CUSTOM KEYS WORK WITH SCOPING IN LARAVEL

Nov 27, 2023 Copy Link

ุงู„ุณู„ุงู… ุนู„ูŠูƒู…, ุงู‡ู„ุงู‹ ุจูŠูƒูˆุง ููŠ ู…ู‚ุงู„ุฉ ุฌุฏูŠุฏุฉ ูˆ ู‡ู†ุชูƒู„ู… ุนู† ุงู„ Custom Keys & Scoping ููŠ ู„ุงุฑููŠู„ ๐Ÿ˜ƒ

 

ุฎู„ูŠู†ูŠ ุงู„ุฃูˆู„ ู‚ุจู„ ู…ุง ุฃุฏุฎู„ ููŠ ู…ูˆุถูˆุนู†ุง ุฃููƒุฑูƒ ุจุดูˆูŠุฉ ุญุงุฌุงุช ู„ุงุฒู… ุชูƒูˆู† ุนุงุฑูู‡ุง ููŠ ู„ุงุฑููŠู„, ูุงุชุนุงู„ูˆุง ูƒุฏุง ู†ุดูˆู ุงู„ุฃูƒูˆุงุฏ ุงู„ู„ูŠ ุฌุงูŠุฉ ุฏูŠ ุจุชู‚ูˆู„ ุงูŠู‡ ๐Ÿง

 

Route::get('/users/{user}', function ($user) {
     return "The user id is: {$user}";
});

 

ู„ูˆ ูƒุชุจู†ุง ุงู„ route ุจุงู„ุดูƒู„ ุฏุง users/5/ ููŠ ุงู„ browser ุงู„ู‚ูŠู…ุฉ ู‡ุชูƒูˆู† The user id is: 5 ูˆ ุฏุง ุงู„ู„ูŠ ู…ูˆุถุญุงู‡ ู„ุงุฑููŠู„ ููŠ ุฌุฒุก ุงู„ Required Parameters ๐Ÿ’ก

 

ุฎู„ูŠู†ุง ู†ุตุนุจ ุงู„ู…ูˆุถูˆุน ุดูˆูŠุฉ ูˆ ู†ุดูˆู ุงู„ูƒูˆุฏ ุงู„ู„ูŠ ุฌุงูŠ ุฏุง ๐Ÿคฏ

 

use App\Models\User;

Route::get('/user/{user}', function (User $user) {
     return "The user name is: {$user->name}";
});

 

ู„ุงุฑููŠู„ ู‡ู†ุง ุจุดูƒู„ ุชู„ู‚ุงุฆูŠ ู‡ุชุฏูˆุฑ ููŠ ุฌุฏูˆู„ ุงู„ users ุจุงู„ id ุงู„ู„ูŠ ุงู†ุช ุนู…ู„ุชู„ู‡ passing, ุจุณ ุฎู„ูŠ ุจุงู„ูƒ ุนุดุงู† ูŠุทุจุนู„ูƒ ุฅุณู… ุงู„ู…ุณุชุฎุฏู… ุจุดูƒู„ ุตุญูŠุญ ู„ุงุฒู… ุงู„ segment name ูŠุจู‚ุง ู†ูุณ ุงู„ type-hinted variable name ูˆ ุฏุง ุฒูŠ ู…ุงู‡ูˆ ูˆุงุถุญ ู‡ู†ุง ููŠ ุฌุฒุก ุงู„ Implicit Binding ๐Ÿ’ก

 

ุทูŠุจ ู‡ูˆ ุฃู†ุง ุนู„ูŠ ุทูˆู„ ู‡ุนู…ู„ passing ู„ุฑู‚ู… ุงู„ id ุงู„ุฎุงุต ุจุงู„ user ู…ูŠู†ูุนุด ุงุนู…ู„ customize ู„ู„ process ุฏูŠ!ุŸ ๐Ÿค”

 

ู„ุงุฑููŠู„ ู‡ุชู‚ูˆู„ูƒ ู„ุฃ ูˆ ู‡ุชู„ุงู‚ูŠ ุงู„ูƒู„ุงู… ุฏุง ู…ุดุฑูˆุญ ููŠ ุฌุฒุก ุงู„ Customizing The Key ๐Ÿ’ก

 

ุชุนุงู„ูˆุง ุจู‚ุง ู†ุดูˆู ู…ูˆุถูˆุน ุงู„ู†ู‡ุงุฑุฏู‡, ูˆ ู‡ูˆ ู„ูˆ ุฃู†ุง ุนู†ุฏูŠ ุงูƒุชุฑ ู…ู† ุนู…ู„ูŠุฉ model binding ุงู„ู…ูˆุถูˆุน ู‡ูŠู…ุดูŠ ุงุฒุงูŠุŸ ๐Ÿค”

 

use App\Models\Post;
use App\Models\User;

Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
     return "The user {$user->name} has this post title {$post->title}";
});

 

ูƒู„ ุงู„ู„ูŠ ู‡ูŠุญุตู„ ู‡ู†ุง ุฅู† ู„ุงุฑููŠู„ ู‡ุชุนู…ู„ search ููŠ ุฌุฏูˆู„ ุงู„ users ุนู† ุงู„ id ุงู„ู„ูŠ ุฅู†ุช ุนู…ู„ุชู„ู‡ passing ูˆ ูƒุฐุง ุงู„ู…ูˆุถูˆุน ู…ุน ุงู„ posts ๐Ÿ‘€

 

ุฎู„ูˆู†ุง ู†ุนุฏู„ ุงู„ูƒูˆุฏ ูƒุฏุง ุดูˆูŠุฉ ูˆ ู†ุฎู„ูŠู‡ ุจุงู„ุดูƒู„ ุงู„ู„ูŠ ุฌุงูŠ ุฏุง ๐Ÿ› 

 

use App\Models\Post;
use App\Models\User;

Route::get('/users/{user}/posts/{post:slug}', function (User $user, Post $post) {
     return "The user {$user->name} has this post title {$post->title}"; 
});

 

ุงู„ูƒูˆุฏ ุงู„ู„ูŠ ูุงุช ุฏุง ู…ุฎุชู„ู ุนู† ุงู„ู„ูŠ ู‚ุจู„ู‡ ููŠ ุฅู†ูŠ ุจุฏู„ ู…ุง ู‡ุนู…ู„ search ููŠ ุฌุฏูˆู„ ุงู„ posts ุจุฅุณุชุฎุฏุงู… ุงู„ id ู‡ู†ุง ู‡ุนู…ู„ search ูŠุฅุณุชุฎุฏุงู… ุงู„ slug column ูˆ ููŠ ุงู„ scenario ุฏุง ู„ุงุฑููŠู„ ู‡ุชูุชุฑุถ ุฅู† ุงู„ User model ููŠู‡ุง ู…ูŠุซูˆุฏ ุฅุณู…ู‡ุง posts ูˆ ุฏูŠ ุนุจุงุฑุฉ ุนู† ุงู„ relationship ุงู„ู„ูŠ ุจุชุฑุจุท ุจูŠู† ุงู„ users ูˆ ุงู„ posts โ›“

 

ูŠุนู†ูŠ ู„ูˆ ูƒุชุจู†ุง ุงู„ route ุจุงู„ุดูƒู„ ุฏุง users/5/posts/how-the-route-custom-keys-work-with-scoping-in-laravel/ ู„ุงุฑููŠู„ ู‡ุชุนู…ู„ search ููŠ ุฌุฏูˆู„ ุงู„ posts ุจุงู„ post ุงู„ู„ูŠ ุนู†ุฏู‡ slug ุจูŠุณุงูˆูŠ ุงู„ู‚ูŠู…ุฉ ุฏูŠ how-the-route-custom-keys-work-with-scoping-in-laravel ูˆ ููŠ ู†ูุณ ุงู„ูˆู‚ุช ูŠูƒูˆู† related ุจุงู„ู…ุณุชุฎุฏู… ุงู„ู„ูŠ ุงู„ id ุงู„ุฎุงุต ุจูŠู‡ ุจูŠุณุงูˆูŠ 5 ๐Ÿ˜Ž

 

ุทูŠุจ ู„ูˆ ุนุงูŠุฒ ุงุณุชุฎุฏู… ุงู„ scenario ุงู„ุฃูˆู„ ู…ู† ุบูŠุฑ ู…ุง ุฃุนู…ู„ customizing ู„ู„ key ูˆ ููŠ ู†ูุณ ุงู„ูˆู‚ุช ูŠูƒูˆู† ุงู„ two models ุจูŠู†ู‡ู… relation ุฃุนู…ู„ ุฅูŠู‡ุŸ ๐Ÿค”

 

ู„ุงุฑููŠู„ ุจุชู‚ูˆู„ูƒ ุฅุณุชุฎุฏุงู… ุงู„ scopeBindings ู…ูŠุซูˆุฏ ๐Ÿ’ก

 

ุทูŠุจ ุนู„ูŠ ู†ูุณ ุงู„ู†ู‡ุฌ ุงู„ู„ูŠ ูุงุช ูˆ ุนุงูŠุฒ ุงุณุชุฎุฏุงู… ุงู„ scenario ุงู„ุชุงู†ูŠ ูˆ ุฃุนู…ู„ customizing ู„ู„ key ูˆ ู†ูุณ ุงู„ูˆู‚ุช ู…ูŠูƒูˆู†ุด ุจูŠู†ู‡ู… relation ุฃุนู…ู„ ุฅูŠู‡ุŸ ๐Ÿค”

 

ู„ุงุฑููŠู„ ุจุชู‚ูˆู„ูƒ ุฅุณุชุฎุฏุงู… ุงู„ withoutScopeBindings ู…ูŠุซูˆุฏ ๐Ÿ’ก

 

ูˆ ุจูƒุฏุง ุฃูƒูˆู† ุฎู„ุตุช ูˆ ุฃุชู…ู†ูŠ ุชูƒูˆู† ุฅุณุชูุฏุช โœ”

 

 ู„ูˆ ููŠ ุญุงุฌุฉ ู…ู† ุงู„ู„ูŠ ู‚ูˆู„ุชู‡ุง ู…ุด ูˆุงุถุญุฉ ุฃูˆ ุตุนุจุฉ ุชู‚ุฏุฑ ุชุณุฆู„ู†ูŠ ู…ู† ุฎู„ุงู„ ุงู„ Discussion Zone ๐Ÿ˜ƒ

Share via

Mahmoud Ramadan

Mahmoud Ramadan

Mahmoud is the creator of Digging Code and a contributor to Laravel since 2020.

Newly published

  • How to Enable Relationship Autoloading in Versions Before v12.8

    How to Enable Relationship Autoloading in Versions Before v12.8

    PREMIUM

  • Get your environment ready to welcome Laravel v12

    Get your environment ready to welcome Laravel v12

    PREMIUM

  • How to generate Arabic PDF using TCPDF

    How to generate Arabic PDF using TCPDF

    FREE