在Laravel中显示特定用户的帖子

时间:2019-06-14 18:53:59

标签: laravel-5

我对显示特定用户的帖子有疑问。我正在为兄弟公司建立一个基本的保存记录网站。我为普通用户(公司员工)和管理员(公司经理)进行了重定向登录。因此,我是Laravel和编程的新手,为此网站做了所有工作,而我需要做的就是如何在特定用户的管理个人资料上显示帖子。在管理员个人资料上,我将为所有工作人员个人资料创建页面,在特定页面上,我需要显示该特定用户的帖子。我怎样才能做到这一点?制作新的控制器?

<?php

namespace App\Http\Controllers;

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

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',
            'status' => '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->status = $request->input('status');
        $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)
    {
        $this->validate($request, [
            'br_kesice' => 'required',
            'ime' => 'required',
            'br_telefona' => 'required',
            'posao' => 'required',
            'cijena' => 'required',
            'placanje' => 'required',
            'popust' => 'required',
            'datum_preuz' => 'required',
            'smjena' => 'required',
            'radnik' => 'required',
            'status' => 'required'
        ]);

        $post = Post::find($id);
        $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->status = $request->input('status');
        $post->save();



        return redirect('/home');
    }

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

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;


class Post extends Model
{


    protected $table = 'posts';

    public $primaryKey = 'id';

    public $timestamps = true;

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

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Post;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

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

    public function is_admin(){
        if($this->admin)
        {
            return true;
        }

        return false;
    }
}

0 个答案:

没有答案