Tchopa's avatar

How to pass through client ID to the store method

Hey all,

I am trying to add some note taking functionality to my web app. I'd like it so that when I click the show button next to a client line, it shows more info about them and their notes underneath. I believe I have the indexing and most of the routes sorted, but when I try to store a note, it gives me a $client variable not found error.

How do I pass through the show buttons linked client ID to the note store method so that it saves the note with the correct linked client ID.

Controllers: Note -

public function store(Request $request)
    {
     
    Note::create([
        'title' => $request->title,
        'description' => $request->description,
        'category' => $request->category,
        'employee_id' => $request->employee_id,
        'client_id' => $request->client_id,
    ]);

Routes

Route::resource('notes', NoteController::class);
Route::get('/{client}/add-note', [App\Http\Controllers\NoteController::class, 'create'])->name('database.{client}.notes.create');
Route::delete('{client}/notes/{note}',[App\Http\Controllers\NoteController::class, 'destroy'])->name('notes.destroy');
Route::get('{client}/notes/{note}/edit', [App\Http\Controllers\NoteController::class, 'edit'])->name('database.notes.edit');
Route::get('{client}/notes/{note}/show', [App\Http\Controllers\NoteController::class, 'show'])->name('database.notes.show');

Store method route in view

<form action="{{ route('notes.store', ['client_id'=>$client->id]) }}" method="POST">

Thanks in advance.

0 likes
9 replies
Nakov's avatar

I assume you are passing the $client to the page that loads your form you just need to change the client_id to client as that's what is expected in the url:

<form action="{{ route('notes.store', ['client'=>$client->id]) }}" method="POST">

And then accept it in the method

public function store(Request $request, Client $client)
{
     
    Note::create([
        'title' => $request->title,
        'description' => $request->description,
        'category' => $request->category,
        'employee_id' => $request->employee_id,
        'client_id' => $client->id,
    ]);
...

it will be injected for you.

Tchopa's avatar

@Nakov Hey Nakov, thanks for your response.

I made the changes and got the following error

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'client_id' cannot be null

I believe it is because $client->id isn't returning a value.

When I dd($client) it is blank, no attributes.

Nakov's avatar

@Tchopa that's because as I said above you are probably not passing the $client to the page that loads the <form with which you submit a note. Make sure when you return that view you pass the client.

Tchopa's avatar

@Nakov Sorry can you please elaborate a little more on how I can do that. The method that stores the information is a resource route, is that what's causing the issue?

newbie360's avatar

@Tchopa normaly the store route in resource route is without param

you can check with this command

php artisan route:list --name=store

when you inspect the form in browser, you will see there has the route param, but your store route doesn't passing the param

so you need change the store route in resource route, add the param

Nakov's avatar

@Tchopa that's definetely a problem.. I thought you had the client in the url.

then you can add a hidden input in your form:

<input name="client_id" value="{{ $client->id }}" />

and then as you had it initially should do it

public function store(Request $request)
    {
     
    Note::create([
        'title' => $request->title,
        'description' => $request->description,
        'category' => $request->category,
        'employee_id' => $request->employee_id,
        'client_id' => $request->client_id,
    ]);

and you don't need to add it in the url:

<form action="{{ route('notes.store']) }}" method="POST">
1 like
Tchopa's avatar

@Nakov Great. That's working better, and now I'm only getting this error.

I think it has to do with the routes because I've seen it before but entirely sure how to fix this one considering its a resource route

Missing required parameter for [Route: database.clients.show] [URI: database/clients/{client}/show] [Missing parameter: client].
Nakov's avatar
Nakov
Best Answer
Level 73

@Tchopa so whenever you have {} in the route, you need to pass that parameter from somewhere.. so wherever you are using the form, make sure you pass the $client

{{ route('database.clients.show', $client) }}
1 like

Please or to participate in this conversation.