controller::
$states=$this->States->get_States();
$data=array('states'=>$states);
echo View::make('frontend.list-property')->with('data', $data);
list-property.blade.php:: // this is sub view
#extends('layouts.frontend.frontend_login')
#section('content')
<select>
{{$states_drpdown}} //when I use this statement I am getting error
</select>
#stop
frontend_login.blade.php //This is layout file
<?php
$states_drpdown='';
foreach ($data['states'] as $state):
$states_drpdown.='<option value="'.$state->sid.'">'.$state->statename.'</option>';
endforeach
?>
<select>
{{$states_drpdown}} //I am getting list of options here
</select>
#yield('content')
I am getting following error, can any one help me out pls.
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Method Illuminate\View\View::__toString() must not throw an exception
You may also share a piece of data across all views:
View::share('name', 'Steve');
You can find more about this here.
In your case, try: (warning untested code)
$states = $this->States->get_States();
$data = View::share('states', $states);
return View::make('frontend.list-property');
$data = array('states' => $this->States->get_States());
echo View::make('frontend.list-property', $data);
Assuming $this->States->get_States() returns a string of options, not an array?
<select>
{{$states}}
</select>
If it $this->States->get_States() returns an array, you could just do this (no need for <select> tags):
{{ Input::select('name-of-field', $states, Input::old('name-of-field')) }}
Consider returning the view instead of echoing.
Related
My dynamic drop down list is not working when I am trying to navigate to update page by using url id and then its showing errors.The result suppose to be a name of city that is associated with id in the database.
controller class
public function updateAddress($edit_id)
{
$data=[];
$data['edit_id']=$edit_id;
$address_data = $this->address->find($edit_id);
$data['first_name']=$address_data->first_name;
$data['last_name']=$address_data->last_name;
$data['street']=$address_data->street;
$data['zip_code']=$address_data->zip_code;
$data['city']=$address_data->city;
return view('update/edit',$data);
}
Here is my blade file. The rest of the fields are working properly except drop down list. I am using foreach loop to display data into the drop down list.
<select id="city" name="city" >
#foreach($data as $cities)
<option id="city" value="{{$cities->edit_id}}" selected="selected">{{$cities->city}}</option>
#endforeach
</select>
Update the code in the controller so that:
return view('update/edit',$data);
Is changed to:
return view('update/edit', ['data' => $data]);
change your controller code to
return view('update/edit',compact('data'));
and change your view page like
{{ $data['edit_id'] }}. you dont want to use #foreach.
using Laravel 5.6.28 my route on web.php file looks like this:
Route::get('kitysoftware/besttrades', 'SectorsController#besttradesview');
My controller with bestrasdeview function:
class SectorsController extends Controller
{
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
foreach ($sectors as $sector) {
echo $sector->SectorName;
}
//passing variable sectors1 to my view (is = $sector)
return view('besttradesview', ['sectors1' => $sector]);
}}
My view Bestradesview.blade.php is:
<form method="GET">
<div class="selectsector">
<Select class="selectsector" name = "sectors">
<option value="{{ sectors1 }}"></option>
<!-- test#2: <option value="{{ $sectors1->sector[] }}"></option> -->
<!-- test#3: <option value="{{ $sectors1->sector }}"></option> -->
</form>
</select>
And i get this error: Use of undefined constant sectors1 - assumed
'sectors1' (this will throw an Error in a future version of PHP)
When testing #2 commented line instead i get another error: Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Cannot use [] for reading
When testing #3 commented line without [] i get another error: Use of undefined constant sectors1 - assumed 'sectors1' (this will throw an Error in a future version of PHP)
Probably it's pretty simple but it's driving me nuts because i can't see why the variable is not passing to the view.
I know it's reading my table because if i remove the last return view call line on my controller, it echoes out all the values from my SectorName column, but i want it on a select dropdown menu.
I have been reading the docs, forums and watching laracast videos without luck. Any insight or just pointing me out to where to learn the proper sintax solution will be appreciatted.
Thanks in advance
It looks like you're trying to populate the select options from the query. If that's the case, your loop is in the wrong place.
Take the loop out of the controller method, and pass the $sectors collection directly to the view.
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
return view('besttradesview', ['sectors1' => $sectors]);
}
Then loop in the view to output the options.
<select class="selectsector" name="sectors">
#foreach($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
</select>
I assumed you wanted some text in the option. If you use the sector name as the option text it will also be used as the option value by default. If you want to use something else for the value, it would be like this, for example:
<option value="{{ $sector->id }}">{{ $sector->SectorName }}</option>
Change,
return view('besttradesview', ['sectors1' => $sector]);
To,
return view('besttradesview', ['sectors1' => $sectors]);
Notice the s in $sectors ($sectors = DB::table('Sectors')->get();) and in the actual view, it should be $sectors1 not just sectors1.
You also need to loop through actual sector:
#foreach ($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
Also, I would definitely suggest creating a model for your Sectors table rather than using the DB:: facade. You should make use of the M in MVC.
I have a simple controller function that fetch all records from db. but when i am trying to show all these records it show nothing. In fact it shows me hard coded foreach loop like this.
#foreach ($compactData as $value) {{ $value->Name }} #endforeach
this is my contoller function.
public function showallProducts()
{
$productstock = Product::all()->stocks;
$productoldprice = Product::all()->OldPrices;
$productcurrentprice = Product::all()->CurrentPrice;
$compactData=array('productstock', 'productoldprice', 'productcurrentprice');
return view('welcome', compact($compactData));
}
this is my view
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="title m-b-md">
All products
</div>
<table>
<tbody>
#foreach ($compactData as $value)
{{ $value->Name }}
#endforeach
</tbody>
</table>
</div>
</div>
</body>
why it is behaving like this. any solution?? I am using phpstorm version 17. Is their any setting issue to run project because what ever project I ran it gives me the only page which i ran with only html?
My route is.
Route::get('/', function () {
$action = 'showallProducts';
return App::make('ProductController')->$action();
});
Have you checked your $compactData variable? Please dd($compactData) to see what it contains.
Problem 1
You are accessing a relational property as a property of Eloquent collection, like this:
Product::all()->stocks
which is not correct. Because the Collection object doesn't have the property stocks but yes the Product object might have a stocks property. Please read the Laravel documentation about Collection.
Problem 2
$compactData = array('productstock', 'productoldprice', 'productcurrentprice');
This line creating an array of 4 string, plain string not variable. So, your $compactData is containing an array of 4 string. If you want to have a variable with associative array then you need to do the following:
$compactData = compact('productstock', 'productoldprice', 'productcurrentprice');
Problem 3
return view('welcome', compact($compactData));
Here you are trying to pass the $compactDate to the welcome view but unfortunately compact() function doesn't accept variable but the string name of that variable as I have written in Problem 2. So, it should be:
return view('welcome', compact('compactData'));
Problem 4
Finally, in the blade you are accessing each element of the $compactData data variable and print them as string which might be an object.
You most likely have a problem with your web server.
Try to use Laravel Valet as development environnement.
Edit : I found this : Valet for Windows
I think you didn't mention the blade in the name of the view file by which it is saved. So change the name of the file by which it is save to something like:
filename.blade.php
and try again.
Explanation:
#foreach ($compactData as $value) this is the syntax of blade template engine, and to parse and excute it, you have to mention the blade extension in the name.
I got value from database and then i need to set those value to textbox. I have created a controller file with the method name of edit look like below
userdata.blade.php:
public function edit($id)
{
echo "You have clicked edit link".$name;
$editdata = DB::table('newuser')->where('Id','=',$id)->get();
return View::make('editdata',array('list' => $editdata));
}
I have passed array of value as parameter to the view file.now i need to diaplay the value of name to textbox.how to do that in html page using laravel. My html page look like below
editdata.blade.php:
<html>
<head></head>
<body>
<div>
{{Form::open(array('url' => 'login', 'method' => 'post'))}}
{{Form::label('name','Name',array('id'=>'label-name'))}}
{{Form::text('name',{{$list->Name}}}}
{{ Form::close() }}
</div>
</body>
</html>
can anyone tell me that what mistake i did.Thanks in advance
Just remove the curly brackets, you are already "inside" PHP code and don't need them:
{{ Form::text('name',$list->Name) }}
Also you get a collection from your controller you probably want to do:
$editdata = DB::table('newuser')->where('Id','=',$id)->first();
Or even:
$editdata = DB::table('newuser')->find($id);
get() returns a collection (multiple rows) and not the model itself. You can use User::find($id) which gives you direct access to the model with the specified Id.
When not using eloquent just replace get() with first()
Simple view not rendering:
public function getFranchise() {
echo 'getFranchise';
$franchiseData['shopViews'] = array(1 => 'a');
$franchiseData['aaa'] = 'bbb';
return View::make('test.filteredData.franchise', $franchiseData);
}
view in test/filteredData/franchise.blade.php
franchise
{{--$shopsViews}} {{-- Fatal error: Method Illuminate\View\View::__toString() must not throw an exception in D:\projektai\dashboard\app\storage\views\d2973247ea68aed2fdfd33dc19cccada on line 5}}
{{ $aaa }}
#foreach ($shopsViews as $shop)
<strong>aaa</strong>
#endforeach
Only word getFranchise is displayed which means controller function is called. No any errors, no anything. WHat is that?
Even in constructor is added
ini_set("display_errors", true);
Edited
Found that this:
{{--$shopsViews}} {{-- Fatal error: Method Illuminate\View\View::__toString() must not throw an exception in D:\projektai\dashboard\app\storage\views\d2973247ea68aed2fdfd33dc19cccada on line 5}}
comment was causing stop of execution in the script. WHy is that? this is valid laravel comment. Also I noticed weird thigs when I comment
<?php //print_r ?>
then it shows something like web page not found, like interent connection has gone. I dont get at all whats happening with commenting.
Your blade view must contain #extends() and #section() to work in this case.
And comment should look like this {{-- $shopsViews --}}. That should fix your problem.
#extends('your_layout_folder.layout')
#section('content')
#foreach ($shopsViews as $shop)
<strong>aaa</strong>
#endforeach
#stop
Please follow the documentation! http://laravel.com/docs