0

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.

Nargesh Rana
  • 134
  • 1
  • 15

2 Answers2

0

You can use foreach like this.

foreach ($user->product as $product) {

}

In controller. Notice that it's use like ->product and not like ->product() And notise it's more appropriate using products() inside Model.

And inside blade you can use. ince you have mentioned table I'm guessing you need to iterate <td> s inside blade foreach.

@foreach ($user->product as $product)

@endforeach
vimuth
  • 5,064
  • 33
  • 79
  • 116
  • it's not in . And in controller I added $user->product which is not working. – Nargesh Rana May 12 '20 at 22:23
  • error is "Trying to get property 'logo' of non-object" I have logo in user table. – Nargesh Rana May 12 '20 at 22:25
  • The reason may be there are no users in table. Try adding inside forelse instead of foreach. https://stackoverflow.com/questions/32652818/laravel-blade-check-empty-foreach#answer-32653175 – vimuth May 12 '20 at 22:27
  • Now getting this: Property [logo] does not exist on this collection instance. – Nargesh Rana May 12 '20 at 22:30
  • Try this answer. Think that is your issue https://stackoverflow.com/questions/41366092/property-title-does-not-exist-on-this-collection-instance#answer-41366122 – vimuth May 12 '20 at 22:34
  • Didn't work. Now Getting: Trying to get property 'logo' of non-object – Nargesh Rana May 12 '20 at 22:42
  • `
    @foreach ($collection as $product)

    {{ $product->text}}

    @endforeach
    `
    – Nargesh Rana May 12 '20 at 22:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213737/discussion-between-aruna-perera-and-nargesh-rana). – vimuth May 12 '20 at 22:48
0

Try it like this

Model user

public function user(){
    return $this->belongsTo(User::class);
}

Model product

public function product(){
    return $this->hasMany(Product::class, 'user_id');
}

If not right, put your migrates.