The Route is Route::resource('/admin/post', 'postController');
in controller the code is
public function index()
{
$s = Singer::where('id','<',5)->get()->name;
return view("admin.new-post", ['s'=>$s]);
}
And the view is
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true">
<?php foreach ($s as $ss){
echo '<option>'. $ss .'</option>';
}?>
</select>
I want to list down all the names of singers from data base in select. And the problem is that when I get a single value using first() in query instead of get() it returns single value correctly widhout any error. but when I use get() and in view use foreach loop to get all singers then it gives this error. Undefined property: Illuminate\Database\Eloquent\Collection::$name. I have reviewed code many times but could not understand that what the error is. while there is no problem to get a single name.. please provide me some solution.
Controller Code
public function index()
{
$s = Singer::where('id','<',5)->get();
return view("admin.new-post", ['s'=>$s]);
}
View Code
<select class="form-control select2 select2-hidden-accessible" multiple="" data-placeholder="Select a State" style="width: 100%;" tabindex="-1" aria-hidden="true">
<?php foreach ($s as $ss){
echo '<option>'. $ss->name .'</option>';
}?>
</select>
When you use first() you get a single result, when you use get() you are getting a collection of results, even if your searching for one user.
Related
I'm getting this error: Undefined variable: sales_rep_names (View: C:\wamp64\www\VLCMRenewals\resources\views\search\index.blade.php)
when I try adding a new search dropdown in my blade. This is the new code I've added that's causing the problem:
{{--select Sales Representative --}}
<!-- adding sales rep placeholder
Can we make this a drop-down menu?
need to make sure "name=" is correct -->
<div class="form-group col-md-4">
<label class="mr-sm-2" >Sales Representative</label>
<select class="custom-select mr-sm-2" name="primary_sales_rep">
<option selected></option>
#if(count($sales_rep_names) >0)
#foreach ($sales_rep_names as $sales_rep_name)
<option value='{{$sales_rep_name->Primary_Sales_Rep}}'>{{$sales_rep_name->Primary_Sales_Rep}}</option>
#endforeach
#endif
</select>
</div>
And here's an example of a dropdown that DOES work (I copied the format):
{{--select Manufacturer --}}
<div class="form-group col-md-4" >
<label class="mr-sm-2" >Manufacturer Name</label>
<select class="custom-select mr-sm-2" name="company">
<option selected></option>
#if(count($manufacturer_names) >0)
#foreach ($manufacturer_names as $manufacturer_name)
<option value='{{$manufacturer_name->Manufacturer_Name}}'>{{$manufacturer_name->Manufacturer_Name}}</option>
#endforeach
#endif
</select>
</div>
Lastly, here's the code I have my in Controller (manufacturer and customer both work):
public function index()
{
// will need to add Sales Rep here
$customer_names = DB::table('dbo.contract_view')
->distinct()
->orderBy('Customer_Name','asc')
->get(['Customer_Name']);
$manufacturer_names = DB::table('dbo.contract_view')
->distinct()
->orderBy('Manufacturer_Name','asc')
->get(['Manufacturer_Name']);
// when I tested with product part number it also gave me an error with index.blade.php. maybe this is the wrong spot?
$sales_rep_names = DB::table('dbo.contract_view')
->distinct()
->orderBy('Primary_Sales_Rep','asc')
->get(['Primary_Sales_Rep']);
return view('search.index', ['customer_names' => $customer_names], ['manufacturer_names' => $manufacturer_names], ['sales_rep_names' => $sales_rep_names]);
}
Any advice?
You are sending the variables as 3 different arrays separated by commas. The view parameters are $view, $data, $mergeData, so the forth parameter you are sending is ignored.
The correct way to pass variables to the view is like this:
return view('search.index', [
'customer_names' => $customer_names,
'manufacturer_names' => $manufacturer_names,
'sales_rep_names' => $sales_rep_names
]);
or
return view('search.index')
->with(compact('customer_names', 'manufacturer_names', 'sales_rep_names');
or
return view('search.index')
->with('customer_names', $customer_names)
->with('manufacturer_names', $manufacturer_names)
->with('sales_rep_names', $sales_rep_names)
I have two tables, schools and grades and they are many to many related. I have successfully set up the many to many relationships in the model already.
I am able to insert the select box value into the database but i having difficulty try to fetch.
When i click on a grade with id = 1 for instance, i am trying to fetch its corresponding school into the select box including the values. i get an error Integrity constraint violation: 1052 Column 'id' in field list is ambiguous.
What am i not doing right in my query?
controller
public function editPage($id)
{
$grade = Grade::whereId($id)->firstorFail();
$schools = $grade->schools()->pluck('id')->toArray();
return view('grades.edit',compact('grade','schools'));
}
Model
public function schools()
{
return $this->belongsToMany('App\School')
->withTimestamps();
}
View
<div class="form-group">
<label for="select" class="col-lg-2 control-label">School</label>
<div class="col-lg-10">
<select class="form-control" id="school" name="school[]" mulitple>
#foreach($schools as $school)
<option value="{!! $school->id !!}" #if(in_array($school->id, $schools)) selected="selected" #endif >
{!! $schools->name !!}
</option>
#endforeach
</select>
</select>
</div>
</div>
$school is an array in your case, so your view most likely can't decide what to do with $school->id...
Seems more likely you wanna just use school models in your view, so just only compact('grade') and then use foreach($grade->schools as $school) in your view.
Also your conditional for selected options seems wrong, every $school (which is only an id at this point, because $schools is an array of ids) is obviously in $schools because that's how your foreach is set up. But you should fix the other thing first.
I think it's probably because it's not sure what 'id' you want to pluck.
Try
$schools = $grade->schools()->pluck('schools.id')->toArray();
You could also just pass the grade and get the schools in the view...
public function editPage($id)
{
$grade = Grade::whereId($id)->with('schools')->firstorFail();
return view('grades.edit', compact('grade'));
}
And then
$school_options = $grade->schools->pluck('name', 'id');
#foreach($school_options->all() as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
I'm trying to pass variables from controller to view i tried both compact and with but nothing works, i get this error:
ErrorException undefined variable: programs.
Controller
public function getManageCourse(){
$programs = Program::all();
$academics = Academic::orderBy('academic_id','DESC')->get();
return view(('courses.manageCourse'),compact('programs','academics'));
}
View
<div class="col-sm-4">
<label for="program">Course</label>
<div class="input-group">
<select class="form-control" name="program_id" id="program_id">
<option value="">------------</option>
#foreach($programs as $key =>$p)
<option value="{{$p->$program_id}}">{{$y->program}}</option>
#endforeach
</select>
<div class="input-group-addon">
<span class="fa fa-plus" id="add-more-program"></span>
</div>
</div>
</div>
I have looked into similar problems but i didn't find a solution, why is this happening? what am i doing wrong?
I found the solution i was doing $p->$program_id instead of $p->program_id
make it like this:
return view('courses.manageCourse',compact('programs','academics'));
not like you are doing it:
return view(('courses.manageCourse'),compact('programs','academics'));
write
return view('courses.manageCourse',compact('programs','academics'));
and
<option value="{{$p->$program_id}}">{{$p->program}}</option>
instead of
<option value="{{$p->$program_id}}">{{$y->program}}</option>
this controller controller code. $name contain array of values i have display name in dropdownlist
public function category()
{
$name =DB::select('select name from category');
$category = json_encode($name);
return view('Addworkout')->with('categoryname',$category);
}
this is my view page
<select id="select1" class="form-control select2-hidden-accessible" style="width: 100%" data-placeholder=" select category" tabindex="-1" aria-hidden="true">
<?php foreach($categoryname as $cate) { ?>
<option value="" disable selected><?php echo $cate->name; ?></option>
<?php } ?>
</select>
When you do
$category = json_encode($name);
you're serializing the results of the query into a string, which is indeed an invalid argument for a foreach loop.
Why are you doing so?
I'm trying to pass data from controller to view page.. My query loaded from the model prints the result but when passed into view page, data not giving any result,instead it shows error:Message: Undefined index: Country_id .Why this really happens for.?
public function something()
{
$data['country']= $this->some_model->getCountry();
$this->load->view('another/location.php',$data);
//print_r($data['country']);
}
my view:
<div class="form-group">
<label for="" class="control-label col-xs-4">Country*</label>
<div class="col-md-6">
<select class="form-control" name="country" id="country">
<option value="">Select</option>
<?php foreach ($country as $count) { ?>
<option value="<?php echo $count["Country_id"]; ?>"><?php echo $count["Country"]; ?></option>
<?php } ?>
</select>
</div>
</div>
and model:
public function getCountry()
{
$this->db->select('*');
$this->db->from('tbl_country');
$query = $this->db->get();
return $query->result();
}
Print $country in view. Use echo ""; print_r($country);. I think you used foreach loop for data print and Country_id doesn't exist in database table column.So this error occured.
In CodeIgniter, when you pass data from controller to view.
You can access the passed data with keys.
In your example, you are passing $data['country'].
So, in view, you need to print echo '<pre>'print_r($country);echo '</pre>';
Please check it this was correct in your case.