在过去的5个小时里,我一直在努力解决这个问题,这让我疯狂。
首先是我的代码。
有一个产品控制器
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
use Illuminate\Http\Request;
use App\Product;
class ProductController extends Controller {
protected $product;
public function __construct(Product $product)
{
$this->product = $product;
$this->middleware('auth', ['except' => ['index','show']]);
}
一些方法然后编辑方法
public function edit($id)
{
//$products = $this->product->findBySlug($slug);
$products = $this->product->with('images' )->find($id);
dd($products);
}
产品型号:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
class Product extends Model implements SluggableInterface {
use SluggableTrait;
protected $guarded = array();
protected $sluggable = array(
'build_from' => 'title',
'save_to' => 'slug',
);
public static $rules = array(
'type' => 'required',
'price' => 'required',
'title' => 'required'
);
protected $fillable = [
'shorturl',
'url',
'type',
'title',
'slug',
'overview',
'price',
'availability'
];
/**
* through product_tags pivot table
* @return [type] [description]
*/
public function tags()
{
return $this->hasMany('App\Tag');
}
public function images()
{
return $this->hasMany('App\Image');
}
}
和图像模型:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model {
protected $guarded = array();
public static $rules = array(
'title' => 'required',
'url' => 'required',
'product_id' => 'required'
);
protected $fillable = [
'title',
'url',
'size'
];
public function product()
{
return $this->belongsTo('App\Product');
}
}
我的问题是,当我跑
时$products = $this->product->find($id);
我得到了我正在寻找的记录。但是,如果我使用
$products = $this->product->with('images' )->find($id);
然后我收到以下错误。
BadMethodCallException in Builder.php line 1990:
Call to undefined method Illuminate\Database\Query\Builder::images()
前几行是:
in Builder.php line 1990
at Builder->__call('images', array())
at Builder->images()
at call_user_func_array(array(object(Builder), 'images'), array()) in Builder.php line 933
at Builder->__call('images', array())
at Builder->images()
at call_user_func_array(array(object(Builder), 'images'), array()) in Model.php line 3323
at Model->__call('images', array()) in Builder.php line 451
at Product->images() in Builder.php line 451
at Builder->Illuminate\Database\Eloquent\{closure}()
at call_user_func(object(Closure)) in Relation.php line 158
如果您愿意,我可以发布其余内容,但我认为我的错误是我缺失的基本原因。
这是完整的堆栈跟踪
in Builder.php line 1990
at Builder->__call('images', array())
at Builder->images()
at call_user_func_array(array(object(Builder), 'images'), array()) in Builder.php line 933
at Builder->__call('images', array())
at Builder->images()
at call_user_func_array(array(object(Builder), 'images'), array()) in Model.php line 3323
at Model->__call('images', array()) in Builder.php line 451
at Product->images() in Builder.php line 451
at Builder->Illuminate\Database\Eloquent\{closure}()
at call_user_func(object(Closure)) in Relation.php line 158
at Relation::noConstraints(object(Closure)) in Builder.php line 452
at Builder->getRelation('images') in Builder.php line 422
at Builder->loadRelation(array(object(Product)), 'images', object(Closure)) in Builder.php line 402
at Builder->eagerLoadRelations(array(object(Product))) in Builder.php line 169
at Builder->get(array('*')) in Builder.php line 136
at Builder->first(array('*')) in Builder.php line 84
at Builder->find('4') in ProductController.php line 117
at ProductController->edit('4')
at call_user_func_array(array(object(ProductController), 'edit'), array('products' => '4')) in Controller.php line 246
at Controller->callAction('edit', array('products' => '4')) in ControllerDispatcher.php line 162
at ControllerDispatcher->call(object(ProductController), object(Route), 'edit') in ControllerDispatcher.php line 107
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Authenticate.php line 47
at Authenticate->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 108
at ControllerDispatcher->callWithinStack(object(ProductController), object(Route), object(Request), 'edit') in ControllerDispatcher.php line 67
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ProductController', 'edit') in Route.php line 198
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 131
at Route->run(object(Request)) in Router.php line 691
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Router.php line 693
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 660
at Router->dispatchToRoute(object(Request)) in Router.php line 618
at Router->dispatch(object(Request)) in Kernel.php line 178
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 111
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53
答案 0 :(得分:1)
您发布的图片模型有一个类名称产品。
如果是您的实际代码,那么:
public function images()
{
return $this->hasMany('App\Image');
}
此关系中使用的App \ Image类不存在。
答案 1 :(得分:-1)
如果你仔细检查堆栈跟踪Model->__call('images', array())
它是因为它没有在with()
内调用Product
方法(因为它是静态的)。
但是你应该能够做到以下几点:
$products = $this->product->newQuery()->with('images')->find(1);