laravel

时间:2018-11-01 13:23:00

标签: php mysql laravel

你好朋友,我试图在laravel中获取当前登录用户的数据,但是我正在获取所有用户的数据,我的意思是我有两个表。一个是用于存储登录用户数据的用户表,另一个是表是发布表,用于存储注册用户的发布结果。我如何获取当前登录用户发布的数据。如何通过user_id foregion键来查找发布和用户。 我的代码在这里给出。

Publication.blade.php

@extends('layouts.app')
@section('content')


<div class="container"><br>
    <h1 class="text-success text-center">Your Publications</h1><br>
    <table  class="table table-bordered">
        <tr class="">
            <th>Publication Title</th>
            <th>Publication Status</th>
            <th>Year</th>
            <th>More Actions</th>
        </tr>
        @foreach($data as $value)
        <tr>
            <td> {{ $value ->title}}</td>
            <td>{{ $value ->status}}</td>
            <td>{{ $value ->year}}</td>
            <td><a href=""><button>Edit</button></a>&nbsp;<a href=""><button>Delete</button></a></td>
        </tr>
        @endforeach

    </table>
    <button class="btn btn-info" data-toggle="collapse" data-target="#demo">Add more publications here</button> <button class="btn btn primary"><a href="home">Go Back to Dashboard</a></button><br>
    <div id="demo" class="collapse">
    <h3 class="text-success text-center">Complete your publication's detail</h3><br>
    <div class="col-md-offset-3 col-md-6 m-auto d-block">
        <form action="pub" method="post">

            <input type="hidden" name="_token" value="{{csrf_token()}}">

            <div>
                <div class="form-group">
                    <label>Title </label>
                    <input type="text" name="title" id="" class="form-control">

                </div>

                <div class="form-group">
                    <label>Conference / General </label>
                    <div class="radio form-control">
                        <label class="col-md-4"><input type="radio" name="status" value="conf" >Conference</label>

                        <label class="col-md-4"><input type="radio" name="status"  value="general">General</label>
                        <label><input type="radio" name="status" value="other" >Other</label>
                        </div>
                </div>

                <div class="form-group">
                    <label>Year: </label>
                    <input type="number" name="year" id="" class="form-control">
                </div>
            </div>

            <div>
                <input type="submit" name="submit" value="ADD!" class="btn btn-lg col-md-offset-3 col-md-6 m-auto d-block">
            </div>
            <input type="hidden" name="_token" value="{{csrf_token()}}">
            </div>

    </div>
    </div><!--end demo-->


    @endsection

我的模型代码在这里publication.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class publication extends Model
{
    //
    protected $fillable = ['user_id','title','status','year'];
}

在此给出了PublicationController.php的代码。请帮助我,问题出在哪里。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\publication;
use Auth;
use DB;

class PublicationController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('publications');
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        publication::create([
            'user_id' => Auth::user()->id,
            'title' => request('title'),
            'status' => request('status'),
            'year' => request('year')
        ]);

        return 'inserted';
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show()
    {
        //
        $data['data'] = DB::table('publications')->get();
        if(count ($data)>0){
        return view('publications',$data);
        }
        else
        {
            return view('publications');
        }
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

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

web.php的代码,我的意思是根。

//for publications
Route::get('publications','PublicationController@index');
Route::post('pub','PublicationController@store');
Route::get('publications','PublicationController@show');

这两个表的用户表和发布表都通过区域键user_id连接,但是我无法获取当前登录用户数据的数据。它为我提供了存储在出版物表格中的所有数据。

3 个答案:

答案 0 :(得分:2)

这可以为您提供帮助

   public function show()
     {
    $id = Auth::user()->id;
    //
    $data['data'] = DB::table('publications')->where('user_id','=', $id)->first();
    if(count ($data)>0){
    return view('publications',compact('data'))
    }
    else
    {
        return view('publications');
    }
}

答案 1 :(得分:0)

您可以获得登录用户的数据。

首先将其包含在类声明上方的控制器中

use Auth;

然后如下所示更改功能show

public function show()
{
    //
    $user_id = Auth::user()->id;
    $data['data'] = DB::table('publications')->where('user_id' ,'=', $user_id)->get();
    if(count ($data)>0){
    return view('publications',compact('data'));
    }
    else
    {
        return view('publications');
    }
}

答案 2 :(得分:0)

您需要通过以下方式更改查询以按用户添加过滤器:

$userId = Auth::user()->id;
$data['data'] = DB::table('publications')->where('user_id', $userId)->get();

您正在使用两条相似的路由,只需删除发送到索引方法的一条。