I have laravel 7.7 app which contains multiple user and all users have products. Now I want to display a user profile for guest visitor on www.domain.com/company/user-name . All the product that the users has created should be displayed on the same page along with user profile.
All product are being saved with user_id.
My Product Model has:
// User relation
public function user(){
return $this->belongsTo('Add\User', 'user_id');
}
My User Model has:
public function product(){
return $this->hasMany('App\Product');
}
CompanyController has:
public function show($slug)
{
// Show individual company
$user = User::where('slug', $slug)->first();
return view('company.show')->with('user', $user);
}
Migration:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('image');
$table->string('title');
$table->string('text');
$table->string('price');
$table->integer('user_id');
$table->timestamps();
});
}
I need help understanding how to grab/display all products of user from user_id in product table in CompanyController. Without using auth.