How do I get a assign a record value to a variable in Laravel 5.3? - laravel

I need to display all the vehicle records belonging to properties of the currenty logged on user.
Before I pass the data to the view I need to filter out all the properties except those owned by the current user id.
I have a relationship in the Vehicle model that performs the foreign key constraint.
public function propiedad(){
return $this->belongsTo('App\Property', 'propiedad_id','id' );
}
My Controller is using this filter before passing the data to the view. I assume I have to acquire the property ids that belong to the user before I filter the vehicles that belong to those properties. This is where I am having trouble.
$user_properties = array(?????);
$Vehiculo = Vehiculo::where('propiedad_id', $user_properties);

If your properties table has a user_id column then on your user model define a vehicles hasManyThrough relationship like
public function vehicles()
{
return $this->hasManyThrough(Vehicle::class, Property::class);
}
Then in your controller you can do:
$users = User::with('vehicles')->get();
Or for a single user
$vehicles= User::find($id)->vehicles;

Related

laravel Many Records to One single record....but how?

I have two tables, one is Users and the other is Loans. These have one to many relationship between them. One user can have many loans and one loan belongs to one User.
Now I want to get the user from the list of loans.
Loan::where('user_id', $user)->get()
This give me repeated array of sample user associated to the multiple loans. But I want to get only a single record, like many loans associate to one user.
I assume you already set up relationships between User & Loan Models
// App\User
public function loans() {
return $this->hasMany('App\Loan');
}
// App\Loan
public function user() {
return $this->belongsTo('App\User');
}
Get user from the list of loans
$user = Loan::where('user_id', $id)->first()->user;
// eager loading version
$loansWithUsers = Loan::with('user')->where('user_id', $id)->get();
Loans associate to one user
$loans = User::findOrFail($id)->loans;
// eager loading version
$userWithLoans = User::with('loans')->findOrFail($id);
For what I understand in your question, you want to get the user together with it's loan right ? Your query should like this.
$user_loans = User::with('loans')->where('id', $user_id)->get();
Get the user informations from loan table
Loan::where('user_id', $user_id)->with('user')->get()
Ge the users from loans table depends on brach id
Loan::where('branch_id', $branch_id)->with('user')->get();

laravel user registration by roles in many to many relationship

In my application, I have two types of users "title" & "notary".
I have connected some users to these roles by many to many relationship [there is a table 'role_user', which is pivot table and contains 'user_id' from users table and 'roles_id' from roles table].
I have made seeds to give the users, specific roles. till this point my application is fine.
Now, I want to set a users role, when he registers [the idea is to select a radio button before registration].
When someone registers, he will be given selected role. How to do this in many to many relationship?
When user selects radio button then you will get that value and call a relationship on user to assign role.
$role = $request->role //get this value from radio
Since this is registration method so you will first create user and call a relationship to attach role.
$user = User::create($request->only('required fields ...'));
Now attach role to created user.
$user->roles()->attach($role); //this line is actual answer to your question
This will fill your pivot table with user role
Make sure you have defined roles() method in User.php model
public function register (Request $request){
$role = $request->role; //this is not necessary to save role value in variable you can get it directly from request object
$user = User::create($request->only('required fields'));
$user->roles()->attach($role or $request->role); // you can get role value directly from $request object.
}

Get formatted attributes from a related table for eloquent model

I have 3 tables
1. User table[id, name, email]
2. user_roles[user_id, role_id]
3. roles[ id, permission{post:{edit:true,delete:false}} ]
user hasone user_roles
roles belongs to user_roles
userRole.role is eager loaded in user model
Is it possible to get the permission by calling some custom function in user model.
instead of user->userRole->role everytime.
like call like $user->permissions() will return json from roles tables
You can create a method in your User model like this-
public function permissions()
{
return $this->userRole->role;
}
Laravel allows you to call your relation like that. Here $this represents your User model where you created this method. So calling a relation on $this would work without any problem.
Then you can call this with your user collection like this-
$user->permissions();
If you want to use this with logged in user then you can call it like this-
auth()->user()->permissions();

Laravel relationship between two tables

I'd like your input on this.
I have a Customer_table with a field name. I have another table called Reservation_table with a Customer_name field.
How can I relate them in such a way that I'd see all the bookings by the specific customer?
In Reservation_table you should have a field(foreign key) userid in order ta have a user for each reservation.
You can using primary key - foreign key relationship to relate/join those two tables. Also, instead of having a 'Customer_name' field as your FK referring to 'name' field in 'Customer_table' table, it is better to have an id (unique) generated for each customer; This way you can have an efficient way of uniquely identifying and relating customer across tables; can save space on Database side as well. Hope this helps!
If you want to use eloquent you must first define a relationship.
One reservation belongs to a user. Here is how to define the relationships:
Inside the Reservation model:
public function user()
{
return $this->belongsTo('App/User'); //User model
}
To define the inverse you do the following:
Inside User model:
public function reservations()
{
return $this->hasMany('App/Reservation'); // Reservation Model
}
Now you can do the following in your controller:
$reservations = Auth::user()->reservations;
Now you have all reservations by the currently logged in user.
I am not sure if I got the question right so ask away.

Laravel 4: Return field from related table within Model function

I have a table 'sits' and it has user_id and sitter_id columns.
Inside of my User model, I have the following functions that all work perfectly and return almost everything I need.
public function mySits()
{
return $this->hasMany('Sit', 'sitter_id');
}
public function openSits()
{
return $this->hasMany('Sit')->where('sitter_id', '=', '0');
}
They work perfectly and returns almost everything I need. I say almost because what I need to do is reference the users table to get the name of the Sitter.
In the sits table, the sitter_id maps to id in the users table which has a name field.
Can I add something to the return data in the User model methods, or would I need do something in my controller.
$user = User::find(1)->load('mySits', 'openSits');
Just create a User relationship inside your Sit model. Then access it via $sit->user->name or similar.

Resources