Thank you watching my question.
I have one Error in Laravel5.6
I want to display Category and Subcategory. I was displayed Category but Subcategory is can't display. Error is this "Invalid argument supplied for foreach()"
I know mean this error. I checking my database and code. but i think no problem.
try use (array) in this code
@foreach ($store->sub_categories as $sub_category) @endforeach </p>
but not working
please tell me problem.
thank you.
web.php
<?php
Route::get('/', function () {
return view('welcome', ['stores' => App\Store::all(), 'categories' => App\Category::all(), 'sub_categories' => App\subCategory::all()]);
});
Route::post('/', function () {
$store = new \App\Store();
$store->name = request()->name;
$store->location = request()->location;
$store->price = request()->price;
$store->open_time = request()->open_time;
$store->closed_date = request()->closed_date;
$store->tell_number = request()->tell_number;
$store->save();
$store->Categories()->attach(request()->category);
$store->subCategories()->attach(request()->sub_category);
redirect('/');
});
welcome.blade.php
<body>
<form method="post">
@foreach ($categories as $category)
<input type="checkbox" name="category[]" value="">
@endforeach
<br>
@foreach ($sub_categories as $sub_category)
<input type="checkbox" name="sub_category[]" value="">
@endforeach
<br>
<input name="name"><br>
<input name="location"><br>
<input name="price"><br>
<input name="open_time"><br>
<input name="closed_date"><br>
<input name="tell_number"><br>
<button>投稿</button>
</form>
@foreach ($stores as $store)
<hr>
<p>Category: @foreach ($store->categories as $category) @endforeach </p>
<p>Sub category: @foreach ($store->sub_categories as $sub_category) @endforeach </p>
<p></p>
@endforeach
</body>
Store.php
class Store extends Model
{
public function Categories(){
return $this->belongsToMany(Category::class);
}
public function subCategories(){
return $this->belongsToMany(subCategory::class);
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire