laravel显示外键值而不是key



这是汽车控制器

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsBrand;
use AppModelsCar;
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 AppHttpControllersUserController;
use IlluminateSupportFacadesRoute;
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', 'AppHttpControllersCarController@index');
Route::get('/dashboard/create', 'AppHttpControllersCarController@create');
Route::post('/dashboard', 'AppHttpControllersCarController@store');
Route::get('/dashboard/create_brand', 'AppHttpControllersBrandController@create');
Route::post('/dashboard/create_brand','AppHttpControllersBrandController@store');
Route::get('/dashboard/create_brand', 'AppHttpControllersBrandController@show');
});
Route::group( ['middleware' => ['auth', 'role:user']], function(){
Route::get('/profile', function(){
return view('profile/profile');
});    
});
Route::get('/car_listing', 'AppHttpControllersCarController@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

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

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

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

Cars Model中的例子

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

查看页面

$car->brand->brand_name

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

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

这个函数使用in with方法

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

相关内容

  • 没有找到相关文章

最新更新