James
James

Reputation: 400

Laravel 5.2 How to access Foreign Key in View

i know this question already asked before. But i still can't understand how it works, so i ask again. The title explain my question everything.

I have 2 tables, products and product_types

in products

id
name
type_id

in product_types

id
type_name

Here's my Product Model

class Product extends Model
{
    protected $table = 'products';
    protected $primaryKey = 'id';

    public function productType() {
        return $this->hasOne('App\ProductType', 'type_id');
    }
}

Here's my ProductType Model

class ProductType extends Model
{
    protected $table = 'product_types';
    protected $primaryKey = 'id';

    public function product() {
        return $this->hasMany('App\Product');
    }
}

Here's my Controller for my view

public function viewManageProduct() {
    $products = Product::all();
    $productTypes = ProductType::all();
    return view('manages.products')->with(['products' => $products]);
}

my View

@foreach ($products as $product)
<table>
    <tr>
        <td>{{ $product->name }}</td>
        <td>{{ $product->productType->type_name }}</td>
    </tr>
</table>
@endforeach

I don't know how to access the type_name . Can anyone help? Thanks in advance

Upvotes: 1

Views: 2123

Answers (4)

James
James

Reputation: 400

Nevermind, i already find the answer. Thanks for your reply anyway.

Here's my code:

products table migration

Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->integer('mediumPrice');
    $table->integer('largePrice');
    $table->string('image');
    $table->string('product_type_id')->references('product_type_id')->on('product_types');
    $table->timestamps();
});

product_types table migration

Schema::create('product_types', function (Blueprint $table) {
    $table->increments('product_type_id');
    $table->string('product_type_name');
    $table->timestamps();
});

Product model

class Product extends Model
{
    protected $table = 'products';
    protected $primaryKey = 'id';

    public function product_type() {
        return $this->hasOne('App\ProductType', 'product_type_id', 'product_type_id');
    }
}

ProductType model

class ProductType extends Model
{
    protected $table = 'product_types';
    protected $primaryKey = 'product_type_id';

    public function product_type() {
        return $this->hasMany('App\Product', 'product_type_id', 'product_type_id');
    }
}

view controller

public function view() {
    $products = Product::all();
    return view('manages.products')->with(['products' => $products]);
}

And last my HTML View

@foreach ($products as $product)
<table>
    <tr>
        <td>{{ $product->name }}</td>
        <td>{{ $product->product_type['product_type_name'] }}</td>
    </tr>
</table>
@endforeach

Once again, thank you for your reply. That's really a big help, thank you.

Upvotes: 1

lakshmanan1993
lakshmanan1993

Reputation: 1

In your Product model change the function like this,

public function productType() {
    return $this->belongsTo('App\ProductType', 'type_id');
}

Hope this will work.

Upvotes: 0

linktoahref
linktoahref

Reputation: 7992

There is no foreign key on product_types table for one-to-one relationship. So create a foreign key column in the product_types table.

+============+          +===================+
|  Products  |          |   ProductTypes    |
+============+          +===================+
|     id     |  ---|    |        id         |
+------------+     |    +-------------------+
|    name    |     |--> |   <foreign_key>   |
+------------+          +-------------------+
|   type_id  |          |      type_name    |
+------------+          +-------------------+

Your hasOne relationship would be

public function productType() {
    return $this->hasOne('App\ProductType', 'foreign_key');
}

and hasMany relationship would be

public function product() {
    return $this->hasMany('App\Product', 'type_id');
}

Documentation

Upvotes: 0

Siva Ganesh
Siva Ganesh

Reputation: 1445

Here is the Simplest Way In Your Controller

$products = Product::all();
$productTypes = ProductType::all();

$data['products'] = $products;
$data['productTypes'] = $productTypes;
return view('manages.products',$data);

In Your View Page you can get like this $products and $productTypes. In Your View Page

    @foreach ($products as $product)
<table>
    <tr>
        <td>{{ $product->name }}</td>
        <td>{{ $productTypes->type_name }}</td>
    </tr>
</table>
@endforeach

Hopes Its Work

Upvotes: 0

Related Questions