-2

I am running this command for model, migration, resource controller.

`php artisan make:model QuestionAnswer -mc -r` ..

Laravel give me in Resource Controller

public function show(QuestionAnswer $questionAnswer) {
        // return $questionAnswer;
 }
public function edit(QuestionAnswer $questionAnswer) {
    // return $questionAnswer;
}
public function update(Request $request, QuestionAnswer $questionAnswer){
  // return $questionAnswer;
}

if I write in web.php
Route::resource('question-answer','QuestionAnswerController'); or Route::resource('questionAnswer','QuestionAnswerController'); or Route::resource('question_answer','QuestionAnswerController'); laravel resolve route model binding... that means....

Example as a

public function edit(QuestionAnswer $questionAnswer)
    {
        return $questionAnswer;  
    }

$questionAnswer return object for this url {{route('admin.question-answer.edit',$questionAnswer->id)}}

but if I write in web.php Route::resource('faq','QuestionAnswerController'); laravel can not resolve route model binding...

that means.. $questionAnswer return null for this url {{route('admin.faq.edit',$questionAnswer->id)}}

also in show and update function $questionAnswer; return null...

for working as a faq url.. i need change in edit function variable($faq) . or Route::resource('faq','QuestionAnswerController')->parameters(['faq' => 'questionAnswer']);I

But These three url questionAnswer,question-answer,question_answer by default work...

I check it on "laravel/framework": "^6.0" (LTS)

Question

is there possible way to find out what exact url I will write ? .. like question-answer.. or is there any command line ...

after running auth command .. php artisan route:list command give us all route list.. and when I make model Category laravel create table name categories and follow grammar rules

نور
  • 1,425
  • 2
  • 22
  • 38

3 Answers3

1

How will I know I need to write question-answer this ? by default it works... when i write faq i need to change in edit function variable($faq) .

How will I know by default url (question-answer) will work ..when php artisan route:list command give us all route list.. and when I make model Category laravel create table name categories and follow grammar rules

think about i will create 20 model ,migration & controller by cmd... i will not change edit,show and update function variable ...how i will know the default url for 20 model and controller ? Laravel is an opinionated framework. It follows certain conventions

Let us understand a route parts

Route::match(
    ['PUT', 'PATCH'], 
    '/question-answer/{questionAnswer}',
    [QuestionAnswerController::class, 'update']
)->name('question-answers.update')

In the above route:

1st argument: ['PUT', 'PATCH'] are the methods which the route will try to match for an incoming request

2nd argument: '/question-answer/{questionAnswer}' is the url wherein

/question-answer is say the resource name and

{questionAnswer} is the route parameter name

3rd argument: [QuestionAnswerController::class, 'update'] is the controller and the action/method which will be responsible to handle the request and provide a response

When you create a model via terminal using

php artisan make:model QuestionAnswer -mc -r

It will create a resource controller for the 7 restful actions and take the method parameter name for show, edit, update and delete routes as camel case of the model name i.e. $questionAnswer

class QuestionAnswerController extends Controller
{
    public function show(QuestionAnswer $questionAnswer){}

    public function edit(QuestionAnswer $questionAnswer){}

    public function update(Request $request, QuestionAnswer $questionAnswer){}

    public function delete(QuestionAnswer $questionAnswer){}
}

Which means if you don't intend to change the parameter name in the controller methods then you can define the routes as below to get the benefit of implicit route model binding

//Will generate routes with resource name as questionAnswer
//It would not be considered a good practice

Route::resource('questionAnswer', QuestionAnswerController::class);

//OR

Route::resource('question-answer', QuestionAnswerController::class)->parameters([
    'question-answer' => 'questionAnswer'
]);

//OR

Route::resource('foo-bar', QuestionAnswerController::class)->parameters([
    'foo-bar' => 'questionAnswer'
]);

RFC 3986 defines URLs as case-sensitive for different parts of the URL. Since URLs are case sensitive, keeping it low-key (lower cased) is always safe and considered a good standard.

As you can see, you can name the url resource anything like foo-bar or question-answer instead of questionAnswer and yet keep the route parameter name as questionAnswer to match the Laravel convention when generating controller via php artisan make:model QuestionAnswer -mc -r and without having to change the parameter name in controller methods.

Laravel is an opinionated framework which follows certain conventions:

  • Route parameter name ('questionAnswer') must match the parameter name in controller methods ($questionAnswer) for implicit route model binding to work

  • Controller generated via artisan commands, have parameter name as camelCase of the model name

  • Routes generated via Route::resource('posts', PostController::class) creates routes with resource name equal to the first argument of the method and route parameter name as the singular of the first argument

  • Route::resource() provides flexibility to use a different name for route resource name and route parameter name

Read more at Laravel docs:

If you want to know how the php artisan make:model works you can study the code in vendor/laravel/framework/src/Illuminate/Foundation/Console/ModelMakeCommand.php and have a look at the various stubs these commands use to generate the files.

For almost all artisan commands you will find the class files with code in

vendor/laravel/framework/src/Illuminate/Foundation/Console and the stubs used by the commands to generate files in vendor/laravel/framework/src/Illuminate/Foundation/Console/stubs folder.

If you study these command classes properly then you will get an idea of the various conventions Laravel follows when generating files via the artisan commands

