cameronmd's avatar

Grab current Item in ItemController

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

0 likes
6 replies
Snapey's avatar

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)

cameronmd's avatar

Thanks for the reply @Snapey

I've gone down this route but couldn't get it working so currently I have

public function addPhoto($item, Request $request)
{
    $file = $request->file('photo');

    $name = time().$file->getClientOriginalName();

    $file->move('items_add/photos', $name);

    //Find the current item
    //needs to change from first() to 'current()'
    //$item = Item::first();
    // $item = Item::find(3);
    $item = Item::find($item);
    //$item = Item::find($item->id);
    
    $item->photos()->create(['path'=>"/items_add/photos/{$name}"]);

    return 'Done';
}

However using the current line in place gives me this error:

Fatal error: Call to a member function photos() on null

and points to

    $item->photos()->create(['path'=>"/items_add/photos/{$name}"]);

Is it because im using $item for 2 purposes?

Snapey's avatar

Are you sure you pass item through from the form? Use network tools in your browser to check what URL is called when you post the photo.

Alternatively dd($item, $request); at the top of this function to check that you are getting the item number

cameronmd's avatar

Ok @snapey sorry for the delay in reply. After trying dd($item, $request); It returns: "{$item->id}"

So the item number is not getting there in the first place. So I go to my show.blade.php where the drop photo thing is and I have this

    <form id="addPhotosForm" action="/items/{$item->id}/photos" method="POST" class="dropzone">
        
        {{ csrf_field() }}    

    </form>

Where would I ensure I'm passing item through the form?

cameronmd's avatar
cameronmd
OP
Best Answer
Level 1

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"
Snapey's avatar

You need double braces for blade to insert the value and not just the string.

Please or to participate in this conversation.