php 如何在雄辩的 Laravel 中使用名称查找条目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12087098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:47:04  来源:igfitidea点击:

How to find entry using name in eloquent Laravel?

phpmysqlormlaravel

提问by monk

By default we generally search any entry on db table by id number. But I could not find how to search any entry by the name column.

默认情况下,我们通常通过 id 号搜索 db 表上的任何条目。但我找不到如何按名称列搜索任何条目。

This is my code for finding entry and rendering it to view

这是我用于查找条目并将其呈现以查看的代码

Controller : Authors

控制器:作者

class Authors_Controller extends Base_Controller {

    public $restful = true;

    public function get_view($id){
        $authorModel = Authors::find($id);
        return View::make('authors.view')
            ->with('author', $authorModel)
            ->with('title', $authorModel->name);
    }

}

Model : Authors

型号 : 作者

<?php 

class Authors extends Eloquent {
    public static $table = 'authors';
}

Route :

路线 :

Route::controller(Controller::detect());

Route::get('author/(:any)', array('as'=>'author', 'uses'=>'authors@view'));

View :

看法 :

@layout('main.index')

@section('content')
<h1>{{$author->name}}</h1>

<p>
    {{$author->bio}}
</p>

<small>
    {{$author->created_at}} |
    {{HTML::link(URL::$base.'/authors/', 'Go back')}}
</small>
@endsection

How do i make the url as not to display id but to display the name of the post like

我如何使 url 不显示 id 而是显示帖子的名称

some.com/category/name (instead of some.com/category/id)

some.com/category/name(而不是 some.com/category/id)

回答by David Barker

In your controller you are always going to search by $idas your Eloquent query uses:

在您的控制器中,您将始终$id按照 Eloquent 查询使用的方式进行搜索:

$authorModel = Authors::find($id);

As your named route can be supplied with an int or string (:any) run a type check in the controller on $idand run a different query based on the result.

由于您的命名路由可以提供 int 或 string (:any),因此在控制器中运行类型检查$id并根据结果运行不同的查询。

public function get_view($id)
{
   if (is_numeric($id))
   {
       $authorModel = Authors::find($id);
   }
   else
   {
       $column = 'name'; // This is the name of the column you wish to search

       $authorModel = Authors::where($column , '=', $id)->first();
   }

   return View::make('authors.view')
                ->with('author', $authorModel)
                ->with('title', $authorModel->name);

}

I hope that helps you.

我希望这对你有帮助。

As a side note, your Eloquent model.

作为旁注,您的 Eloquent 模型。

There is no need for you to supply a table name if you use correct naming conventions.

如果您使用正确的命名约定,则无需提供表名。

class Author extends Eloquent {

}

Note the singular Authorwill map to a table called Authorsautomatically without any intervention from you.

请注意,单数Author将映射到Authors自动调用的表,而无需您的任何干预。