Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

saqar's avatar
Level 1

Help on laravel

I want to get the name of a bedtype to show in my bed table view instead of its id

Bed BedType Charge Edit Delete

1 jo 1 $20 update delete

below is my add bed template and the show bed template

<!-- plugins:css -->

@include('nurse.css')

@include('nurse.banner') @include('nurse.sidebar')
  <!-- partial -->
  @include('nurse.navbar')

 
         
    {{-- <div class="row "> --}}
      <div class="col-12 grid-margin">
        {{-- <div class="card"> --}}
          <div class="card-body">
            <h4 class="card-title" style ="padding-top:60px;margin:10px;"></h4>
            <div class="table-responsive" style="padding:50px;">
                <a class="btn btn-info" id="new-bed" href="#" style="margin-bottom:2%">New Bed </a>
              <table class="table table-striped table-hover">
                <thead class="thead-dark">
                  <tr>
                    
                    <th scope="col">#</th>
                    <th scope="col">Bed</th>
                    <th scope="col">Bed Type</th>
                    <th scope="col">Charge</th>
                    {{-- <th scope="col">Available </th> --}}
                    <th scope="col">Edit</th>
                    <th scope="col">Delete</th>
                  </tr>
                </thead>
                <tbody>
                  @foreach ($beds as $bed)
                  <tr>
                    <td>{{ $bed->id}}</td>
                    <td>{{ $bed->name}}</td>
                    <td>{{ $bed->bed_type}}</td>
                    <td>{{ $bed->charge}}</td>
                    {{-- <td>{{ $bed->is_available}}</td> --}}
                    <td>
                        <a class="btn btn-primary" href="{{url('updateBed',$bed->id)}}">Update</a>                        
                    </td>  
                    <td>
                      <a onclick="return confirm('Are you sure you want to delete?')" class="btn btn-danger" href="{{url('deleteBed',$bed->id)}}">Delete</a>                     
                  </td>
            
                  </tr>
                  @endforeach
              </tbody>
            </table>
          </div>
        </div>
      </div>



      <div class="modal fade " id="nurse-modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="exampleModalLabel">New Bed</h5>
              <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
      
                        <form  action="{{url('upload_bed')}}" method="POST" id="nurse-form" enctype="multipart/form-data">
                          @csrf
                          <div class="row mb-3">
                              <label for="name" class="col-sm-2 col-form-label">Bed</label>
                              <div class="col-sm-10">
                                  <input type="text" class="form-control" id="name" name="name" placeholder="Bed" required>
                              </div>
                          </div>
                          <div class="row mb-3">
                              <label for="bedTypes" class="col-sm-2 col-form-label">Bed Type</label>
                              <div class="col-sm-10">
                                <select class="form-select form-select-sm" name="bed_type" aria-label=".form-select-sm example" >
                                  <option>--Select Bed Type--</option>
                                  @foreach($bedTypes as $bed_type)
                                  <option value="{{$bed_type->id}}" class="form-control">{{$bed_type->title}}</option>
                                  @endforeach
                                </select>
                              </div>
                          </div>
                          <div class="row mb-3">
                            <label for="number" class="col-sm-2 col-form-label">Charge</label>
                            <div class="col-sm-10">
                                <input type="number" class="form-control" id="charge" name="charge" placeholder="Ghc" required>
                            </div>
                        </div>
                          
                      </form> 
      
      
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Close</button>
              <button type="submit" id="sub-nurse-btn" class="btn btn-outline-primary">Submit</button>
            </div>
          </div>
        </div>
      </div>
<!-- container-scroller -->

<!-- plugins:js -->

<!-- End custom js for this page -->
@include('nurse.script')

{{-- @section('customNurseScript') --}}

$(document).ready(function(){ $('#new-bed').click(function(){ $('#nurse-modal').modal('show'); }); $('#sub-nurse-btn').click(function(){ $('#nurse-form').submit(); }); });

{{-- @endsection --}}

0 likes
20 replies
tykus's avatar

Format your code correctly please.

And clarify the question, it is not clear what you are actually asking.

1 like
saqar's avatar
Level 1

@tykus @martinbean Ok i have a table i want to show. But in one of the columns(bedtype), i want to get the name of the bedtype instead of the id. This is the code for table view template

New Bed
                    <th scope="col">#</th>
                    <th scope="col">Bed</th>
                    <th scope="col">Bed Type</th>
                    <th scope="col">Charge</th>
                    {{-- <th scope="col">Available </th> --}}
                    <th scope="col">Edit</th>
                    <th scope="col">Delete</th>
                  </tr>
                </thead>
                <tbody>
                  @foreach ($beds as $bed)
                  <tr>
                    <td>{{ $bed->id}}</td>
                    <td>{{ $bed->name}}</td>
                    <td>{{ $bed->bed_type}}</td>
                    <td>{{ $bed->charge}}</td>
                    {{-- <td>{{ $bed->is_available}}</td> --}}
                    <td>
                        <a class="btn btn-primary" href="{{url('updateBed',$bed->id)}}">Update</a>                        
                    </td>  
                    <td>
                      <a onclick="return confirm('Are you sure you want to delete?')" class="btn btn-danger" href="{{url('deleteBed',$bed->id)}}">Delete</a>                     
                  </td>
            
                  </tr>
                  @endforeach
              </tbody>
            </table>
          </div>

