Data not fetching of current user in laravel - php

I am trying to get the data of current log in user but i am getting the result of all users in database. While in all views same problem is accure please help.
My code is given
My view is personal.blade.php
#extends('layouts.app')
#section('content')
<div class="container"><br>
<h1 class="text-success text-center">Profile</h1><br>
<table class="table table-bordered">
#foreach($data as $value)
<tr>
<th>Name</th>
<td>{{ $value ->tname}}</td>
</tr>
<tr>
<td>Father Name</td>
<td>{{ $value ->fname}}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $value ->email}}</td>
</tr>
<tr>
<td>Official Email</td>
<td>{{ $value ->off_email}}</td>
</tr>
#endforeach
</table>
<div class="form-group">
<label>Generate Report</label>
<div class="radio form-control">
<label class="col-md-4"><input type="radio" name="status" value="conf" >Personal</label>
<label class="col-md-4"><input type="radio" name="status" value="general">Publications</label>
</div>
</div>
<button class="btn btn-info" data-toggle="collapse" data-target="#demo"> Generate PDF</button>
<button class="btn btn-info" data-toggle="collapse" data-target="#demo"> Generate CSV</button>
<button class="btn btn primary">Go to Home</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>
#endsection
My controller is EducationController.php and its code is given.
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use App\education;
use DB;
class EducationController extends Controller
{
public function show()
{
//
$data['data'] = DB::table('users')->get();
if(count ($data)>0){
return view('personal',$data);
}
else
{
return view('personal');
}
}
}
My route code is given here.
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/personal','EducationController#personal');
Please help me. I dont know how to use the current user in all views. I am login while in result I am getting the result of all users which is stored in database. thanks in advance.

You use a #foreach which implies you expect multiple users in your view.
If you only want to show details for the logged in user you should do:
<?php
class EducationController extends Controller
{
public function show()
{
if(\Auth::check()){
$user = \Auth::user();
return view('personal',compact('user'));
}
else
{
return view('personal');
}
}
}
And in your view:
#extends('layouts.app')
#section('content')
<div class="container"><br>
<h1 class="text-success text-center">Profile</h1><br>
<table class="table table-bordered">
#if (isset($user))
<tr>
<th>Name</th>
<td>{{ $user->tname}}</td>
</tr>
<tr>
<td>Father Name</td>
<td>{{ $user->fname}}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $user->email}}</td>
</tr>
<tr>
<td>Official Email</td>
<td>{{ $user->off_email}}</td>
</tr>
#endif
</table>
// Rest of view

It is because your code is actually for getting all users. You should do it like this :
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use App\education;
use DB;
class EducationController extends Controller
{
public function show()
{
$user = Auth::user();
return view('personal',compact('user'));
}
}
}
then in your view:
<tr>
<th>Name</th>
<td>{{ $user->tname}}</td>
</tr>
<tr>
<td>Father Name</td>
<td>{{ $user->fname}}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $user->email}}</td>
</tr>
<tr>
<td>Official Email</td>
<td>{{ $user->off_email}}</td>
</tr>
Note that if you have multiple auth, you must specify your guard :
Auth::guard('yourguard')->user();
otherwise, your user will return a null.

Related

How to assign user ID automatically in PHP upon user register?

I am looking at creating user ID automatically upon user registration. I've coded the files using Laravel and the database uses MySQL. The format should be S0001, S0002, S0003, so on and so forth.
What kind of datatype I should change it to in MySQL? Currently it is set at BIGINT.
If yes, how do I proceed with the code then?
This code below is from viewuser.blade.php:
{{View:: make('title')}}
{{View:: make('menu')}}
<div class="table-responsive">
<table class="table table-striped table-sm">
<!-- <caption>List of Users</caption> -->
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">User ID</th>
<th scope="col">Full Name</th>
<th scope="col">Email Address</th>
<th scope="col">Password</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{ $loop->iteration}}</td>
<td>{{ $loop->iteration}}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->password }}</td>
<td>{{ date('D, d F Y', strtotime($user->created_at)) }}</td>
<td>
Edit
Delete
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<div class="pagination">
{{ $users->links() }}
</div>
And this is the Register Page code, in case it's useful:
{{View:: make('title')}}
<div>
<form action="register" method="post">
#csrf
<div>
<label for="exampleInputEmail1">Full Name</label>
<input type="text" name="fullname" id="exampleInputName1">
</div>
<div>
<label for="exampleInputEmail1">Email Address</label>
<input type="email" name="email" id="exampleInputEmail1" aria-describedby="emailHelp">
<div id="emailHelp">We'll never share your email with anyone else
</div>
</div>
<div>
<label for="exampleInputPassword1">Password</label>
<input type="password" name="password" id="exampleInputPassword1">
</div>
<button type="submit">Register</button>
<button type="button" onclick="javascript:history.back()">Cancel</button>
</form>
</div>
{{View:: make('footer')}}
Why not just use the existing id column on the Users table, then define a prefix column in your Users table that you can prepend to the id whenever you want to display it.
Users table migration
$table->string('prefix')->default('S');
Users model
Then you can do something like define an accessor on your model to return the combined value of the id and prefix columns:
class User extends Model
{
protected function matriculationNumber()
{
return $this->prefix . str_pad($this->id, 3, '0', STR_PAD_LEFT);
}
}

