In your routes file, you pass the item Route::post('items/{item}/photos', 'ItemsController@addPhoto');
So you need to add $item to the method public function addPhoto($item, Request $request)
So following ProjectFlyer series I'm adding photos to my adverts (my project is slightly different from Jeffreys but same idea).
It works but only for the ->first() item, what I am stumped with is finding current item, I have tried many combinations of things but can't get it going. The commented out lines are along the lines of the things I have been trying.
Here is the code in question, ItemsController:
public function addPhoto(Request $request)
{
$file = $request->file('photo');
//Names file unique name, current time plus file name
$name = time().$file->getClientOriginalName();
//Moves file to resting spot in project
$file->move('items_add/photos', $name);
//Find the current item
//needs to change from first() to 'current()'
$item = Item::first();
//$item = Item::find($item->id);
//$item = Item::findOrFail($item);
//Add photo to current item
$item->photos()->create(['path'=>"/items_add/photos/{$name}"]);
return 'Done';
}
routes.php:
<?php
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', 'PagesController@home');
Route::get('about', 'PagesController@about');
Route::get('/home', 'HomeController@index');
Route::get('items', 'ItemsController@index');
Route::post('items', 'ItemsController@store');
Route::get('items/create', 'ItemsController@create');
Route::get('items/{item}', 'ItemsController@show');
Route::post('items/{item}/photos', 'ItemsController@addPhoto');
});
Thanks in advance
Is it to do with the syntax of the action="..." part? Should it be {{...}} maybe?
UPDATE: Yep that was it, just need double {{..}} around $item->id
action="/items/{{$item->id}}/photos"
Please or to participate in this conversation.