this is the car controller
<?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]);
}
}
Routes
<?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';
this is where i want to display the value of the foreign key and it shows nothing
@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 hljs-string">">
<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 hljs-string">"></i>Model:{{$car->model}}</li>
<li><i hljs-string">"></i>{{$car->transmission}}</li>
<li><i hljs-string">"></i>{{$car->kilometrage}}kmpl</li>
</ul>
<div hljs-string">">
<a href="car-listing.html#" >Rent Car</a>
<a href="car-listing.html#" class="offer-btn-2">Details</a>
</div>
</div>
</div>
</div>
@endforeach
when i do this it shows me the id im new to laravel and i don't know how to make this work, can you help me with showing the value of the foreign key instead of the foreignkey itself thank you
<h3 value="">{{$car->brand_id}}</h3>
CodePudding user response:
Have you created a relation in Cars models?
example in Cars Model
public function brand() {
return $this->belongsTo(Brands::class,'brand_id');
}
at view page
$car->brand->brand_name
CodePudding user response:
You can create function in car model like this
public function brandDetails(){
return $this->hasOne('Brand model path', 'brand_table_key','car_table_foreignkey');
}
And this function use in with method
public function show(){
$cars = Car::with(['brandDetails']);
return view('car.car_listing',compact('cars'));
}