I want to display all the data coming from the ‘senders_name” column in my ‘staff’ table, so i can display it in my select input box dropdown.
In the code below I have establish a one to many relationship between staff and Post, and I am trying to loop between all senders_name data
in my select drop down option, I have attached the staff model in my PostConroller and i have pass the variable to
//this is my post table
//this is my post model
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Post extends Model
{
protected $table = 'posts';
public function staff(){
return $this->belongsTo('AppModelsStaff', 'staff_id');
}
}
//this is my staff model
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Staff extends Model
{
protected $table = 'staff';
public function posts(){
return $this->hasMany('AppModelsPost');
}
}
//this is my PostController
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsStaff;
use AppModelsPost;
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{ $staffs= Staff::all();
return view("posts.create")->with('posts', $staffs);
}
//this is my create page that shows the error(undefine variable staff)
@extends('layouts.admin')
@section('content')
<div class="block bg-warning ">
<h1 class="lead p-3 container">Expense Requests Form</h1></div>
@include('inc.messages')
<div class="container">
<link rel="stylesheet" href="{{ asset('css/style.css') }}">
<form action='{{url('posts')}}' method='POST'>
@csrf
<div> <label for="exampleSelectRounded0" style="margin-top:10px;">DRIVERS DETAILS</label><h6>Drivers Name</h6>
@foreach ($staffs as $staff )
<select class="custom-select rounded-0" id="exampleSelectRounded0" name="staff_id">
<option value="{{ $staff->id }}" {{ old('') == "" ? 'selected' : '' }}>{{ $staff->senders_name }}</option>
@endforeach
</div>
</div>
<div>
<@endsection>