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.
Related
i have a problem with this eloquent. how i can pass only the blog that have the attribute categoryid that same as the id that clicked from index.blade.php ?
This is the code of show function in CategoryController.php, the parameter is the id and comes from the index.blade.php
This is the picture of show.blade.php , the page after click button from index.blade.php
You forgot the get() method after the where method on your CategoryController, something like this:
return view('categories.show', [
'category' => $category,
'blog' => Blog::where('categoryid', $category->id)->get(),
]);
I have two related tables and I want to return all fields including the ID (key) field. My query below returns all fields except the ID. how do I return the ID field from one of the tables?
'programmes' => ProgrammeInstances::with('programmes')->get(),
the query below returns Unknown column 'programmes.programme_title' as it is looking for it in the table 'programme_instances'
'programmes' => ProgrammeInstances::with('programmes')->select('programmes.programme_title', 'programmeInstances.id', 'programmeInstances.name', 'programmeInstances.year')->get(),
Laravel provides multiple relationships, one of these is the hasMany() relationship which should return a collection where a User hasMany rows inside of your database
For example, inside your User model :
public function programmes() {
return $this->hasMany(Program::class);
}
Now in your controller, you can do :
public function edit($id) {
$programmes = User::find($id)->with('programmes')->get();
return view('user.edit')->with('programmes', $programmes);
}
And then you can loop over it inside your view
#forelse($programmes->programmes as $program)
// provide the data
#empty
// the user doesn’t have any programmes
#endforelse
a solution i found below - still not sure why ID isnt automatically returned when i get all fields, but works when i specify individual fields:
'programmes' => ProgrammeInstances::with('programmes')
->get()
->transform(fn ($prog) => [
'programme_title' => $prog->programmes->programme_title,
'id' => $prog->id,
'name' => $prog->name,
'year' => $prog->year,
]),
I have a modal for example User. The User has relationships to Country and Currency modal as well. I would like to return a JSON object of the User with their relationships. I can achieve that using the code below:
$user = User::create($request->all()); // request contains all the information to create the user
$userDetails = User::with('country', 'currency')->where('id', $user->id)->first();
return json_encode($userDetails);
It works. However, is there a better or more recommended way to achieve this? Thank you for your time.
I found out that creating a new Laravel Resource and using the following approach seems much cleaner:
// in UserResource.php:
public function toArray($request)
{
return [
'id' => $this->id,
'country' => $this->country, // relationship
'currency' => $this->currency // relationship
]
}
// In controller:
return new UserResource($user);
Thanks!
I'd like to show names on my select dropdown
I have 3 tables: 'users','users_pro' and 'practice'
users : id ,name ,dob ,.. etc
users_pro : id ,user_id,..etc
practice : id , user_pro_id ,...etc
I want to input data to table 'practice' and I want to make a select dropdown using pluck on users_pro with users like this :
public function input_practice()
{
$data= Users_pro::with('users')->get()->pluck('id','user_id');
dd($data);
// return view('admin.input.input_riwayat_kgb',
// [
// 'data'=>$data,
// ]);
}
If I dd($data), I get :
Collection {#327 ▼
#items: array:4 [▼
6 => 3 // 6 is user_id ,and 3 is 'id' on users_pro , i need to show 'name' like 3 => 'jhon'
13 => 4
12 => 5
14 => 6
]
}
But I want to show the name of the user on my select dropdown. Can someone help me?
You are trying to pull a collection and then get the name and id from the relationship models within. Thus, you will get an error that the property does not exist on the collection because it is many User_pro objects and then tries to get user data from each one of the relations would require a loop.
Assuming you have a users relationship set up on the User_pro model, you can get your dropdown list like this:
$data= User::whereHas('user_pro')->pluck('name','id');
This gives you a collection of users (who happen to be pros) and then plucks the name and id of your users for you to use in your dropdown.
This is solved with this line :
$data = Users_pro::with('users')->get()->pluck('users.nama', 'id');
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();
....
}