gpt4 book ai didi

php - 查询 Eloquent hasMany 返回 columnize 错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:48 25 4
gpt4 key购买 nike

我正在尝试查询我在 Laravel 中设置的模型。我想要做的就是在我的 categories 表中获取所有 categories ,这些 categories 具有给定的 App\Category 实例作为它们的 parent。不幸的是,我的 hasMany 正在返回 Type error: Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given, called in/Users/infosec/kannopy/vendor/laravel/framework 中/src/Illuminate/Database/Query/Grammars/Grammar.php 第 122 行

这是我的 App\Category 模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
protected $fillable = ['id', 'name'];

/**
* return the items in a category
*/
public function items(){
$this->hasMany('App\Item', 'category_id', 'id');
}

/**
* return the categories parent category
*/
public function parent_category(){
$this->belongsTo('App\Category', 'parent', 'id');
}

/**
* get a list of the child categories of any given category
*/
public function children(){
$this->hasMany('App\Category', 'parent', 'id');
}


}

这是我进行查询的 Controller :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Category;
class CategoryController extends Controller
{

/**
* return the children of a given category
*/
public function getChildren($id){
$parent = Category::find($id);
return $parent->children();
}
}

此外,这是category 表的数据库迁移:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('parent')->unsigned()->nullable(); //this needs to be done before the foreign key constraint
$table->foreign('parent')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}

最后,这是完整的回溯(作为 html sinppet):

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow" />
<style>
/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;}

