最初に私の悪い英語を申し訳ありません。 私はモデル関係に問題があります。コントローラーからの私のデータにアクセスできません。LaravelRalationship cant access
iは
カテゴリの表に、この
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();
});
と私は2つを作成するように起こっている2つのテーブルカテゴリとcategorydescriptionを持っていますモデルカテゴリモデルとカテゴリ説明モデル**は私のモデルです**
<?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');
}
}
及び第2のモデル
<?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);
}
}
とあなたが代わりにこののプロパティ$desc->categorydescription()
ような関係のメソッドにアクセスする必要があるデータ
<?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,
// ]);
}
}
ここで達成しようとしていることを明記してください。モデルでの関係を記述した方法では、カテゴリには複数の説明があります。それはあなたが作りたいものですか? – HasilT
問題は何ですか?何を試しましたか?何が起こったのですか? –