laravel 5.2 only paginate data with current user id - php

I want to paginate the data according to current logged in user.
this is my controller :
public function index()
{
$dosen = Dosen::paginate(5);
return view('dosen.index', compact('dosen'));
}

I'm assuming you've a user_id column in your Dosen model table. So you can say
$dosen = Dosen::where('user_id', auth()->user()->id)->paginate(5);
return view('dosen.index', compact('dosen'));

You can get current login user by Auth::user()->id
So pass it in your query.
$dosen = Dosen::where('user_id', '=', Auth::user()->id)->paginate(5);
Note:- user_id is your dosen table column name.

You can first filter the result using query and then apply paginate on the result to get the result-set of logged in user only.
Like this,
$doesn_query = Dosen::query();
$doesn_query->where('user','=',$userid); // Replace $userid with logged in user id.
$result = $doesn_query->paginate(5);

Related

Laravel display the query parameter in the url

I have users table and each user have a column for 'qr_url' and it's a random 170 characters length string generated with Str::random
I have a page that displays all the users and for each user there is a button named inquiry to check if the user exists in the database
I have a route name inquiry that check if this qr_url already for a user or not
Route::get('/users/inquiry/{query}', [UserController::class, 'inquiry'])->name('users.inquiry');
in the inquiry() function I receive this query that contain the qr_url
public function inquiry($query)
{
$user = User::where('qr_url', '=', $query)->first();
return view('users/inquiry', compact('user'));
}
I need to know how can I make the URL looks like that
http://example.com/Home/GetResultByQR?query=theqr_url
Hello guy you should use request()->get('query'). For example:
public function inquiry()
{
$user = User::where('qr_url', '=', request()->get('query'))->first();
return view('users/inquiry', compact('user'));
}

Laravel : How to get all the rows from a table except the first one?

I want to select all the users in my table "User" except the first One cause its the admin,
im using this function index in my controller but it doesn't work .
public function index()
{
// this '!=' for handling the 1 row
$user = User::where('id', '!=', auth()->id())->get();
return view('admin.payroll',compact('user'))->with(['employees' => User::all()]);
}
Better to used here whereNotIn Method
Note: first you find the admin role users and create a static array
$exceptThisUserIds = [1];
$user = User::whereNotIn('id', $exceptThisUserIds)->get();
It's not a good idea to specify the admin user with just id. A better design would be using some sort of a flag like is_admin as a property of your User model.
Still you can use the following code to get the users who have an id greater than 1:
User::where('id', '>', 1)->get()
For getting data skipping the first one you should use skip() method and follow the code like below
public function index()
{
$user = User::orderBy('id','asc')->skip(1)->get();
return view('admin.payroll',compact('user'))->with(['employees' => User::all()]);
}

get last id form database in laravel-8

I want to get only the last inserted data from my database.
Here, I get all data from the database but I want only the last value.
This is ProductController.php
function indextwo()
{
return DB::select("select * from products");
}
This is web.php
Route::get('products_link', [ProductController::class, 'indextwo']);
Here is my current output:
You can get the last id using Query Builder and Eloquent.
Query Builder
function indextwo() {
return DB::table('products')->orderBy('id', 'DESC')->first();
}
Eloquent
function indextwo() {
return Product::orderBy('id', 'DESC')->first();
}
Maybe you can use latest() function for get
$user = DB::select("select * from products")
->latest()
->first();
Maybe you can use Model name direct in controller like your model name is "Product" and also use limit() and latest()
$user = Products::latest()->limit(1);
A very simple way you can follow
Product::query()->latest()->first()
It will return you the latest inserted data according to order by id desc.
Another way :
Product::query()->orderByDesc('id')->first();

Laravel - Use conditions when selecting data using controllers

I have the following controller where I try to select all the customers of a user (The ones with 'user_id' equal to the 'id' of the authenticated user). I know how to do it in the following way, but as you can see it is not very efficient since it selects more records than necessary.
public function index() // Http/Controllers/CustomerController.php:17
{
$user_id = Auth::id(); // Get user ID
$customers = Customer::all()->where('user_id', $user_id); // Select all users and then filter by ID
return $this->showAll($customers, 200); // Return JSON response with response code
}
Change your $customers = Customer::all()->where('user_id', $user_id); to:
$customers = Customer::where('user_id', $user_id)->get();
https://laravel.com/docs/7.x/eloquent#retrieving-models

Laravel Model get all where $id = Auth::user()->id

How to List all rows from a DB where $id matches the logged user id.
I'm using default Auth from Laravel.
At the moment i can list them all with this method in my controller:
public function index(){
$invoices = Invoice::all();
return view('index', compact('invoices'));
}
But i just want the ones that are from this user which is logged in:
Something like
$invoices = Invoice::where('id', '=', Auth::user()->id);
Your code Seems almost right. I would do:
$invoice = Invoice::where('id', Auth::user()->id)->get();
So basically use the get in order to fetch a collection. And maybe I would separate the user id in a varaible in case that you change the authentication in the future ;)
When using any condition then of course you must need to add the get() method. Otherwise, you can't show your data.
$invoices = Invoice::where('id', '=', Auth::user()->id)->get();
This means that you want to see data on which user is logged in now

Categories