简体   繁体   中英

Laravel mcamara/laravel-localization package pass to view

Laravel version 7.6.2

i'm trying to use mcamara/laravel-localization package.

https://github.com/mcamara/laravel-localization

I followed the instruction the they give in their github page and made some personalization that allow me to use another field instead using the id to reach.

These are my Routes:

use Illuminate\Support\Facades\Route;


Route::group(
    [
        'prefix' => LaravelLocalization::setLocale(),
        'middleware' => [ 'localize', 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
    ],function () {

    Route::get('/', 'HomeController@index')->name('home');
    Route::get(LaravelLocalization::transRoute('routes.vehicle'), 'VehicleController@index')->name('vehicle');
    Route::get(LaravelLocalization::transRoute('routes.vehicle/{vehicle_url}'), 'VehicleController@show')->name('vehicle.show');

});

Vehicle table structure:

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();
    });
}

Vehicle Slug table structure:

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();
    });
}

This the Vehicle Model:

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');
    }
}

This is the Controller:

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'));
    }

This is 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>

This is the array used to translate (/resourse/lang/de/routes.php).

return [
    "vehicles"   => "fahrzeuge",
    "vehicle/{vehicle_url}"  =>  "fahrzeug/{vehicle_url}",
]; //The English file is the same with vehicles and vehicle instead of fahrzeuge and farhzeug.

Well if I type app.loc/de/fahrzeuge/fahrrader on the address bar it works perfectly and i can reach the bikes page with the proper translation, but not able to translate the app.loc/de/fahrzeuge/{slug} slug using the link from the view.

Surely i miss something. Can someone help?

I had the same problem, I was looking for and I gave this post it is related to the solution here , but I use the following packages Astrotomic and mcamara

Apparently you have a bad declared your translated route, you have to go without the parameter

Route::get(LaravelLocalization::transRoute('routes.vehicle'), 'VehicleController@show')->name('vehicle.show');

that the route file is solved in the corresponding language

/resourse/lang/de/routes.php

This is the solution that I have used with the previous packets.

    public function state(State $state)
    {
        $state = State::whereTranslation('slug', $state->slug)->firstOrFail();
        if ($state->translate()->where('slug', $state->slug)->first()->locale != app()->getLocale()) 
        {
        return redirect()->route('state.show', $state->translate()->slug);
        }

        return view('front.states.state', compact('_label', 'state'));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM