【问题标题】:laravel display foreign key value instead of keylaravel 显示外键值而不是键
【发布时间】:2021-12-14 11:56:02
【问题描述】:

这是汽车控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Brand;
use App\Models\Car;

class CarController extends Controller
{
    public function index(){
        return view('/dashboard/index');
    }
    public function create(){
        $brand = Brand::all();
        return view('dashboard.create')->with('brand', $brand);
    }
    public function store(Request $request){
        $car = new Car();

        $car->brand_id = request('brand_id');
        $car->model = request('model');
        $car->kilometrage = request('kilometrage');
        $car->engine = request('engine');
        $car->description = request('description');
        $car->price = request('price');
        $car->transmission = request('transmission');
        if($request->hasfile('image')){
            $file = $request->file('image');
            $extention = $file->getClientOriginalExtension();
            $filename = time().'.'.$extention;
            $file->move('img/', $filename);
            $car->image = $filename;
        }

        $car->save();

        return redirect('/dashboard/create');
    }

    public function show(){

       // $brand = Car::with('brand');
        $cars = Car::all();
        return view('car.car_listing', ['cars' => $cars]);    
    }  
}

路线

<?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

//
/*Route::get('/dashboard', function () {
    return view('layouts/dashboard');
})->middleware(['auth'])->name('dashboard');*/


Route::group( ['middleware' => ['auth', 'role:administrator']], function(){
    Route::get('/dashboard', 'App\Http\Controllers\CarController@index');
    Route::get('/dashboard/create', 'App\Http\Controllers\CarController@create');
    Route::post('/dashboard', 'App\Http\Controllers\CarController@store');
    Route::get('/dashboard/create_brand', 'App\Http\Controllers\BrandController@create');
    Route::post('/dashboard/create_brand','App\Http\Controllers\BrandController@store');
    Route::get('/dashboard/create_brand', 'App\Http\Controllers\BrandController@show');

});

Route::group( ['middleware' => ['auth', 'role:user']], function(){
    Route::get('/profile', function(){
        return view('profile/profile');
    });    
});

Route::get('/car_listing', 'App\Http\Controllers\CarController@show');

Route::resource('user', UserController::class)->shallow();
//Route::get('user', [UserController::class, 'index'])->name('user.index');

require __DIR__.'/auth.php';

这是我想显示外键值的地方,它什么也不显示

@foreach($cars as $car)
    <div class="col-md-6">
        <div class="single-offers">
            
            <div class="offer-image">
                <a href="car-listing.html#">
                    <img src="{{ asset('img/'.$car->image) }}" alt="offer 1" />
                </a>
            </div>
            
            <div class="offer-text">
                    <div>
                        <a href="car_listing">
                            <h3 value="{{$car->brand_id}}">{{$car->brand_name}}</h3>
                        </a>
                    </div>
               
                
                <h4>${{$car->price}}<span>/ Day</span></h4>
                <ul>
                    <li><i class="fa fa-car"></i>Model:{{$car->model}}</li>
                    <li><i class="fa fa-cogs"></i>{{$car->transmission}}</li>
                    <li><i class="fa fa-dashboard"></i>{{$car->kilometrage}}kmpl</li>
                </ul>
                <div class="offer-action">
                    <a href="car-listing.html#" class="offer-btn-1">Rent Car</a>
                    <a href="car-listing.html#" class="offer-btn-2">Details</a>
                </div>
                
            </div>
            
        </div>
    </div>
    
@endforeach

当我这样做时,它会向我显示我是 laravel 的新手,我不知道如何进行这项工作,你能帮我显示外键的值而不是外键本身吗?谢谢

<h3 value="">{{$car->brand_id}}</h3>

【问题讨论】:

标签: php eloquent foreign-keys laravel-8


【解决方案1】:

您是否在 Cars 模型中创建了关系?

汽车模型中的示例

public function brand() { 
 return $this->belongsTo(Brands::class,'brand_id'); 
}

在查看页面

$car->brand->brand_name

【讨论】:

  • 非常感谢我不知道汽车模型中的功能它是如此有用
  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

你可以像这样在汽车模型中创建函数

public function brandDetails(){
    return $this->hasOne('Brand model path', 'brand_table_key','car_table_foreignkey');
}

这个函数用在with方法中

public function show(){
    $cars = Car::with(['brandDetails']);
    return view('car.car_listing',compact('cars'));  
} 

【讨论】:

  • 我遵循@nakin 建议的任何方式都可以正常工作,并且关于您的解决方案,它返回了一个空白页,谢谢
猜你喜欢
  • 2019-12-07
  • 2013-08-18
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 2016-03-17
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
相关资源
最近更新 更多