LaravelRalationship无法访问

时间:2017-09-17 09:51:01

标签: php laravel relationships

首先抱歉我的英语不好.. 我有模型关系的问题,我无法从控制器和视图访问我的数据,你能帮忙吗?

我有两个表类别和类别描述

类别表就像这样

Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('image', 255)->nullable();
            $table->integer('parent_id')->defaut(0);
            $table->tinyInteger('top');
            $table->integer('column');
            $table->integer('sort_order')->defaut(0);
            $table->tinyInteger('status');
            $table->timestamps();
        });

** categories_description表,如你所见**

Schema::create('categories_description', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('categories_id')->unsigned();
            $table->foreign('categories_id')->references('id')->on('categories');
            // $table->foreign('language_id')->references('id')->on('languages');
            $table->string('name', 255);
            $table->text('description');
            $table->string('meta_title', 255);
            $table->string('meta_description', 255);
            $table->string('meta_keyword', 255);
            $table->timestamps();
        });

我为他们创建了两个模型分类模型和CategoryDe​​scription模型** Thas是我的模型**

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{


    public function selectQuery($sql_stmt) {
        return DB::select($sql_stmt);
    }

    public function sqlStatement($sql_stmt) {
        DB::statement($sql_stmt);
    }

    public function categorydescription() {
        return $this->hasMany('App\Model\CategoryDescription', 'categories_id', 'id');
    }


}

和第二个模型

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class CategoryDescription extends Model
{

    protected $table = 'categories_description';

    public function selectQuery($sql_stmt) {
        return DB::select($sql_stmt);
    }

    public function sqlStatement($sql_stmt) {
        DB::statement($sql_stmt);
    }

}

和我的家庭控制器代码,用于获取数据

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Model\Category;
use App\Model\CategoryDescription;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        // $this->middleware('auth');
        $this->categories = Category::with('categorydescription')->get(); // where('status', 1)->orderBy('created_at', 'desc')->

    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {

        $res = $this->categories;
        foreach($res as $desc) {
            echo $desc->id;
            echo "<br>";
            foreach ($desc->categorydescription() as $key) {
                echo "<hr>";
                echo $key->id;
                echo "<hr>";
            }
        }

        // die();
        // return view('home', [
        //     'title'         => 'Home',
        //     'categories'    => $this->categories,
        // ]);
    }
}

1 个答案:

答案 0 :(得分:0)

您必须将关系方法作为属性$desc->categorydescription()而不是此属性访问,您必须按如下方式使用它$desc->categorydescription

尝试使用以下

public function index() {

        $res = $this->categories;
        foreach($res as $desc) {
            echo $desc->id;
            echo "<br>";
            foreach ($desc->categorydescription as $key) {
                echo "<hr>";
                echo $key->id;
                echo "<hr>";
            }
        }


    }