Laravel审核仅显示已接受的帖子

时间:2019-07-11 11:42:38

标签: laravel

我在当前项目中安装了hootlex-moderation软件包,并且工作正常。但是问题在于,它只显示已接受的帖子,我在我过去的项目中找到了解决方案,并且将“严格”审核设置为false,但这一次不是解决方案。你们知道如何解决这个问题吗?

我的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Post;
use App\User;

class PostsController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $post = Post::all();
        return view('posts.tabela')->with('posts', $post);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        return view('posts.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'br_kesice' => 'required',
            'ime' => 'required',
            'br_telefona' => 'required',
            'posao' => 'required',
            'cijena' => 'required',
            'placanje' => 'required',
            'popust' => 'required',
            'datum_preuz' => 'required',
            'datum_izdav' => 'required',
            'smjena' => 'required',
            'radnik' => 'required',

        ]);

        $post = new Post;
        $post->br_kesice = $request->input('br_kesice');
        $post->ime = $request->input('ime');
        $post->br_telefona = $request->input('br_telefona');
        $post->posao = $request->input('posao');
        $post->cijena = $request->input('cijena');
        $post->placanje = $request->input('placanje');
        $post->popust = $request->input('popust');
        $post->datum_preuz = $request->input('datum_preuz');
        $post->datum_izdav = $request->input('datum_izdav');
        $post->smjena = $request->input('smjena');
        $post->radnik = $request->input('radnik');

        $post->user_id = auth()->user()->id;
        $post->save();

        return redirect('/home');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $post = Post::find($id);

        if(auth()->user()->id !==$post->user_id){
            return redirect('/posts')->with('error', 'Nedozvoljen pristup!');
        }
        return view('posts.edit', compact('post', 'id'))->with('post', $post);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        switch($request->get('approve'))
        {
            case 0:
                Post::postpone($id);
                break;
            case 1:
                Post::approve($id);
                break;
            case 2:
                Post::reject($id);
                break;
            case 3:
                Post::postpone($id);
                break;
            default:    
                break;

        }





        return redirect('/home');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

moderation.php文件

<?php
return array(
    /*
    |--------------------------------------------------------------------------
    | Status column
    |--------------------------------------------------------------------------
    */
    'status_column' => 'status',

    /*
    |--------------------------------------------------------------------------
    | Moderated At column
    |--------------------------------------------------------------------------
    */
    'moderated_at_column' => 'moderated_at',

    /*
    |--------------------------------------------------------------------------
    | Moderated By column
    |--------------------------------------------------------------------------
    | Moderated by column is disabled by default.
    | If you want to include the id of the user who moderated a resource set
    | here the name of the column.
    | REMEMBER to migrate the database to add this column.
    */
    'moderated_by_column' => null,

    /*
    |--------------------------------------------------------------------------
    | Strict Moderation
    |--------------------------------------------------------------------------
    | If Strict Moderation is set to true then the default query will return
    | only approved resources.
    | In other case, all resources except Rejected ones, will returned as well.
    */
    'strict' => false,


);

我的迁移

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('br_kesice');
            $table->string('ime');
            $table->string('br_telefona');
            $table->mediumText('posao');
            $table->string('cijena');
            $table->string('placanje');
            $table->string('popust');
            $table->string('datum_preuz');
            $table->string('datum_izdav');
            $table->string('smjena');
            $table->string('radnik');
            $table->smallInteger('status')->default(1);
            $table->dateTime('moderated_at')->nullable();
            $table->timestamps();
        });
    }

这是我的编辑刀片

<select name="approve">
                <option value="0" @if($post->status==0)selected @endif>Pending</option>
                <option value="1" @if($post->status==1)selected @endif>Approve</option>
                <option value="2" @if($post->status==2)selected @endif>Reject</option>
                <option value="3" @if($post->status==3)selected @endif>Postponed</option> 
</select>

2 个答案:

答案 0 :(得分:0)

<?php

namespace App;
use Hootlex\Moderation\Moderatable;
use Illuminate\Database\Eloquent\Model;
use App\User;


class Post extends Model
{

   use Moderatable;

    protected $table = 'posts';

    public $primaryKey = 'id';

    public $timestamps = true;

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

答案 1 :(得分:0)

这是我的HomeController,我将其设置为当前登录用户的页面。这是我的代码。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Post;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {

        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        return view('home')->with('posts', $user->posts);
    }


}

当我添加Post :: withAnyStatus()-> get();像这样...

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Post;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {

        $posts = Post::withAnyStatus()->get();
        return view('home')->with('posts', $posts);
    }


}

它显示所有帖子,并且正在运行,但是我只需要显示当前登录的那个user_id的帖子。对此有任何解决方案吗?