Home > other >  Laravel 9 Display Data After Login
Laravel 9 Display Data After Login

Time:09-05

These are my laravel codes, I want for the user to display his own data after he logs in, I've tried to use Auth::user() but it only shows data from another table (users) and I could figure out how to change the targeted table. I've tried with session, is it good?

Here is my model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Chababounauser extends Model
{
    use HasFactory;
    protected $fillable = [
        'name', 'surname', 'sex', 'job', 'day', 'month', 'year', 'place', 'residence', 'hobby', 'help', 'photo', 'email', 'scholar_year', 'tel', 'DadTel'
    ];
    public $timestamps = false;
}

class ABC extends Model {

    const CREATED_AT = null;
    const UPDATED_AT = null;

}

and here is my view

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="/css/DataUser.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" type="image/x-icon" href="/img/android-chrome-512x512.png">
    <title>استمارة التسجيل</title>
</head>
<body>
    <div >
    <h1>استمارة التسجيل بجمعية شبابنا</h1>
    <section><div>{{ Auth::user()->id }}</div> <span>:الرقم</span></section>
    <h2>بطاقة معلومات التلميذ</h2>
    <section><div>{{ Auth::user()->name }}</div><span>:الاسم</span></section>
    <section><div>{{ Auth::user()->surname }}</div><span>:اللقب</span></section>
    <section><div>{{ Auth::user()->sex }}</div><span>:الجنس</span></section>
    <section><div><img src="{{ asset(Auth::user()->photo) }}"></div><span>:الصورة</span></section>
    <section><div>{{ Auth::user()->year }} / {{ Auth::user()->month }} / {{ Auth::user()->day }}</div><span>:تاريخ الميلاد</span></section>
    <section><div>{{ Auth::user()->place }}</div><span>:مكان الميلاد</span></section>
    <section><div>{{ Auth::user()->residence }}</div><span>:العنوان</span></section>
    <section><div>{{ Auth::user()->job }}</div><span>:مهنة الولي</span></section>
    <section><div>{{ Auth::user()->email }}</div><span>:رقم الهاتف</span></section>
    <section><div>{{ Auth::user()->level }}</div><span>:المستوى</span></section>
    <section><div>{{ Auth::user()->hobby }}</div><span>:الهواية</span></section>
    <section><div>{{ Auth::user()->help }}</div><span>:ما يمكن تقديمه</span></section>
    <section><div>{{ Auth::user()->scholar_year }}</div><span>:المستوى الدراسي</span></section>
    <section><div>{{ Auth::user()->tel }}</div><span>:رقم الهاتف</span></section>
    <section><div>{{ Auth::user()->DadTel }}</div><span>:رقم هاتف الولي</span></section>
</body>
</html>l

I also got many controller for different forms, here is one of them:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Chababounauser;
use Illuminate\Support\Facades\Auth;

class EtudiantController extends Controller
{
    function registerUser(Request $req){
        // dump($req);
        $this->validate($req,[
            'name' => 'required|max:120',
            'surname' => 'required|max:120',
            'job' => 'max:120',
            'day' => 'required|max:120',
            'month' => 'required|max:120',
            'year' => 'required|max:120',
            'place' => 'required|max:120',
            'residence' => 'required|max:120',
            'email' => 'required|email|unique:users',
            'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'scholar_year' => 'required|max:120',
            'tel' => 'required|regex:/(0)[0-9]{9}/',
            ]);

        $e = new Chababounauser;
        $e->name = strip_tags($req->name);
        $e->surname = strip_tags($req->surname);
        $e->sex = $req->sex;
        $e->job = strip_tags($req->job);
        $e->day = strip_tags($req->day);
        $e->month = strip_tags($req->month);
        $e->year = strip_tags($req->year);
        $e->place = strip_tags($req->place);
        $e->residence = strip_tags($req->residence);
        $e->email = strip_tags($req->email);
        if($req->hasfile('photo')){
            $file = $req->file('photo');
            $extension = $file->getClientOriginalExtension();
            $filename = time().'.'.$extension;
            $file->move('uploads/students', $filename);
            $e->photo = $filename;
        }
        $e->scholar_year = strip_tags($req->scholar_year);
        $e->tel = strip_tags($req->tel);
        $e = auth::user();
        $data = $req->input();
        $req->session()->put('name', $data['name']);
        return redirect('user');
    }

    function loginUser(Request $req){
        $this->validate($req,[
            'name' => 'required|max:120',
            'surname' => 'required|max:120',
            'email' => 'required',
            'tel' => 'required|regex:/(0)[0-9]{9}/',
            ]);
            $name = Chababounauser::where('name', '=', $req->name)->first();
            $surname = Chababounauser::where('surname', '=', $req->surname)->first();
            $email = Chababounauser::where('email', '=', $req->email)->first();
            $tel = Chababounauser::where('tel', '=', $req->Tel)->first();
            if($name && $surname && $email && $tel){
                $req->session()->put('name', $name->name);
                return redirect('الصفحة-الرئيسية');
            }else{
                return back()->with('fail', 'هذا المستعمل غير موجود');
            }
    }
}

CodePudding user response:

You can use Auth middleware in your contrllers and your web routes: For example adding this function to your controller:

public function __construct()
    {
        $this->middleware('auth');
    }

and example for a route:

Route::group(['middleware' => ['auth']], function () {
    Route::get('/data', 'DataController@index')->name('data');
});

CodePudding user response:

1st option

By default, Auth::user() uses the table specified in the configuration config/auth.php

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

so Auth::user() return App\Models\User:class (users table).

If u want use App\Models\Chababounauser::class instead of App\Models\User:class, change it - and then Auth::user() will return Chababounauser::class table.

2nd option

If you don't want to change it, just send Chababounauser data from Controller to View - and in View don't use Auth::user() just data from controller.

Controller:

$e = new Chababounauser;
$e->name = 'example name'
$e->save()

return view('user', $e);

User view:

<div>{{ $e->name }}</div>

https://laravel.com/docs/9.x/blade

https://laravel.com/docs/9.x/redirects

  • Related