Laravel hyperlink to a specific record from table - laravel

I have a table in my database, say users with one of the columns being internal_id. I have a a list of users being displayed in view.blade.php
Now suppose each user is a link which when clicked displays the details of that user and each user has a unique internal_id. I want this internal_id to be used when called for all the column details of the user clicked. How can I do this?

First you need to create a route:
Route::get('user-profile/{internalId}', ['as' => 'profile', 'uses' => 'UserController#showProfile']);
Then you need to create a link. You can use simple URI:
{{ url('user-profile/'.$internalId) }}
Or you can use name of the route:
{{ route('profile', ['internalId' => $internalId]) }}
To get internal ID in a controller, do this:
public function showProfile($internalId)
{
$profile = User::where('internal_id', $internalId)->first();
....
}

Related

How to access other fields on a three way pivot in Laravel?

Here's what my architecture looks like:
The idea is that a user have a role on a given group. Of course, the user belongs to many groups, and a user can have multiple roles (in different groups)
I'm trying to get all the groups a user belongs to.
Please forgive me for not using the convention to name the pivot table as groups_roles_users, I decided this way for readability.
So, in the User model I say:
public function groups() {
return $this->belongsToMany('\App\Group', 'roles_in_groups');
}
In a view I do:
#foreach ($user->groups as $group)
{{ $group->name }}
#endforeach
works perfectly.
Now I want to access the roles table. I want to print something like: In {{ $group->name }} you're a {{ $group->role->name }}, obviously, It doesn't work like that.
If I use the pivot property, it will give me only the user_id and group_id, not even the role_id to search with an static method.
SO, how can I accomplish that?
I'm using Laravel 5.7
Maybe try:
public function roles() {
return $this->belongsToMany('\App\Role', 'roles_in_groups');
}
$user = \User::with(['roles','groups'])->first();
$role_name = $user->role->name
$group_name = $user->group->name
https://laravel.com/docs/5.7/eloquent-relationships#eager-loading

How to show data by id in laravel 5.2

I want to show data by id. If id=1 its need to show only id=1 data and if id 2 its should show only id=2 all data.
My controller method:
->with('classroom',classroomModel::find($id)->first());
Here is my view:
<li>Class code-->{{$classroom->class_code}}</li>
<li>Subject-->{{$classroom->subject_name}}</li>
<li>Section-->{{$classroom->section}}</li>
now it is only show id=1 data. If i view id=2 data then it's show id=1 data. What should to do in my controller?
You should pass variable to your controller's function, to do that you can make a link in your view:
Id 1
Id 2
Routes:
Route::get('/something/{id}', [
'uses' => 'somecontroller#yourfunction',
'as' => 'name'
]);
Function:
public function yourfunction($id)
{
// you can use $id here and return view with classroom
}
I Hope this example will help you.

Accessor parameter is giving me null in Laravel 5.3

I am working on one to many relationship between tasks and Projects, i.e. a task can only belong to a single project, and I have used laravel's Accessor to get the selected project in my view in drop down:
my code is as follow:
public function getAssignUserAttribute($value)
{
dd($value); // gives me null
// if $value have id of user I want to get that user from db
}
and my view contains the drop down is:
{!! Form::select('assign_user', $assign_user, null, ['class' => 'form-control select2', 'id' => 'assign_user']) !!}
I have accessed all the users from database into TasksController to the view as:
$assign_user = User::pluck('title', 'id');
return view('tasks.edit', compact('task', 'assign_user'));
But I get all users selected, while I only want to have the selected user in my drop down.
Can someone guide me to the right path.
Thank you
finally I solved the issue by myself I have edit the Accessor as follow:
public function getAssignUserAttribute()
{
return [0 => $this->attributes['assign_user'] ];
}
Since I required an array so I assigned the current user to the array index 0 and return, in that case the view selected the returned user in drop down :)
This may help someone :)

Laravel - Convert database integer to an associated string

I have my users table with a column named status. In this column I just store a number, based off their status, 1 - admin, 2 - owner, etc. I know that I can display these values with {{ users->status }}, but only shows the stored number. How can I show the actual name instead of the stored number?
Use Eloquent Accessors & Mutators.
Define an array of statuses and use the status number from your database as a key to change the value in the model so that in the view {{ $users->status }} equals admin
class User extends Eloquent {
protected $statuses = array(
'1' => 'admin',
'2' => 'owner'
);
public function getStatusAttribute($value)
{
return $this->statuses[$value];
}
}
getStatusAttribute converts 1 to admin when retrieving a record.
You could also have a set method for when records are stored in the model.
In View {{ $user->status }} // echos 'admin'

Laravel Foreign Keys Drop-down List

I have 2 tables:
CUSTOMERS(id, full_name, company_id)
COMPANIES(id, company_name)
I already created the relation between the two tables, and it's working fine because I can display company name in the customers view like this: $customer->company->company_name
I'm now having issues with the customer create and edit views. I'd like to have the company_name as a drop-down (Form Select) in create and edit views. Then insert the company id to the CUSTOMERS table.
You need to supply Form::select with companies as an array('id'=>'name'):
// Controller, repo or wherever you want it:
$companies = Company::lists('company_name','id');
// $companies passed to the view, then in the create view:
{{ Form::select('company_id', $companies, null, $options) }}
// edit view:
{{ Form::model($customer, array('route' => array('YourCustomerUpdateRoute', $customer->id))) }}
...
{{ Form::select('company_id', $companies, null, $options) }}
// form model binding autopopulates the form, so correct option will be selected
After submitting the form validate the input, check if provided company_id exists on the companies table and save the customer, that's all.
The Jarek Tkaczyk answer is excellent. However if you want make a default value for create form and avoid pre-select the first element in the $companies array, you can do something like this in your controller:
$companies = Company::lists('company_name','id');
$companies = array('0' => 'Select Company') + $companies;
Then pass $companies array to the view as Jarek Tkaczyk said.
Note:
I did
$companies = array('0' => 'Select Company') + $companies;
in order to preserve the array keys.

Resources