and this is the code for the form to insert values into the above table

                        <form  action="{{url('upload_bed')}}" method="POST" id="nurse-form" enctype="multipart/form-data">
                          @csrf
                          <div class="row mb-3">
                              <label for="name" class="col-sm-2 col-form-label">Bed</label>
                              <div class="col-sm-10">
                                  <input type="text" class="form-control" id="name" name="name" placeholder="Bed" required>
                              </div>
                          </div>
                          <div class="row mb-3">
                              <label for="bedTypes" class="col-sm-2 col-form-label">Bed Type</label>
                              <div class="col-sm-10">
                                <select class="form-select form-select-sm" name="bed_type" aria-label=".form-select-sm example" >
                                  <option>--Select Bed Type--</option>
                                  @foreach($bedTypes as $bed_type)
                                  <option value="{{$bed_type->id}}" class="form-control">{{$bed_type->title}}</option>
                                  @endforeach
                                </select>
                              </div>
                          </div>
                          <div class="row mb-3">
                            <label for="number" class="col-sm-2 col-form-label">Charge</label>
                            <div class="col-sm-10">
                                <input type="number" class="form-control" id="charge" name="charge" placeholder="Ghc" required>
                            </div>
                        </div>
                          
                      </form> 
tykus's avatar

@saqar so a Bed belongs to a BedType? Do you have a relationship defined on the Bed model?

saqar's avatar
Level 1

@tykus yeah this is the relationship in the Bed model

public function bed_types() { return $this->belongsTo(BedType::class); }

tykus's avatar

@saqar by convention this would be singular bed_type since it returns 0 or 1 instance, but it appears above that the foreign key is bed_type as well, so let's not change it!

Now you can eager-load the BedType along with the Beds Collection:

$beds = Bed::with('bed_types')
    // ->where(etc)
    ->get();

And in the view, you can use the bed_types property which is a BedType instance (the ?? operator will mitigate for orphaned data)

@foreach($beds as $bed)
    <!-- etc. -->
    {{ $bed->bed_types->name ?? 'no bed type'}}
@endforeach
saqar's avatar
Level 1

@tykus So what you are saying is i should add this piece of code $beds = Bed::with('bed_types') // ->where(etc) ->get(); to the relationship in the Bed model?

tykus's avatar

@saqar no. Wherever you are getting the $beds variable for the view, you need to eager-load the bed_types relationship.

saqar's avatar
Level 1

@tykus Alright thanks a lot. But now it seems to be getting only the 'no bed type' choice though i select a bedtype in the form

tykus's avatar

What does your code look like now? How are you assigning the bed_types foreign key on a Bed model whenever you are submitting the form?

Tray2's avatar

@saqar A foreign key usually has the table name it references in singular form followd by an id. That is the naming convention that is assumed by Laravel, unless you tell it something else.

So you have a beds table and a bed_types table, and since a bed can have a bed_type the foreign key should reside in the beds table. Thus the name of the foreign key should be bed_types in it's sindular form bed_type followed by the name if the primary key in the bed_types table, which should be id. Putting all that together makes the foreign ke column name bed_type_id, and that is what Laravel expects you to have in your beds table.

saqar's avatar
Level 1

@tykus So i guess you want to see thecodes for the form ?

saqar's avatar
Level 1

@tykus This is the table view.

                    <th scope="col">#</th>
                    <th scope="col">Bed</th>
                    <th scope="col">Bed Type</th>
                    <th scope="col">Charge</th>
                    {{-- <th scope="col">Available </th> --}}
                    <th scope="col">Edit</th>
                    <th scope="col">Delete</th>
                  </tr>
                </thead>
                <tbody>
                  @foreach ($beds as $bed)
                  <tr>
                    <td>{{ $bed->id}}</td>
                    <td>{{ $bed->name}}</td>
                    <td>{{  $bed->bed_types->name ?? 'no bed type'}}</td>
                    <td>{{ $bed->charge}}</td>
                    {{-- <td>{{ $bed->is_available}}</td> --}}
                    <td>
                        <a class="btn btn-primary" href="{{url('updateBed',$bed->id)}}">Update</a>                        
                    </td>  
                    <td>
                      <a onclick="return confirm('Are you sure you want to delete?')" class="btn btn-danger" href="{{url('deleteBed',$bed->id)}}">Delete</a>                     
                  </td>
            
                  </tr>
                  @endforeach
              </tbody>

