Reputation: 3
Why am I getting the exception:
Property [id] does not exist on this collection instance.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Goods;
use App\Categories;
class CategoriesController extends Controller
{
public function categoryAction($latin_url){
$category = Categories::where('latin_url', $latin_url)->get();
var_dump($category->id); die;
if ($category){
$goods = Goods::where('category_id', $category->id)->get();
return view('goods', ['goods' => $goods]);
}
}
}
Upvotes: 0
Views: 368
Reputation: 2541
$category = Categories::where('latin_url', $latin_url)->get();
This returns you a Collection of results, no only one result. So there is no id attribute. Use:
$category = Categories::where('latin_url', $latin_url)->first();
The above example does not work when there are 0 results, instead of this you can use:
$category = Categories::where('latin_url', $latin_url)->firstOrFail();
what will result in a HTTP 404 error when it does not exist
Upvotes: 2