【发布时间】:2014-07-17 12:11:48
【问题描述】:
我创建了一个带有模型和控制器的新迁移(称为“tipo”)。而且我还创建了它的视图......我所做的与我之前所做的完全一样。事实上,我已经复制并粘贴了大部分代码。其他一切都完美无缺。
但是当我想访问我的“tipo/index”时,我收到了下一条消息:
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Call to undefined method Tipo::orderBy()
我已经阅读了一些可能的解决方案,并且我已经更新了我的控制器。我也有同样的错误...... Argggg!!!
我的控制器:TipoController.php
class TipoController extends AdminController {
/**
* Display a listing of the resource.
* GET /tipo
*
* @return Response
*/
public function index()
{
$tipos = Tipo::orderBy('created_at', 'DESC')->paginate(10);
return View::make('tipos.index')->with('tipos', $tipos);
}
/**
* Show the form for creating a new resource.
* GET /tipo/create
*
* @return Response
*/
public function create()
{
return View::make('tipos.create');
}
/**
* Store a newly created resource in storage.
* POST /tipo
*
* @return Response
*/
public function store()
{
$input = Input::all();
// var_dump($input);
$v = Validator::make($input, Tipo::$rules);
if ($v->passes()) {
$tipo = new tipo;
$tipo->nombre = Input::get('nombre');
$tipo->descripcion = Input::get('descripcion');
$tipo->m_desc = Input::get('m_desc');
$tipo->slug = Str::slug(Input::get('nombre'));
$tipo->user_id = Sentry::getUser()->id;
$tipo->save();
// return $tipo->user_id;
return Redirect::route('tipos.index');
}
return Redirect::back()->withErrors($v);
}
/**
* Display the specified resource.
* GET /tipo/{id}
*
* @param int $id
* @return Response
*/
public function show($id)
{
$tipo = Tipo::where('slug', $id)->first();
$date = $tipo->created_at;
setlocale(LC_TIME, 'Europe/Madrid');
$date = $date->formatlocalized('%A %d %B %Y');
return View::make('tipos.show')->with('tipo', $tipo)->with('date', $date);
}
public function myShow($id, $slug)
{
$tipo = Tipo::where('id', $id)->first();
$date = $tipo->created_at;
setlocale(LC_TIME, 'Europe/Madrid');
$date = $date->formatlocalized('%A %d %B %Y');
return View::make('tipos.show')->with('tipo', $tipo)->with('date', $date);
}
/**
* Show the form for editing the specified resource.
* GET /tipo/{id}/edit
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$tipo = Tipo::where('id', $id)->first();
$date = $tipo->created_at;
setlocale(LC_TIME, 'Europe/Madrid');
$date = $date->formatlocalized('%A %d %B %Y');
return View::make('tipos.edit')->with('tipo', $tipo)->with('date', $date);
}
/**
* Update the specified resource in storage.
* PUT /tipo/{id}
*
* @param int $id
* @return Response
*/
public function update($id)
{
$input = array_except(Input::all(), '_method');
$v = Validator::make($input, Tipo::$rules);
if($v->passes())
{
Tipo::find($id)->update($input);
return Redirect::route('tipos.index');
}
return Redirect::back()->withErrors($v);
}
/**
* Remove the specified resource from storage.
* DELETE /tipo/{id}
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
Tipo::find($id)->delete();
return Redirect::route('tipos.index');
}
}
还有我的模型:Tipo.php
class Tipo extends Eloquent {
protected $guarded = array();
public static $rules = array(
'nombre' => 'required',
);
public function user()
{
return $this->belongsTo('User', 'user_id');
}
}
【问题讨论】:
-
请问您可以发布您的型号代码吗?
-
'required', ); public function user() { return $this->belongsTo('User', 'user_id'); } }
-
嗯...您的模型看起来不错,我可以看看您的控制器吗?请您通过pastebin.com 发帖以方便阅读?
-
对不起...我不知道它是如何工作的,但这是您可以看到代码的网址:pastebin.com/0y1MyeMc
-
@JaviZu 看看这个:php.net/manual/en/class.reflectionclass.php 它可以让你这样做:
$ref = new ReflectionClass('Tipo'); $ref->getFileName(); // path to the class file和更多有用的东西。