This is the form

       <form  action="{{url('upload_bed')}}" method="POST"  enctype="multipart/form-data">
                @csrf
                <div class="mb-3">
                  <label for="name" class="form-label">Bed</label>
                  <input type="text" class="form-control" style="color:black"  name="name" required>
                </div>
                <div class="col-12 col-sm-6 py-2 wow fadeInRight" data-wow-delay="300ms">
                  <label for="title" class="form-label">Bed Type</label>
                    <select name="title" id="title"  class="custom-select" required>
                      <option>--Select Bed Type--</option>
                      @foreach($bed_types as $bed_type)
                      <option value="{{$bed_type->id}}" class="form-control">{{$bed_type->title}}</option>
                      @endforeach
                    </select>
                  <div class="mb-3">
                    <label for="charge" class="form-label">Charge</label>
                    <input type="number" class="form-control" style="color:black"  name="charge" required>
                  </div>

this is the controller

class BedController extends Controller{ public function showBed(){

    // $beds = Bed::all();
    $beds = Bed::with('bed_types')
 ->where('bed_type', '!=', null)
->get();
  $bedTypes = BedType::all();
    
    return view("nurse.showBed",compact("beds"))->with('bedTypes', $bedTypes);
}



   public function upload(Request $request){

       $request->validate([
    'name'=> 'required|unique:beds,name',
    'charge'=> 'required',
    // 'bed_type' => 'required|exists:bed_types,title',
    
    ]);

Bed::create([
    'name' => $request->name,
    'charge' => $request->charge,
    'bed_type' => $request->bed_type,
]);



return redirect()->back()->with('message','Bed Added Successfully');

}

public function deleteBed($id){

$beds = Bed::find($id);
$beds->delete();

return redirect()->back();

}

public function updateBed($id){

$beds = Bed::find($id);

return view('nurse.update_bed',compact("beds"));

}

public function editBed(Request $request , $id){

$beds=Bed::find($id);
$beds = new Bed();
$beds->name = $request->name;
$beds->charge = $request->charge;
$beds->bedtype_id = $request->bed_types;

$beds->save();

return redirect()->back->with('message','Bed Details updated Successfully');

}

tykus's avatar

@saqar the select input is named incorrectly as title, and not bed_type

<select name="title" id="title"  class="custom-select" required>
// change to 
<select name="bed_type" id="bed_type"  class="custom-select" required>

Good validation rules would have caught this...

Also, is the database column bed_type or bedtype_id -- the inconsistency is maddening!

Last, WTF is this $beds assignment?

public function editBed(Request $request , $id)
{
    $beds=Bed::find($id);
    $beds = new Bed();
    // ...
saqar's avatar
Level 1

@tykus the database column is bed_type.

$bed assignment - is it wrong?? Please bear with me i'm only a beginner

tykus's avatar

@saqar in the form, the input should be named correctly, and represented in the validation rules and getting data out of the Request:

<select name="bed_type" id="bed_type"  class="custom-select" required>

Then in the controller actions, you get that data from the Request using the same name:

public function upload(Request $request)
{
    $request->validate([
        'name'=> 'required|unique:beds,name',
        'charge'=> 'required',
        'bed_type' => 'required|exists:bed_types,title',
    ]);

    Bed::create([
        'name' => $request->name,
        'charge' => $request->charge,
        'bed_type' => $request->bed_type,
    ]);

    return redirect()->back()->with('message','Bed Added Successfully');
}
public function editBed(Request $request , $id)
{
    $beds=Bed::find($id);
    $beds->name = $request->name;
    $beds->charge = $request->charge;
    $beds->bed_type = $request->bed_type;
    $beds->save();

    return redirect()->back->with('message','Bed Details updated Successfully');
}

As I mentioned yesterday, and @tray2 today, there are conventions that we use for foreign keys (such as the _id suffix) which will make other aspects of your development easier. But, I don't think it is worth laboring on it for now.

1 like
saqar's avatar
Level 1

@tykus I've done all those changes you asked but it still shows the 'no selected bed' tho i chose a bed type

tykus's avatar

@saqar does BedType model have a name property? I see you are using title in the select options!

<option value="{{$bed_type->id}}" class="form-control">{{$bed_type->title}}</option>

but name in the table:

<td>{{  $bed->bed_types->name ?? 'no bed type'}}</td>

If there is no name, then you should be using title here!

1 like
martinbean's avatar

@saqar Try and pick a better name for threads than “Help on Laravel”. People wanting help with Laravel is the primary reason people post here.

Summarise your actual problem in your thread title. This forum would be a mess if every thread title was “Help with Laravel”, “Laravel help”, or any other number of derivatives.

1 like

Please or to participate in this conversation.