DaniloP's avatar

Getting data from a foreing key Laravel7

I don't know how to get the data from this foreign key I have followed all steps from the documentation, but I still don't know what needs to be done I've tried all the possible solution that i found online and still i don't get it

This is my product model:


namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public function sector(){
        return $this->hasOne('App\Sector');
    }
    public function sale(){
        return $this->hasOne('App\Sale');
    }
}

This is my Sector Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Sector extends Model
{
    protected $primaryKey = 'products_id';

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

This is my controller (only index):

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;
use App\Sector;
class ProductController extends Controller
{
    /**
     * Display a listing of the resource
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       $products = Product::all();
       return view('worker(dot*)index', compact('products'));
    }

And this is my view (only the interested part):


@foreach ($products as $product)
    <div class="card" style="width: 18rem;">
        <div class="card-body">
        <h5 class="card-title">{{$product->name}}</h5>
        <p class="card-text">{{$product->codice_prodotto}}</p>

        {{-- <p class="card-text">{{$product->sectors->products_id}}</p> i've tried this and that gets' me this error
        Trying to get property 'products_id' of non-object         --}}

        {{-- <p class="card-text">{{$product->sectors['products_id']}}</p> i've tried this and that get's me this error
        Trying to access array offset on value of type null

        --}}

And these are my migration

Products

Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->integer('codice_prodotto');
            $table->integer('codice_stock');
            $table->date('data_di_scadenza');
            $table->string('name');
            $table->text('description');
            $table->decimal('costo', 10, 2);
            $table->timestamps();
        });

Sectors

Schema::create('sectors', function (Blueprint $table) {
            $table->foreignId('products_id')->constrained();
            $table->integer('codice_stock');
            $table->string('settore');
            $table->string('scaffale');
            $table->integer('quantita_rimanente');
            $table->timestamps();
        });

*it's my first day sooo this is a dot

0 likes
4 replies
Snapey's avatar
Snapey
Best Answer
Level 122

seems to me like it's more likely that product belongs to sector?

Anyway to get it working

-rename the column to product_id and add an id column to the sectors table. remove the primary key statement in the model

eager load sector

public function index()
    {
       $products = Product::with('sector')->get();
       return view('worker.index', compact('products'));
    }

in the view use $product->sector->field ?? 'no sector'

your relation is called sector not sectors

the null coalesce operator ?? handles the case where a product is missing its sector

1 like
laithalenooz's avatar

In the sector model add the foreign key to the belongsTo relation.

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

in the ProductController make it Product::with('sector')->all(); or call it this way

public function index()
    {
       return view('worker(dot*)index')->with('products', Product::with('sector')->all());
    }

Please or to participate in this conversation.