html { background: #eee; padding: 10px }
img { border: 0; }
#sf-resetcontent { width:970px; margin:0 auto; }
.sf-reset { font: 11px Verdana, Arial, sans-serif; color: #333 }
.sf-reset .clear { clear:both; height:0; font-size:0; line-height:0; }
.sf-reset .clear_fix:after { display:block; height:0; clear:both; visibility:hidden; }
.sf-reset .clear_fix { display:inline-block; }
.sf-reset * html .clear_fix { height:1%; }
.sf-reset .clear_fix { display:block; }
.sf-reset, .sf-reset .block { margin: auto }
.sf-reset abbr { border-bottom: 1px dotted #000; cursor: help; }
.sf-reset p { font-size:14px; line-height:20px; color:#868686; padding-bottom:20px }
.sf-reset strong { font-weight:bold; }
.sf-reset a { color:#6c6159; cursor: default; }
.sf-reset a img { border:none; }
.sf-reset a:hover { text-decoration:underline; }
.sf-reset em { font-style:italic; }
.sf-reset h1, .sf-reset h2 { font: 20px Georgia, "Times New Roman", Times, serif }
.sf-reset .exception_counter { background-color: #fff; color: #333; padding: 6px; float: left; margin-right: 10px; float: left; display: block; }
.sf-reset .exception_title { margin-left: 3em; margin-bottom: 0.7em; display: block; }
.sf-reset .exception_message { margin-left: 3em; display: block; }
.sf-reset .traces li { font-size:12px; padding: 2px 4px; list-style-type:decimal; margin-left:20px; }
.sf-reset .block { background-color:#FFFFFF; padding:10px 28px; margin-bottom:20px;
-webkit-border-bottom-right-radius: 16px;
-webkit-border-bottom-left-radius: 16px;
-moz-border-radius-bottomright: 16px;
-moz-border-radius-bottomleft: 16px;
border-bottom-right-radius: 16px;
border-bottom-left-radius: 16px;
border-bottom:1px solid #ccc;
border-right:1px solid #ccc;
border-left:1px solid #ccc;
word-wrap: break-word;
}
.sf-reset .block_exception { background-color:#ddd; color: #333; padding:20px;
-webkit-border-top-left-radius: 16px;
-webkit-border-top-right-radius: 16px;
-moz-border-radius-topleft: 16px;
-moz-border-radius-topright: 16px;
border-top-left-radius: 16px;
border-top-right-radius: 16px;
border-top:1px solid #ccc;
border-right:1px solid #ccc;
border-left:1px solid #ccc;
overflow: hidden;
word-wrap: break-word;
}
.sf-reset a { background:none; color:#868686; text-decoration:none; }
.sf-reset a:hover { background:none; color:#313131; text-decoration:underline; }
.sf-reset ol { padding: 10px 0; }
.sf-reset h1 { background-color:#FFFFFF; padding: 15px 28px; margin-bottom: 20px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="sf-resetcontent" class="sf-reset">
<h1>Whoops, looks like something went wrong.</h1>
<h2 class="block_exception clear_fix">
<span class="exception_counter">1/1</span>
<span class="exception_title"><abbr title="Symfony\Component\Debug\Exception\FatalThrowableError">FatalThrowableError</abbr> in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Grammar.php line 107" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Grammar.php line 107</a>:</span>
<span class="exception_message">Type error: Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given, called in /Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 122</span>
</h2>
<div class="block">
<ol class="traces list_exception">
<li> in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Grammar.php line 107" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Grammar.php line 107</a></li>
<li>at <abbr title="Illuminate\Database\Grammar">Grammar</abbr>->columnize('1') in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php line 122" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Grammar.php line 122</a></li>
<li>at <abbr title="Illuminate\Database\Query\Grammars\Grammar">Grammar</abbr>->compileColumns(<em>object</em>(<abbr title="Illuminate\Database\Query\Builder">Builder</abbr>), '1') in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php line 76" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Grammar.php line 76</a></li>
<li>at <abbr title="Illuminate\Database\Query\Grammars\Grammar">Grammar</abbr>->compileComponents(<em>object</em>(<abbr title="Illuminate\Database\Query\Builder">Builder</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php line 52" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Grammar.php line 52</a></li>
<li>at <abbr title="Illuminate\Database\Query\Grammars\Grammar">Grammar</abbr>->compileSelect(<em>object</em>(<abbr title="Illuminate\Database\Query\Builder">Builder</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php line 38" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">MySqlGrammar.php line 38</a></li>
<li>at <abbr title="Illuminate\Database\Query\Grammars\MySqlGrammar">MySqlGrammar</abbr>->compileSelect(<em>object</em>(<abbr title="Illuminate\Database\Query\Builder">Builder</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php line 1578" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Builder.php line 1578</a></li>
<li>at <abbr title="Illuminate\Database\Query\Builder">Builder</abbr>->toSql() in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php line 1645" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Builder.php line 1645</a></li>
<li>at <abbr title="Illuminate\Database\Query\Builder">Builder</abbr>->runSelect() in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php line 1631" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Builder.php line 1631</a></li>
<li>at <abbr title="Illuminate\Database\Query\Builder">Builder</abbr>->get('1') in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php line 613" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Builder.php line 613</a></li>
<li>at <abbr title="Illuminate\Database\Eloquent\Builder">Builder</abbr>->getModels('1') in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php line 318" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Builder.php line 318</a></li>
<li>at <abbr title="Illuminate\Database\Eloquent\Builder">Builder</abbr>->get('1') in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php line 288" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Builder.php line 288</a></li>
<li>at <abbr title="Illuminate\Database\Eloquent\Builder">Builder</abbr>->first('1') in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php line 168" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Builder.php line 168</a></li>
<li>at <abbr title="Illuminate\Database\Eloquent\Builder">Builder</abbr>->find('id', '1')</li>
<li>at <abbr title=""></abbr>call_user_func_array(<em>array</em>(<em>object</em>(<abbr title="Illuminate\Database\Eloquent\Builder">Builder</abbr>), 'find'), <em>array</em>('id', '1')) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php line 3516" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Model.php line 3516</a></li>
<li>at <abbr title="Illuminate\Database\Eloquent\Model">Model</abbr>->__call('find', <em>array</em>('id', '1'))</li>
<li>at <abbr title=""></abbr>call_user_func_array(<em>array</em>(<em>object</em>(<abbr title="App\Category">Category</abbr>), 'find'), <em>array</em>('id', '1')) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php line 3530" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Model.php line 3530</a></li>
<li>at <abbr title="Illuminate\Database\Eloquent\Model">Model</abbr>::__callStatic('find', <em>array</em>('id', '1')) in <a title="/Users/infosec/myProject/app/Http/Controllers/CategoryController.php line 17" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">CategoryController.php line 17</a></li>
<li>at <abbr title="App\Http\Controllers\CategoryController">CategoryController</abbr>->getChildren('1')</li>
<li>at <abbr title=""></abbr>call_user_func_array(<em>array</em>(<em>object</em>(<abbr title="App\Http\Controllers\CategoryController">CategoryController</abbr>), 'getChildren'), <em>array</em>('id' => '1')) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Controller.php line 55" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Controller.php line 55</a></li>
<li>at <abbr title="Illuminate\Routing\Controller">Controller</abbr>->callAction('getChildren', <em>array</em>('id' => '1')) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php line 44" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">ControllerDispatcher.php line 44</a></li>
<li>at <abbr title="Illuminate\Routing\ControllerDispatcher">ControllerDispatcher</abbr>->dispatch(<em>object</em>(<abbr title="Illuminate\Routing\Route">Route</abbr>), <em>object</em>(<abbr title="App\Http\Controllers\CategoryController">CategoryController</abbr>), 'getChildren') in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Route.php line 189" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Route.php line 189</a></li>
<li>at <abbr title="Illuminate\Routing\Route">Route</abbr>->runController() in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Route.php line 144" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Route.php line 144</a></li>
<li>at <abbr title="Illuminate\Routing\Route">Route</abbr>->run(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Router.php line 642" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Router.php line 642</a></li>
<li>at <abbr title="Illuminate\Routing\Router">Router</abbr>->Illuminate\Routing\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php line 53" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 53</a></li>
<li>at <abbr title="Illuminate\Routing\Pipeline">Pipeline</abbr>->Illuminate\Routing\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php line 41" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">SubstituteBindings.php line 41</a></li>
<li>at <abbr title="Illuminate\Routing\Middleware\SubstituteBindings">SubstituteBindings</abbr>->handle(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>), <em>object</em>(<abbr title="Closure">Closure</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php line 137" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 137</a></li>
<li>at <abbr title="Illuminate\Pipeline\Pipeline">Pipeline</abbr>->Illuminate\Pipeline\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php line 33" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 33</a></li>
<li>at <abbr title="Illuminate\Routing\Pipeline">Pipeline</abbr>->Illuminate\Routing\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php line 48" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">ThrottleRequests.php line 48</a></li>
<li>at <abbr title="Illuminate\Routing\Middleware\ThrottleRequests">ThrottleRequests</abbr>->handle(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>), <em>object</em>(<abbr title="Closure">Closure</abbr>), '60', '1') in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php line 137" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 137</a></li>
<li>at <abbr title="Illuminate\Pipeline\Pipeline">Pipeline</abbr>->Illuminate\Pipeline\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php line 33" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 33</a></li>
<li>at <abbr title="Illuminate\Routing\Pipeline">Pipeline</abbr>->Illuminate\Routing\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php line 104" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 104</a></li>
<li>at <abbr title="Illuminate\Pipeline\Pipeline">Pipeline</abbr>->then(<em>object</em>(<abbr title="Closure">Closure</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Router.php line 644" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Router.php line 644</a></li>
<li>at <abbr title="Illuminate\Routing\Router">Router</abbr>->runRouteWithinStack(<em>object</em>(<abbr title="Illuminate\Routing\Route">Route</abbr>), <em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Router.php line 618" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Router.php line 618</a></li>
<li>at <abbr title="Illuminate\Routing\Router">Router</abbr>->dispatchToRoute(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Router.php line 596" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Router.php line 596</a></li>
<li>at <abbr title="Illuminate\Routing\Router">Router</abbr>->dispatch(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php line 267" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Kernel.php line 267</a></li>
<li>at <abbr title="Illuminate\Foundation\Http\Kernel">Kernel</abbr>->Illuminate\Foundation\Http\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php line 53" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 53</a></li>
<li>at <abbr title="Illuminate\Routing\Pipeline">Pipeline</abbr>->Illuminate\Routing\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/barryvdh/laravel-debugbar/src/Middleware/Debugbar.php line 51" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Debugbar.php line 51</a></li>
<li>at <abbr title="Barryvdh\Debugbar\Middleware\Debugbar">Debugbar</abbr>->handle(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>), <em>object</em>(<abbr title="Closure">Closure</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php line 137" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 137</a></li>
<li>at <abbr title="Illuminate\Pipeline\Pipeline">Pipeline</abbr>->Illuminate\Pipeline\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php line 33" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 33</a></li>
<li>at <abbr title="Illuminate\Routing\Pipeline">Pipeline</abbr>->Illuminate\Routing\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php line 46" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">CheckForMaintenanceMode.php line 46</a></li>
<li>at <abbr title="Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode">CheckForMaintenanceMode</abbr>->handle(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>), <em>object</em>(<abbr title="Closure">Closure</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php line 137" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 137</a></li>
<li>at <abbr title="Illuminate\Pipeline\Pipeline">Pipeline</abbr>->Illuminate\Pipeline\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php line 33" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 33</a></li>
<li>at <abbr title="Illuminate\Routing\Pipeline">Pipeline</abbr>->Illuminate\Routing\{closure}(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php line 104" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Pipeline.php line 104</a></li>
<li>at <abbr title="Illuminate\Pipeline\Pipeline">Pipeline</abbr>->then(<em>object</em>(<abbr title="Closure">Closure</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php line 149" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Kernel.php line 149</a></li>
<li>at <abbr title="Illuminate\Foundation\Http\Kernel">Kernel</abbr>->sendRequestThroughRouter(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php line 116" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Kernel.php line 116</a></li>
<li>at <abbr title="Illuminate\Foundation\Http\Kernel">Kernel</abbr>->handle(<em>object</em>(<abbr title="Illuminate\Http\Request">Request</abbr>)) in <a title="/Users/infosec/myProject/public/index.php line 53" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">index.php line 53</a></li>
<li>at <abbr title=""></abbr>require('/Users/infosec/myProject/public/index.php') in <a title="/Users/infosec/.composer/vendor/laravel/valet/server.php line 106" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">server.php line 106</a></li>
</ol>
</div>

</script>
</body>
</html>

最佳答案

你的 parents 函数不应该在 Category 类中,而应该在 Item 类中

分类类

public function parent_category(){
return $this->belongsTo('App\Category', 'parent', 'id');
}

public function children(){
return $this->hasMany('App\Category', 'parent', 'id');
}

你的 Controller 的功能应该是:

public function getChildren($id){
$parent = Category::where('id', $id)->with('children')->first();
return $parent->children;
}

public function getChildren($id){
return Category::where('parent', $id)->get();
}

第一个代码运行两个查询来查找父级然后找到它的子级,而第二个代码运行单个查询并直接通过父级的 ID 找到子级。

关于php - 查询 Eloquent hasMany 返回 columnize 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40558975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com