Reputation: 10886
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
Upvotes: 3
Views: 632
Reputation: 19275
You can pass the parameters to the action
function, as a second array parameter:
<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
EDIT 2
You have not specified your relations correctly: as long as you don't use laravel conventions for tables' field names, you have to specify the foreign keys and primary keys in your relations:
Ticket
public function images()
{
return $this->hasMany('App\image', 'ticket_ticketid', 'ticketid' );
}
Image
public function ticket()
{
return $this->belongsTo('App\ticket', 'ticket_ticketid', 'ticketid');
}
Once you have built a $ticket
model you can retrieve the images with:
$images = $ticket->images();
Or:
$images = Ticket::find($ticket_id)->images();
Upvotes: 1