Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

php - How to remove id from http://localhost:8000/posts/2/edit when editing a data

I have a view that show some data and have edit feature, but when clicked edit button, user will be redirected to http://localhost:8000/posts/2/edit, I dont want post id appear in the URL, what should I do?

<table class="table table-bordered">
    <tr>
        <th width="20px" class="text-center">No</th>
        <th>Title</th>
        <th>Content</th>
      
        <th width="280px" class="text-center">Action</th>
    </tr>
    @foreach ($posts as $post)
    <tr>
        <td class="text-center">{{ ++$i }}</td>
        <td>{{ $post->title }}</td>
        <td>{{ $post->content }}</td>
       

        <td class="text-center">
            <form action="{{ route('posts.destroy',$post->id) }}" method="POST">

                <a class="btn btn-info btn-sm" href="{{ route('posts.show',$post->id) }}">Show</a>

                <a class="btn btn-primary btn-sm" href="{{ route('posts.edit',$post->id) }}">Edit</a>

                @csrf
                @method('DELETE')

                <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Delete?')">Delete</button>
            </form>
        </td>
    </tr>
    @endforeach
</table>

here is the controller

public function edit(Post $post)
    {
        return view('posts.edit', compact('post'));
    }

here is the route

   Route::get('/', function () {
        return view('welcome');
    });
        
    Route::resource('posts', AppHttpControllersPostController::class);
    Auth::routes();

Thanks in advance

question from:https://stackoverflow.com/questions/65950441/how-to-remove-id-from-http-localhost8000-posts-2-edit-when-editing-a-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to prevent users to edit posts that they did not create:

public function edit(Post $post)
    {
        if(Auth::user()->id != $post->user_id) {
           throw new Exception("Access denied", 403); 
        }
        return view('posts.edit', compact('post'));
    }

you need to use the Auth class offcourse:

use IlluminateSupportFacadesAuth;

You need to do the same for the delete function


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...