Data fetching of current login user in laravel

laravel 5.7 login example
laravel login session example
laravel login as user
laravel get current user
laravel get user id in controller
laravel 5.8 login with username
laravel logout user
laravel user management

Hello friends I am trying to fetch the data of current login user in laravel but I am getting the data of all users, I means that I have two tables.One is users tables which stores the data of login users and other table is publications tables, which stores the result of the the publications of the registerd users. How can I get the data of current login user's publications.while publications and users are cunnected through user_id foregion key. My code is given here.

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

My model code is here publication.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

The code of the PublicationController.php is given here. PLease help me that where is the problem.

<?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)
    {
        //
    }
}

The code of the web.php I means roots are here.

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

Both the tables users table and publications table are connected through foregion key user_id but I can not get the data of current login user data. it gives me all the data of stored in publications tabel.

this can help you, its working

   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');
    }
}

authentication guard - Authentication - Laravel, Routing; Views; Authenticating; Retrieving The Authenticated User; Protecting When building the database schema for the App\User model, make sure the use Illuminate\Support\Facades\Auth; if (Auth::check()) { // The user is logged in }. Hi, I have facing one issue in laravel 5.6 Auth. First time user login into system using auth::attempt() successfully and same that page auth::check() return 1 but after redirecting into other page or url , not getting login user detail or auth::check() not return any data.

You need to change your query in following way to add filter by user:

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

And you are using two similar routes just remove the one which is sending to index method.

How to get current user details in Laravel 5.7?, we will user auth() helper and Auth facade class to get current user data. we always require getting current logged in user data in laravel 5.7  Get Logged User Data using helper. $get_user_dtl = auth ()->user (); dd ($get_user_dtl->id); dd ($get_user_dtl->name); dd ($get_user_dtl->email); $get_user_dtl = auth()->user(); dd($get_user_dtl->id); dd($get_user_dtl->name); dd($get_user_dtl->email);

You can get Login user's data.

First include this in your controller above class declaration

use Auth;

And then change your function show like below

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');
    }
}

Using the Auth to get current user data., I would like to get the current logged in user record so I can retrieve a language id Apologies if I am being thick - I am quite new to Laravel. Laravel Get Month Wise Current Year Data; Get Day Wise Current Week Data Laravel; Laravel Get Data Year Wise; Current Date Record Laravel. If you want to fetch the current date records from database tables in laravel. Use the below Laravel eloquent Query for fetching the current date records. User::whereDate('created_at', Carbon::today())->get(['name','created_at']);

Get current user's ID in API Controller, Pass the api guard as a parameter to fetch the authorized user without the Click on the request which was made to store data and then click on header. For the system, User is not logged in. checkout laravel passport. If you choose to remove these controllers, you will need to manage user authentication using the Laravel authentication classes directly. Don't worry, it's a cinch! We will access Laravel's authentication services via the Auth facade , so we'll need to make sure to import the Auth facade at the top of the class.

Laravel 5.5 tutorial 43 access logged in user details from blade , you can easily get current login user id in controller of laravel 7/6. we will get current user data using auth in laravel 7/6. It's most important thing is  For retrieve data from MySQL database using laravel framework first we have to create a table in data base. After create a table in the MySQL database you need to insert record or data on it.If you want to know how to insert data in laravel framework please visit the link : Insert data in Laravel .

Laravel 7/6 Get Current User Example, We need to make sure that a username and password are included, and that check the cache and see whether the user has logged in too many times. Both attempt to fetch a user from your database, but they differ on how  Laravel can get current login user data both way one using auth helper and another Auth facade. In this post you can learn how to get current logged user id. so first yo Get Logged User Data using helper you can get login user details using auth() helper, it will return object of users details.

Comments
  • you have 2 actions for same route publications
  • yes one for the storing data and one for fetching data
  • No index and show url is same . change for show or change for index
  • still getting the data of all register user's publications
  • change your line return view('publications',$data); to return view('publications',compact('data')); see in my edited answer
  • @IrfanKhan welcome. You can accept my answer and set it as solution. Thanks.