Skip to content

Commit

Permalink
用户可以编辑话题
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 20, 2019
1 parent be0f048 commit b9fbce2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
21 changes: 11 additions & 10 deletions app/Http/Controllers/TopicsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,20 @@ public function store(TopicRequest $request, Topic $topic)
return redirect()->route('topics.show', $topic->id)->with('success', '帖子创建成功!');
}

public function edit(Topic $topic)
{
public function edit(Topic $topic)
{
$this->authorize('update', $topic);
return view('topics.create_and_edit', compact('topic'));
}
$categories = Category::all();
return view('topics.create_and_edit', compact('topic', 'categories'));
}

public function update(TopicRequest $request, Topic $topic)
{
$this->authorize('update', $topic);
$topic->update($request->all());
public function update(TopicRequest $request, Topic $topic)
{
$this->authorize('update', $topic);
$topic->update($request->all());

return redirect()->route('topics.show', $topic->id)->with('message', 'Updated successfully.');
}
return redirect()->route('topics.show', $topic->id)->with('success', '更新成功!');
}

public function destroy(Topic $topic)
{
Expand Down
3 changes: 1 addition & 2 deletions app/Policies/TopicPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class TopicPolicy extends Policy
{
public function update(User $user, Topic $topic)
{
// return $topic->user_id == $user->id;
return true;
return $topic->user_id == $user->id;
}

public function destroy(User $user, Topic $topic)
Expand Down
10 changes: 6 additions & 4 deletions resources/views/topics/create_and_edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@

<div class="form-group">
<select class="form-control" name="category_id" required>
<option value="" hidden disabled selected>请选择分类</option>
@foreach ($categories as $value)
<option value="{{ $value->id }}">{{ $value->name }}</option>
@endforeach
<option value="" hidden disabled {{ $topic->id ? '' : 'selected' }}>请选择分类</option>
@foreach ($categories as $value)
<option value="{{ $value->id }}" {{ $topic->category_id == $value->id ? 'selected' : '' }}>
{{ $value->name }}
</option>
@endforeach
</select>
</div>

Expand Down

0 comments on commit b9fbce2

Please sign in to comment.