I am trying to create a multilingual project, I have a problem with routing and with the "show" function, I'm getting an error:
after entering the address: projekt.test/en/cars/2/
ErrorException (E_ERROR)
Trying to get property 'marka' of non-object (View: /var/www/projekt/resources/views/cars/show.blade.php)
after entering the address: projekt.test/pl/categories/3
ErrorException (E_ERROR)
Missing required parameters for [Route: carsCategories] [URI: {locale}/categories/{id}]. (View: /var/www/projekt/resources/views/template.blade.php) (View: /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">
</div>
from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2ItSc5V
via IFTTT
Aucun commentaire:
Enregistrer un commentaire