Separation the table and each name follows the table

I want Make Different on Table, Each table has a service name Dontt Need Loop Again Table same Group Name, Each group has the name of the service
Check : https://i.ibb.co/NTnynGq/639.png
View : Package.blade.php
<div class="pt-120 pb-120">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-12">
<div class="table-responsive--md">
<div class="services-table w-100">
<div class="service-group">
<div class="card">
#foreach($packages as $singlePackage)
<br>
<table class="table table-bordered style--two">
<thead>
<tr>
<th style="width:70%">
<span>Group Name : {{ $singlePackage->groups->name }}</span>
</th>
<th style="width:15%">
<span>#lang('Services Name')</span>
</th>
<th style="width:15%">
<span>#lang('Services Name')</span>
</th>
<th style="width:15%">
<span>#lang('Action')</span>
</th>
</tr>
</thead>
<tbody>
<tr class="block item">
<td class="word-break">{{ $singlePackage->name }}</td>
<td>
<span>{{ $singlePackage->delivery_time }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
</tr>
</tbody>
</table>
#endforeach
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Package Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Package extends Model
{
protected $guarded = ['id'];
public function groups()
{
return $this->belongsTo(Group::class);
}
}
PackageController.php
public function packages()
{
$pageTitle = 'Packages';
$packages = Package::where('status', 1)->with('groups')->paginate(getPaginate());
return view('package',compact('pageTitle', 'packages'));
}
Want Separation each group has the name of the service
Option 1: You can add an ORDER BY group_id in the query
$packages = Package::where('status', 1)->with('groups')->orderBy('group_id')->paginate(getPaginate());
Option 2: You can sort by the group name in the foreach
#foreach($packages->sortBy(function ($item) { return $item->group->name; }) as $singlePackage)
or
#foreach($packages->sortBy(fn($item) => $item->group->name) as $singlePackage)
Option 3: Use the Collection's groupBy method.
<div class="card">
#foreach($packages->groupBy(function ($item) { return $item->groups->name; }) as $name => $group)
<br>
<table class="table table-bordered style--two">
<thead>
<tr>
<th style="width:70%">
<span>Group Name : {{ $name }}</span>
</th>
<th style="width:15%">
<span>#lang('Services Name')</span>
</th>
<th style="width:15%">
<span>#lang('Services Name')</span>
</th>
<th style="width:15%">
<span>#lang('Action')</span>
</th>
</tr>
</thead>
<tbody>
#foreach($group as $singlePackage)
<tr class="block item">
<td class="word-break">{{ $singlePackage->name }}</td>
<td>
<span>{{ $singlePackage->delivery_time }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
</tr>
#endforeach
</tbody>
</table>
#endforeach
</div>

Trying to get property 'name' of non-object (View: C:\xampp\htdocs\AppLaravel\resources\views\admin\editBill.blade.php)

I need to go to my bill list page but this error is stopping me: 'Trying to get property 'name' of non-object'
My route:
Route::get('bill/{id?}',[
'as'=>'bill',
'uses'=>'PageController#editBill'
]);
My Controller:
public function editBill($id)
{
$customerInfo=DB::table('customer')->select('customer.id','customer.name','customer.created_at','customer.phone_number','customer.address','customer.note','bills.total','customer.email')->join('bills','bills.id_customer','=','customer.id')->where('customer.id', '=', $id)->first();
$billInfo=Bill::where('id',$id)->get();
return view('admin.editBill',compact('customerInfo','billInfo'));
}
My view:
<section class="content">
<!-- Default box -->
<div class="box">
<div class="box-header with-border">
<div class="row">
<div class="col-md-12">
<div class="container123 col-md-6" style="">
<h4></h4>
<table class="table table-bordered">
<thead>
<tr>
<th class="col-md-4">Customer Info</th>
<th class="col-md-6"></th>
</tr>
</thead>
<tbody>
<tr>
<td>Customer name</td>
<td>{{ $customerInfo->name }}</td>
</tr>
<tr>
<td>Date Order</td>
<td>{{ $customerInfo->created_at }}</td>
</tr>
<tr>
<td>Phone Number</td>
<td>{{ $customerInfo->phone_number }}</td>
</tr>
<tr>
<td>Address</td>
<td>{{ $customerInfo->address }}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $customerInfo->email }}</td>
</tr>
<tr>
<td>Note</td>
<td>{{ $customerInfo->note }}</td>
</tr>
</tbody>
</table>
</div>
<table id="myTable" class="table table-bordered table-hover dataTable" role="grid" aria-describedby="example2_info">
<thead>
<tr role="row">
<th class="sorting col-md-1" >Id</th>
<th class="sorting_asc col-md-4">Product name</th>
<th class="sorting col-md-2">Quantity</th>
<th class="sorting col-md-2">Unit_price</th>
</thead>
<tbody>
#foreach($billInfo as $key=>$bill)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $bill->product }}</td>
<td>{{ $bill->quantity }}</td>
<td>{{ number_format($bill->unit_price) }} VNĐ</td>
</tr>
#endforeach
<tr>
<td colspan="3"><b>Total</b></td>
<td colspan="1"><b class="text-red">{{ number_format($customerInfo->total) }} đ</b></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-md-12">
<form action="{{route('pdf',$customerInfo->id)}}" method="GET">
<input type="hidden" name="_method" value="PUT">
{{ csrf_field() }}
<div class="col-md-8"></div>
<div class="col-md-4">
<div class="form-inline">
<label>Delivery Status: </label>
<select name="status" class="form-control input-inline" style="width: 200px">
<option value="1">None</option>
<option value="2">On going</option>
<option value="2">Delivered</option>
</select>
<input type="submit" method="get" value="Print" class="btn btn-primary">
</div>
</div>
</form>
</div>
</div>
</div>
</section>
The bill detail list is working fine but the customers detail is not
I tried others way like {{$customer->name ?? ''}} and it did't give an error anymore but it doesn't show any data

