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?
Related
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
I tried but it duplicated products, I wish to save one product under many category for one product_id.
Here my View Create (Product)
<div class="form-group row" id="category">
<label class="col-md-3 col-from-label">{{translate('Category')}} <span class="text-danger">*</span></label>
<div class="col-md-8">
<select class="form-control ml-selectpicker" name="category_id" id="category_id" data-live-search="true" required>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->getTranslation('name') }}</option>
#foreach ($category->childrenCategories as $childCategory)
#include('categories.child_category', ['child_category' => $childCategory])
#endforeach
#endforeach
</select>
</div>
</div>
And Here My control ( Create product )
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$categories = Category::where('parent_id', 0)
->where('digital', 0)
->with('childrenCategories')
->get();
return view('backend.product.products.create', compact('categories'));
}
And for save
$product->category_id = $request->category_id;
Here My model:
public function category()
{
return $this->belongsTo(Category::class);
}
I believe that there function like
$product->Categories()->sync([1, 3, 4]);
if You want to store a product under many categories then. You have to change Your Relationship to Many-to-many(a products can have many categories and a categories have many products).
So You have to create Three Tables
Products- to store products
categories- to store categories
category_products- pivot table
Then Your Models Will be Like below
Products Model
public function categories(){
return $this->belongsToMany(Category::class);
}
Category Model
public function products(){
return $this->belongsToMany(Product::class);
}
Then You can Simply use the function sync() to store a products under many categories
Many to many relationships in laravel
working with laravel 6 and need edit category options here.
PostController.php
public function edit($id)
{
$post = Post::find($id);
$categories = Category::all();
$cats = array();
foreach ($categories as $category) {
$cats[$category->id] = $category->name;
}
return view('posts.edit')->withPost($post)->withCategories($cats);
}
and edit.blade.php
<div class="form-group">
<label for="exampleFormControlSelect1">Category</label>
<select class="form-control" name="category_id" id="category_id">
#foreach($categories as $cats)
<option value="{{$post->category->id}}">{{$post->category->name}}</option>
#endforeach
</select>
</div>
but when I am going to edit page in the edit category options display only current category item. I need display all categories in the table? how could I manage this?
simply like this :
Controller
public function edit($id)
{
$post = Post::findOrFail($id);
$categories = Category::all();
return view('posts.edit', compact(['post', 'categories']));
}
View
<div class="form-group">
<label for="exampleFormControlSelect1">Category</label>
<select class="form-control" name="category_id" id="category_id">
#foreach($categories as $cat)
<option value="{{$cat->id}}" #if($cat->id === $post->category_id) 'selected' #endif >{{$cat->name}}</option>
#endforeach
</select>
</div>
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
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!