Passing variable from controller in components/blade - php

i have this situation.
Undefined variable: city (View: \newjoblist\resources\views\components\hero.blade.php)
in my controller i have
<?php
namespace App\Http\Controllers;
use App\Models\City;
use Illuminate\Http\Request;
class CitiesController extends Controller
{
public function index(Request $request){
$city = City::orderBy('name') // variable displayed in view
->get();
// return view('components.hero')->with('city',$city);
$this->view('<components.hero')->with('city', $this->city);
}
}
and in components/hero.blade.php
<select class="form-group" id="s" name="s">
<option value=""></option>
#foreach ($city as $cities)
<option value="{{$cities}}">{{$cities->name}}</option>
#endforeach
</select>
i don't know if i need to do something in web.php

you need to update your index method to
public function index(Request $request){
$cities = City::orderBy('name') // variable displayed in view
->get();
return view('components.hero')->with('cities',$cities);
}
and at the view
<select class="form-group" id="s" name="s">
<option value=""></option>
#foreach ($cities as $city)
<option value="{{$city->id}}">{{$city->name}}</option>
#endforeach
</select>

You can also do this
public function index(Request $request)
{
$cities = City::orderBy('name') // variable displayed in view
->get();
return view('components.hero', compact('cities'));
}

You pass in your code city as class membervariable but previously you assign only to $city.
Change this line: $this->view('<components.hero')->with('city', $this->city); to view('components.hero')->with('city',$city); . What you already had a line above ;-)
You can also pass Vars to your blade like that:
view('components.hero', ['city' => $city];

I read the comment i find out that you are trying another way to get the data.
So you can do it like this.
<?php
namespace App\Http\Controllers;
use App\Models\City;
use Illuminate\Http\Request;
class CitiesController extends Controller
{
private $city;
public function __construct() {
$this->city = new City();
}
public function index(Request $request) {
$cities = $this->city->query()->orderBy('name')->get();
return view('<components.hero')->with('cities', $cities);
}
}
And in the view:
<select class="form-group" id="s" name="s">
<option value=""></option>
#foreach ($cities as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>

is it possible to continue the error because I want to display in components.hero where is another controller? this is my web.php....
Route::get('/', [Controllers\ListingController::class, 'index']) ->name('listings.index');
and i have others views in this controller
UPDATE*
if I use diff. the route in web.php it's works
Route::get('/search', [Controllers\CitiesController::class, 'index']);
-> displaying cities from DB

Related

How to edit multi value selection inputs in Laravel 6?

working with Laravel 6 and mysql. I have mutiple values input using select2-multi. I have following model structure with Post and Tags Post.php model
public function category()
{
return $this->belongsTo('App\Category');
}
public function tags()
{
return $this->belongsToMany('App\Tag');
}
Tag.php Model
public function posts(){
return $this->belongsToMany('App\Post');
}
and My PostController edit function is
public function edit($id)
{
$post = Post::find($id);
$categories = Category::all();
$tags = Tag::all();
return view('posts.edit')->withPost($post)->withCategories($cats)->withTags($tags);
}
and edit blade is
<div class="form-group">
<label for="exampleFormControlSelect1">Tag</label>
<select class="form-control select2-multi" name="tags[]" id="tags" multiple="multiple">
#foreach($tags as $tag)
<option value="{{$tag->id}}" #if($tag->id === $post->tag_id) 'selected' #endif >{{$tag->name}}</option>
#endforeach
</select>
</div>
but when I try to visit edit blade view following error is coming Undefined variable: tags (View: F:\2020 technologies\laravel\blog\resources\views\posts\edit.blade.php)
how could I fix this matter?

Listing the names of one module into the edit section of another module using relations

Here are both the models.
Faculty.php
<?php
namespace App\Model\Admin;
use Illuminate\Database\Eloquent\Model;
class Faculty extends Model
{
protected $guarded = [];
public function streams()
{
return $this->hasMany(Stream::class);
}
}
Stream.php
<?php
namespace App\Model\Admin;
use Illuminate\Database\Eloquent\Model;
class Stream extends Model
{
protected $guarded = [];
public function faculty()
{
return $this->belongsTo(Faculty::class);
}
public function chapters()
{
return $this->hasMany(Chapter::class);
}
}
StreamController.php
public function edit(Faculty $faculty, Stream $stream, $id)
{
$stream = Stream::find($id);
$faculty = Faculty::all('name', 'id');
return view('admin.stream.edit')->with(['stream' => $stream, 'faculty' => $faculty]);
}
Blade VIew
<div class="form-group">
<label for="status">Select Faculty</label>
<select name="faculty_id" id="" class="form-control form-control-sm">
#foreach($faculty as $item)
<option value="{{ $item->id }}" >{{ $item->name }} </option>
#endforeach
</select>
</div>
I am getting the list but what I want is to show the selected one first since this is the edit section.
How can this be achieved?
Help
You have to change this. And you don't need to pass the $id Because you have the value of $tream already. So don't need this line $stream = Stream::find($id); also.
public function edit(Faculty $faculty, Stream $stream, $id)
{
$stream = Stream::find($id);
$allFaculty = Faculty::all('name', 'id');
return view('admin.stream.edit',compact('faculty','stream','allFaculty'));
}
And your view update this
#foreach($allFaculty as $item)
<option value="{{ $item->id }}" #if($faculty->id==$item->id) selected #endif >{{ $item->name }} </option>
#endforeach

Laravel controller pass HTML element?

The element I want to pass to view:
<option value="UK">UK</option>
<option value="Mexico">Mexico</option>
//Something like this...
Part of my code:
foreach($data as $row)
{
$output .= '<option value='."$row->Country".'>'.$row->Country.'</option>';
}
It will return:
{"table_data":"German<\/option>Mexico<\/option>Mexico<\/option>UK<\/option>Brazil<\/option>UK<\/option>","total_data":6}
So, It can only print the name between the , but can not print out the value inside the open tag.
How can I solve this?
why not you just do it like this:
in the controller
public function country(){
$countries=Country::all();
return view('country',compact('countries'));
}
in the blade view
<select>
#foreach($contries as $country)
<option value="{{$country->name}}">{{$country->name}}</option>
#endforeach
</select>
Please Check This In your Controller passing the countries
public function country(){
$countries=Country::all();
return view('your_blade_file',compact('countries'));
}
In your blade option value is $country->id option show it $country->name
<select>
#foreach($contries as $country)
<option value="{{$country->id}}">{{$country->name}}</option>
#endforeach
</select>

Undefined variable: categories in Blade Laravel 6.4.0

I am trying to show my categories on the site map page with the controller below.
CategoryController.php
class CategoriesController extends Controller
{
public function create()
{
$categories = Category
::orderBy('name','desc')
->where('parent_id', NULL)
->get();
return view('admin.category.create', compact('categories'));
}
}
The following is the part of my Blade file where I use the categories variable template.
create.blade.php
<div class="form-group">
<label for="exampleInputPassword1">Parent Category</label>
<select name="parent_id" class="form-control">
#foreach ($main_categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
I use every way to get variable and passing but none the didn't work do you have any suggestion?
You've got two options here.
Option A:
You named the collection categories in your Controller and pass it to the view as such. To access it in your view, you need to reference it by the same name.
Change this:
// Your view is looking for a collection titled `main_categories`, which does not exist
#foreach ($main_categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
To this:
#foreach ($categories as $c)
<option value="{{ $c->id }}">{{ $c->name }}</option>
#endforeach
Option B:
Pass the data back using main_categories as the collection name
Change this:
return view('admin.category.create', compact('categories'));
To this:
return view('admin.category.create', [
'main_categories' => $categories
]);
You can read up more on passing data to views here.
To be more clear Ill break your code as below and add some details
class CategoriesController extends Controller
{
public function create()
{
$categories = Category
::orderBy('name','desc')
->where('parent_id', NULL)
->get();
// Your are passing variable but I change it as below to be more clear
// return view('admin.category.create', compact('categories'));
//now you are passing the value to the view
return view('admin.category.create', ['categories' => $categories]);
}
}
Now lets catch it in the view. Note that now $categories available in the view. If you pass ['A' => $categories] then view has $A variable so you should call for the relevant variable that you define in the controller.
<div class="form-group">
<label for="exampleInputPassword1">Parent Category</label>
<select name="parent_id" class="form-control">
**{{-- In here you should pass $categories --}}**
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>

How to get Select Value in Laravel

Hello I want to try to get the value of the selected option but i always getting a null value when i dump out the variable.
My Select looks like this:
<div class="col-md-6">
<!--<input id="members" type="text" class="form-control" name="members">-->
<select name="members" class="js-example-basic-multiple js-states form-control" id="members" multiple="multiple" >
<!-- #foreach($users as $user)
<option value='{{$user->id}}'>{{$user->name}}</option>
#endforeach-->
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>
#if ($errors->has('members'))
<span class="help-block">
<strong>{{ $errors->first('members') }}</strong>
</span>
#endif
</div>
And my Controller function looks like this:
$sprintname = $request->input('sprintname');
$startdate = $request->input('startdate');
$enddate = $request->input('enddate');
$members = $request->input('members');
dd($members);
I really dont know whats wrong and i hope someone can help me.
The other inputs are fine but only with the select i get a null value.
you used multiple select try name="members[]"
and in Controller
public function Postdata(Request $request) {
$value = $request->members; }
Or you can do:
dd( request()->all() );
To check all of the data.
If you are using a model define it in the controller and in the Routes.php file.
Here what I have used in my sample project.I haven't use a controller for this.Got good results for laravel 5.0,5.1 and 5.2.
Category.php (this is the model in app directory)
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Input;
class Category extends Model
{
protected $fillable = ['name'];
protected $table = 'categories';
public function category()
{
return $this->belongsTo('App\Category');
}
}
in Routes.php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Category;
Route::get('/',function()
{
$categories = Category::all();
return view('category.index')->with('categories',$categories);
});
in the view
index.blade.php
<div class="form-group">
<label for="">Categories</label>
<select class="form-control input-sm" name="category" id="category">
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}} </option>
#endforeach
</select>
</div>
This work like a charm.Hope this information will be helpful for you!

Categories