Laravel Blade not showing data from controller

I want to show data in tabular form in Laravel Blade View. Now, data is not showing up in blade, but when I check in controller using print_r it shows up Following is the code for Controller:-
public function index(){
$pickup = PickupLocation::select('*')
->whereNull('deleted_at')
->get()
->toArray();
$page = 'pickup_locations';
return view('backEnd.pickupManagement.index',compact('page'));
}
Following is the code in web.php:-
Route::match(['get','post'],'pickup-locations','backEnd\pickupManagement\PickupController#index');
And following is the code I am using View:-
<div class="table-responsive">
<table id="zero_config" class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Contact Number</th>
<th>Address</th>
<th width="13%">Action</th>
</tr>
</thead>
<tbody>
#if(!empty($pickup))
#foreach($pickup as $key=>$value)
<tr>
<td>{{ ucfirst($value['name']) }}</td>
<td>{{ ucfirst($value['contact_no']) }}</td>
<td>{{ $value['location'] }}</td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-trash"></i>
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
add $pickup variable as second argument in your compact method.
Try like this
return view('backEnd.pickupManagement.index',compact('page', 'pickup')); }
Happy codding

Undefined variable: users (View: C:\xampp\htdocs\rentalmobil\resources\views\admin\user.blade.php)

SOLVED
i am confusing that i think i have already make a true code with laravel. i have seen on many references for me to know how to read database. yes i'm newbie and still learning. well here is my code
from model. admin.php is the name of folder:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model
{
protected $table = "admin";
}
here is my controller. my controller name is UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Admin;
class UserController extends Controller
{
public function users(){
$users = Admin::all();
return view('admin.user', ['admin' => $users]);
}
}
and here is my view, it is located in view/admin/ named user.blade.php:
#extends('admin.header')
#section('content')
<div class="col-md-12">
<div class="card">
<div class="header">
<h4 class="title">Striped Table</h4>
<p class="category">Here is a subtitle for this table</p>
</div>
<div class="content table-responsive table-full-width">
<table class="table table-striped">
<thead>
<th>ID</th>
<th>Name</th>
<th>Salary</th>
<th>Country</th>
<th>City</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>{{ $users->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
#endsection
and here is my error
i call from my browser with this http://localhost:8000/users
sorry for bad English :(
#foreach($admin as $users)
<tbody>
<tr>
<td>1</td>
<td>{{ $users->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>
#endforeach
if u not need for each
<tbody>
<tr>
<td>1</td>
<td>{{ $admin[0]->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>
First, the variable will be called admin in the view, because that's how you passed it when you wrote 'admin' => $users.
Second, it's going to be a collection of users, so $users->email isn't going to work. You need to #foreach through each user.
Please update your view/admin/user.blade.php and UserController like:
UserController:
class UserController extends Controller
{
public function users(){
$users = Admin::all();
return view('admin.user', compact('users'));
}
}
view/admin/user.blade.php:
<tbody>
#foreach($users as $user)
<tr>
<td>1</td>
<td>{{ $user->email }}</td>
<td>$36,738</td>
<td>Niger</td>
<td>Oud-Turnhout</td>
</tr>
</tbody>

Categories