jamie jamie - 4 years ago 217
PHP Question

Laravel remove files

I'm building an upload page in my application. But how
can I remove a file in Laravel?

I've got this in my view:

<a href="{!! action('AdminTicketsController@deleteTicket',$ticket->id,$ticket->image) !!}" class="btn btn-dark">Remove ticket</a>


With
$ticket->image
I give all paths that the ticket has. This is my route where I receive the post request:

Route::post('admin/ticket/remove','AdminTicketsController@deleteTicket');


But when I die and dump
$request->image
in my controller, I don't see the paths of that ticket?

What's the proper way of doing this?

EDIT:

When I say:

<a href="{!! action('AdminTicketsController@deleteTicket',['id' => $ticket->id, 'image' => $ticket->image()->path ]) !!}" class="btn btn-dark">Remove ticket</a>


It's telling me:


Undefined property:
Illuminate\Database\Eloquent\Relations\HasMany::$path
(View:/home/vagrant/Code/support/local/resources/views/admin/ticket.blade.php)


In my ticket model I'm saying:

public function image()
{
return $this->hasMany('App\image');
}


and in my image model:

public function ticket()
{
return $this->belongsTo('App\ticket');
}


DATABASE:

TICKET
ticketid
ticketname
IMAGE
imageid
ticket_ticketid
path

Answer Source

Change your link to:

<a href="{!! action('AdminTicketsController@deleteTicket',['id' => $ticket->id, 'image' => $ticket->image ]) !!}" class="btn btn-dark">Remove ticket</a>

And in controller:

public function deleteTicket(Request $request, $id, $image)
{
    //use ticket_image
}

EDIT

I suggest not to pass the image path as a parameter: pass only the ticket ID, then retrieve the path in the controller from the ticket model; for the error you should be sure that your relations are set correctly

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download