0

I have a Data model that I want people to be able to view individual records of and then edit/add data to. I have managed to get the view route working;

Route::get('/data/{data_token}', 'DataController@show');

Data_token, is a unique string. This then uses this DataController function;

Public function show($data) {
  $data = Data::where('data_token',$data)->first();
  return view('data.show', compact('data'))
}

After which I can display the data on the page, and have a form for editing (actually its for adding data that doesn't exist, but whatever, same principle right).

On the form on the data.show view, I am sending it to a different view;

Route::get('/data/{data_token}/edit', 'DataController@edit');

This can use the $request variable to return the forms values, but I can't relate it to the data row I was previously editing?

how do I get the {data_token} passed to the edit function of the controller?

Edit( adding route files)

Noticed I forgot the {'data_token'} in the post route.

/Begs forgiveness

6
  • please show your route files Commented Mar 13, 2018 at 11:01
  • Route::get('/data/{data_token}/edit', 'DataController@edit'); Commented Mar 13, 2018 at 11:02
  • 1
    please share your full route files, atleast show and edit route Commented Mar 13, 2018 at 11:04
  • In your edit view the data_token is in you url so there is your reference to you original data token? Or am i missunderstanding your question? Commented Mar 13, 2018 at 11:05
  • The exact same way you got your data token in you show method, If I understand the question right. Commented Mar 13, 2018 at 11:17

1 Answer 1

1

I think you've misunderstood how the routes and controllers work. What you're looking at is a fairly simple CRUD setup like the following;

Route::get('/data/{data_token}', 'DataController@show');
Route::get('/data/{data_token}/edit', 'DataController@edit');
Route::post('/data/{data_token}/edit', 'DataController@update');

Now your controller would have;

public function show($dataToken) { ... }
public function edit($dataToken) { ... }
public function update($dataToken, Request $request) { ... }

Then you'd have your form on the edit view like so;

<form action="{{ route('DataController@update') }}" method="post">

Laravels router will always try to pass in the URI variables as arguments to the methods provided. Providing that I have understood what you need, this should suffice.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, doing this I get a Route not defined, if I change the form action to {{ url('/data/{data_token}/edit') }} the page works and I can submit the form but there's no $data_token variable available to me? Am I missing something obvious again?
What does the controller method look like? If you used my code, it's $dataToken
It's alright I think I just got myself spun in circles, the controller didn't have the arguments for data_token.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.