Php Laravel mcamara/Laravel本地化软件包通过查看
Laravel版本7.6.2 我正在尝试使用McMara/laravel本地化包 我按照他们在github页面中给出的说明进行了一些个性化设置,允许我使用另一个字段,而不是使用id来访问 以下是我的路线: 车辆工作台结构:Php Laravel mcamara/Laravel本地化软件包通过查看,php,laravel,localization,multilingual,Php,Laravel,Localization,Multilingual,Laravel版本7.6.2 我正在尝试使用McMara/laravel本地化包 我按照他们在github页面中给出的说明进行了一些个性化设置,允许我使用另一个字段,而不是使用id来访问 以下是我的路线: 车辆工作台结构: public function up() { Schema::create('vehicles', function (Blueprint $table) { $table->id(); $table->string('
public function up()
{
Schema::create('vehicles', function (Blueprint $table) {
$table->id();
$table->string('vehicle_url')->unique();
$table->string('name_en');
$table->string('name_de');
$table->text('descr_en');
$table->text('descr_de');
$table->timestamps();
});
}
public function up()
{
Schema::create('vehicle_slugs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('vehicle_id');
$table->string('locale',2);
$table->string('slug')->unique();
$table->timestamps();
});
}
车辆段塞台结构:
public function up()
{
Schema::create('vehicles', function (Blueprint $table) {
$table->id();
$table->string('vehicle_url')->unique();
$table->string('name_en');
$table->string('name_de');
$table->text('descr_en');
$table->text('descr_de');
$table->timestamps();
});
}
public function up()
{
Schema::create('vehicle_slugs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('vehicle_id');
$table->string('locale',2);
$table->string('slug')->unique();
$table->timestamps();
});
}
这与车辆型号有关:
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\VehicleSlugs;
class Vehicle extends Model implements \Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable
{
public function getRouteKeyName()
{
return 'vehicle_url';
}
public function slugs()
{
return $this->hasMany(VehicleSlugs::class);
}
public function getLocalizedRouteKey($locale)
{
return $this->slugs->where('locale', '=', $locale)->first()->slug;
}
public function resolveRouteBinding($slug, $field = NULL)
{
return static::whereHas('slugs', function ($q) use ($slug) {
$q->where('slug', '=', $slug);
})->first() ?? abort('404');
}
}
这是控制器:
namespace App\Http\Controllers;
use App\Vehicle;
use Illuminate\Http\Request;
class VehicleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('vehicles');
}
/**
* Display the specified resource.
*
* @param \App\Vehicle $vehicle
* @return \Illuminate\Http\Response
*/
public function show(Vehicle $vehicle_url)
{
return view('vehicle', compact('vehicle_url'));
}
这是vehicle.blade.php:
<!DOCTYPE html>
<html lang="{{app()->getLocale()}}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Testing mcamara</title>
</head>
<body>
<a href="{{route('home')}}">Home</a>
<a href="{{route('vehicle')}}">Vehicle</a>
@forelse(App\Vehicle::all() as $vehicle)
<a href="{{route('vehicle.show', $vehicle->vehicle_url)}}">{{$vehicle->name_en}}</a> <!-- For Example Bikes/Motocycles/Cars/.. -->
@empty
<a href="#">No Link</a>
@endforelse
</body>
</html>
如果我在地址栏上键入app.loc/de/fahrzeuge/fahrrader,它工作得很好,我可以通过正确的翻译到达bikes页面,但无法使用视图中的链接翻译app.loc/de/fahrzeuge/{slug}slug
我肯定错过了什么。有人能帮忙吗