Wednesday, March 15, 2017

Laravel 5.4 Destroy post function doing it wrong

I'm working on a news thread on laravel 5.4 and my problem is that whenever a user want to delete a post, if there is a post that has the same id as him, it will delete it instead of the post i originally wanted to destroy. But if there is not, he can delete his post without a problem. For exemple, my user has an id '4' and want to delete his post which has an id of '12', the first time i click on my delete link it will delete the post with the id of '4', when this one doesn't exist anymore, he can destroy the post with the id of '12'.

The function in the controller

/**
 * Remove the specified resource from storage.
 *
 * @param  \App\StartUpPost  $startUpPost
 * @return \Illuminate\Http\Response
 */
public function destroy(StartUpPost $startUpPost)
{
    $post = StartUpPost::find($startUpPost);
    $post->delete();

    return redirect('startup/home') -> with('success', 'Post deleted !');
}

The route

Route::get('/StartUpPost/{startUpPost}/delete',[
'uses' => 'StartUpPostController@destroy',
'as'=>'startUpPost.delete'
]);

The link in HTML

<a href="">Delete</a>

The model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class StartUpPost extends Model
{
protected $table = 'start_up_posts';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'content','startup_id','image',
];

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

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

Thanks for you help.



via wllmwng

Advertisement