vendredi 14 février 2020

Laravel - Eloquent Relationship not working One to Many relationship

I have two models with One-to-Many relationship. I want to display data with relationship in blade.

Products Table

Table name = Products
PrimaryKey = pro_id
ForeignKey = cat_id

Categories Table

Table name = categories
PrimaryKey = cat_id

Products Model Code

namespace App;

use Illuminate\Database\Eloquent\Model;

class productsModel extends Model
{
    //code...
    protected $table      = 'products';
    protected $primaryKey = 'pro_id';

    // Every Products Belongs To One Category

    public function category()
    {
        # code...
        return $this->belongsTo('APP\abcModel','cat_id');
    }
}

Categories Model Code

namespace App;

use Illuminate\Database\Eloquent\Model;

class categoryModel extends Model
{
    //code...
    protected $table      = 'categories';
    protected $primaryKey = 'cat_id';

    // One Category Has Many Products

    public function products()
    {
        # code...
        return $this->hasMany('App\productsModel','cat_id','pro_id');
    }
}

Controller Code

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\productsModel;
class productsController extends Controller
{
    //code...
    public function products($category_id='')
    {
        # code...
        $data["products"] = productsModel::where
                            ('cat_id',$category_id)
                            ->get();        
        $data["categories"] = productsModel::where
                            ('cat_id',$category_id)->first()->category;
        echo "<pre>";
        print_r($data);
        echo "</pre>";
    }
}

ERROR: Symfony\Component\Debug\Exception\FatalThrowableError Class 'APP\categoryModel' not found



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2uCoqct
via IFTTT

Aucun commentaire:

Enregistrer un commentaire