Donkarnash
  • 12,433
  • 5
  • 26
  • 37
  • what is my question ? – نور Nov 26 '20 at 08:18
  • 1
    You can define the urls for your application with any words you like doesn't matter. like you could define the route as `/foo/{question}`. The main thing to get route model binding working is that the method parameter name in the controller method should match name of the route parameter you use in the route declaration – Donkarnash Nov 26 '20 at 08:22
  • 1
    When you use `Route::resource()` to define your routes Laravel follows a convention as I stated above to define the routes. – Donkarnash Nov 26 '20 at 08:24
  • @noor I have updated my answer for your two scenarios – Donkarnash Nov 26 '20 at 08:38
  • I attach a question link .. your answer already there ... please try to understand my question .. what I am looking for – نور Nov 28 '20 at 16:38
  • i read this doc...it's about "singularized" version of the resource name. – نور Nov 28 '20 at 17:07
  • Hope my updates above helps solve your queries – Donkarnash Nov 28 '20 at 17:09
  • i am not looking for solution what you provide.. i think you skip my question goal ...i glad for your support _question?_ is there possible way to find out what exact url I will write ? think about i will create 20 `model` ,`migration` & `controller` by cmd... i will not change `edit`,`show` and `update` function variable ...how i will know the default url for 20 `model` and `controller` ? – نور Nov 28 '20 at 17:16
  • "create 20 model by cmd" means using php artisan make:model command? – Donkarnash Nov 28 '20 at 17:21
  • please try this `php artisan make:model QuestionAnswer -mc -r` ... then see what laravel give you ... – نور Nov 28 '20 at 17:22
  • @noor Have updated my answer. Please have a look and let me know if I understood what you are asking correctly – Donkarnash Nov 28 '20 at 17:43
  • why are you putting same thing again and again ? – نور Nov 28 '20 at 17:52
  • When you generate models, migrations and controllers with `php artisan make:model QuestionAnswer -mc -r` you will have to define still define routes. Which you can do with `Route::resource()` and know that routes will be generated by Laravel as per its convention or you have to manually define routes individually. When defining manually you are not forced to use laravel convention you can have whatever url you want. Except if you want route model binding then controller method parameter **must** match the route parameter by name. – Donkarnash Nov 28 '20 at 17:55
  • " i will not change edit,show and update function variable " then you just have to ensure that route parameter name follows laravel convention - i.e. model name | Model: QuestionAnswer => Route parameter name: {questionAnswer} . Route resource name can be anything '/foobar/{questionAnswer}/edit' will also work for that matter – Donkarnash Nov 28 '20 at 18:34
  • i can achieve through (`parameters(['faq' => 'questionAnswer'])`) this .. this answer already there [attach question linke](https://stackoverflow.com/questions/64999075/laravel-resource-url-depend-on-model)...maybe i am not good speaker... my question is How i will identify resource default url in laravel ? `question -asnwer` is default but `faq` not working by default. thanks for support and time – نور Nov 29 '20 at 04:21
  • you write about two step for _following Laravel convention_ ..from the question `question-answer` url by default work.. that means `foobar` also work for `foo-bar` url .. you skip about it ...would you can put source link to study or clarity .... – نور Nov 30 '20 at 08:36
1

Actually Laravel has got Naming Convection Rules In its core.

These Convictions make it to default binding database_tables to model, model to controllers ....

if you want, you can tell your custom parameters but if you dont, The Laravel uses its own default and searching for them.

for example: if you have a model named bar, laravel look for a table named plural bars . and if you dont want this behave, you can change this default by overriding the *Models* $table_name` attribute for your custom parameter.

There are some Name Convection Rules Like :

  • tables are plural and models are singular : its not always adding s (es) in trailing. sometimes it acts more complicate. like : model:person -> table: people

  • pivot table name are seperate with underline and should be alphabetic order: pivot between fooes and bars table should be bar_foo (not foo_bar)

  • table primary key for Eloquent find or other related fucntion suppose to be singular_name_id : for people table : person_id

  • if there are two-words name in model attribute, all of these are Alias : oneTwo === one_two == one-two

check this out:

class Example extends Model{

 public function getFooBarAttribute(){
    return "hello";
}
}

all of this return "hello" :

 $example = new Example();
 $example->foo_bar();
 $example->fooBar();
 // $example->foo-bar() is not working because - could be result of lexical minus

there is a page listing laravel naming conventions :

https://webdevetc.com/blog/laravel-naming-conventions/

Name Conventions : is The Language Between The Laravel and Developer it made it easy to not to explicitly mention everything like Natural Language we can eliminate when we think its obvious. or we can mention if its not (like ->parameter(...)).

Abilogos
  • 4,777
  • 2
  • 19
  • 39
  • 1
    i will get back and complete the answer in an hour – Abilogos Nov 30 '20 at 12:04
  • 2
    firstly thanks for answer ...go providers folder .. then `RouteServiceProvider.php` then `use Illuminate\Routing\Router;` .. in `Router.php` you will find `bind` and `model` function... i think it will help you to resolve `$example->foo-bar() ` this for `one-two` url... – نور Nov 30 '20 at 12:38
-2

I think its becuse of dependency injection which you used in youre method.

Try this

public function edit($id)
    {
        // return $questionAnswer;
        return view('backend.faq.edit',get_defined_vars());
    }
Masoud
  • 1,099
  • 1
  • 10
  • 22