我正在尝试创建一个多语言项目,我在路由和“显示”功能上遇到问题,我遇到了错误:
输入地址后:projekt.test/en/cars/2/
ErrorException(E_ERROR)
试图获取非对象的属性“标记”(查看:/var/www/projekt/resources/views/cars/show.blade.php)
输入地址后:projekt.test/pl/categories/3
ErrorException(E_ERROR)
[路线:carsCategories] [URI:{locale} / categories / {id}]缺少必需的参数。 (查看:/var/www/projekt/resources/views/template.blade.php)(查看:/var/www/projekt/resources/views/template.blade.php)
web.php
Route::get('/', function () {
return redirect(app()->getLocale());
});
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'SetLocale'], function() {
Route::get('/', function () {
return view('welcome');
});
Route::get('cars/', 'CarController@index')->name('Car');
Route::get('cars/{id}/', 'CarController@show')->name('cars');
Route::get('cars/categories/{id}', 'CarController@CarsByCategory')->name('carCategories');
});
CarController.php
<?php
namespace App\Http\Controllers;
use App;
use Illuminate\Http\Request;
use App\Models\Car;
use App\Repositories\CarRepository;
class CarController extends Controller
{
public function index(CarRepository $carRepository, $locale){
$cars = $carRepository->getAllCars();
App::setLocale($locale);
session()->put('locale', $locale);
return view('cars.list',["CarsList"=>$cars,
"title"=>"ok"]);
}
public function show(CarRepository $carRepository, $id, $locale){
$carDetail = $carRepository->find($id);
App::setLocale($locale);
session()->put('locale', $locale);
return view('cars.show', ["carDetail" => $carDetail]);
}
public function CarsByCategory(CarRepository $carRepository, $id){
$cars = $carRepository->getCarsByCategory($id);
return view('cars.list',["CarsList"=>$cars,
"title"=>"ok"]);
}
}
CarRepository.php
<?php
namespace App\Repositories;
use App\Models\Car;
use DB;
class CarRepository extends BaseRepository {
public function __construct(Car $model){
$this->model = $model;
}
public function getAllCars(){
return $this->model->where('status', 'online')->get();
}
public function getCarsByCategory($id){
return $this->model->where('status', 'online')->whereHas('category',
function ($query) use ($id)
{
$query->where('categories.id',$id);
})->orderBy('marka','asc')->get();
}
}
show.blade.php
<div class="card-header">
{{ $carDetail->marka }}
</div>