Facing issue in laravel orderby - laravel

I am facing a issue with laravel order by function.
i want to fetch the records according to country and the result should start from the current user's country.
For example.
if auth user belong to india then list should start from india.
My Current code is
$users = User::where('status', '=',1)->with(['profile'])->latest()->orderBy('last_login', 'desc')->paginate(12);
Thanks

Ok, whatever I've understood, you are looking to sort the data through the country attributes from profiles table, which is in relation with User model, for this you need to use join I'm not sure about your structure so I'm just giving you an example. Suppose you want to sort by profiles => id then you can write something:
Edit: As per the requirement given in comments I would recommend to have something like this:
First of all lets have authenticated user by
$AuthUser = Auth::user();
Then lets call the relation
$users = User::->leftJoinRelations('profile')
->orderByRaw("FIELD(country , ". $AuthUser->profile->country ."), ASC")
->paginate();
So in this way you can have user sorted by authenticated user country. Hope this helps.

Related

How to update a user using the 'Ion Auth' authentication library in CodeIgniter 4 framework

Below is my code snippet. I want to reduce the balance of a user in the database using Codeigniter 4?
dd($this->ionAuth->update($this->ionAuth->users()->row()->id, ['balance' => 1000]));
*I'm using Ion Auth for updating.
It's just a big burden when updating the database using modelling data.
It looks like you intend to use $this->ion_auth->user() instead of $this->ion_auth->users().
Also keep in mind that executing a dump and die on $this->ion_auth->update(...) returns a boolean not the updated user data.
References:
users() gets all users.
users()
Get the users.
Parameters
'Group IDs, group names, or group IDs and names' - array OPTIONAL. If an array of group ids, of group names, or of group ids and names are passed (or a single group id or name) this will return the users in those groups.
Usage
$users = $this->ion_auth->users()->result(); // get all users
user() gets a single user.
user()
Get a user.
Parameters
'Id' - integer OPTIONAL. If a user id is not passed the id of the currently logged in user will be used.
Usage
$user = $this->ion_auth->user()->row();
echo $user->email;
look here authentcate library for ci4
https://github.com/lonnieezell/myth-auth
go there download it
ionAuth is not compablitablke with ci4

How to Get Order Details per a single User in Laravel 7

I am making a multi-vendor ecommerce website using Laravel 7. I am trying to make an Order History page where in a single can user can view only his/her own orders. I am having a problem building the query on my OrderHistoryController. These are the codes on my controller:
$users = User::get();
$orders = Order::find($users);
return view('order-history', compact('orders'));
And I'm trying to loop $orders on my blade file. I have:
#foreach ($orders as $order)
<div>{{ $order->id}}</div>
<div>{{ $order->grand_total}}</div>
#endforeach
I am trying to pass the order id and order grand total to my view. I don't get any error however it shows a different order details from a different customer. How do I do this?
You should use MySQL (or any other database you are using) table relationships and foreign keys.
So, your orders table, should have columns id, user_id, date, and so on. The important thing is user_id column. You want to make that column a foreign key. That basically means that user_id will hold an id value from another table, in this case user. See how to make foreign key columns in laravel migrations here.
Next up is to use Laravel's built in model relationships. You can read more about them here. Basically, in your orders model you want to have a function user which returns $this->belongsTo(App\Models\User::class) and in your user model you want to have a function orders which returns $this->hasMany(App\Models\Order::class) (or whatever the namespace is for both of them).
That way, you can call $user->orders and get a collection of Order models which will contain only the orders of that particular user.
Definitely read the documentation carefully and learn basic concepts of the framework first and how relational databases function!
You can get a lot of this information just by googling it or reading the documentation of Laravel or any other framework you plan on using! Good luck learning :)

Laravel 5.2 eloquent model order by their relation model attribute

I have a question. I have an user model and a post model. A user can have many posts. Now I need to get posts with pagination. And I need to sort posts by their related user's id desc. Per page number is 10. I do not know how to write this code, someone can help me? Thanks.
Since Post belongs to User the posts table has user_id key. Use it:
Post::orderBy('user_id', 'desc')->paginate(10);

Insert a row that contains relational data?

This is a simplified version but... a user can submit the form which contains their name and qualification.
I have two tables - users and qualifications. A user has many qualifications. A qualification has one user.
I save like so:
$user = new User;
$user->first_name = 'Derek';
$qualification = new Qualification;
$qualification->qualification = 'History Degree';
DB::transaction(function() use ($user, $qualification) {
$user->save();
User::find($user->id)->qualifications()->save($qualification);
});
My questions are:
Is this the correct way to insert a row to a table and it's relationship? Is there a better way?
And my 2nd question...this line:
User::find($user->id)->qualifications()->save($qualification);
This may be a bit crazy but I just would like this explaining to me, the $user->id gets the id of the previously inserted row, if two people were to insert at the same time, how would I be sure that it would get the correct id? As in, user A saves, then user B saves, then the first User::find bit runs for user A but this would find user B's id. Is this where the transaction comes in?
Before you save the user by doing this:
user->save();
I think you should first save the "qualification" to that user. As long as your code functions as you currently have it, I would think this would do the trick:
$user->qualifications()->save($qualification);
and then do
user->save();
If you can get it to work in that order, you'll never have to worry about obtaining the wrong user ID and switching the qualifications on the wrong user, if they were to enter their information at approximately the same time.

Retrieve selected keys from database from relations

I am using laravel 4 and I have to retrieve registrations and all users associated with that registrations but user table has lot more keys but I only need 3 keys so how can I get selected keys. I know laravel provide select method but its not working on relations.
Registration::where('section_id', '=', $key->id)
->with('user')
->select('users.id', 'users.name')
->get();
I tried users and user but nothing works. Could someone please post correct way to load this query.
Edit:
- Registration has one user
- Registrations has one section
I have registration table that contains section id and user id. I want to get all users, signed up for particular section. So this is very simple query and its working fine only problem is retrieving selected keys from database.
In your Registration model change your relation to this
public function user()
{
return $this->hasOne('User')->select('id','name');
}
And in your controller write this
Registration::where('section_id', '=', $key->id)->with('user')->get();
Hope this work for you.

Resources