laravel :: find 在课堂上找不到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33121594/
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-09-14 12:30:10  来源:igfitidea点击:

laravel ::find not found in class

phplaravel

提问by Sander Van Keer

I have a few articles in the database and I am trying to find a specific one by searching for its title. This is my controller.

我在数据库中有几篇文章,我试图通过搜索其标题来查找特定的文章。这是我的控制器。

public function showArticle($title)
{
   $article = Article::find($title);

   return view('article.show', compact('article'));

}

This is the error I get in phpStorm: Method "findOrFail" not found in class App/Article.

这是我在 phpStorm 中遇到的错误:在 App/Article 类中找不到方法“findOrFail”。

This is my model:

这是我的模型:

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;



    class Article extends Model
    {
       protected $fillable =
       [
        'title',
        'description',
        'published_at'
       ];
    }

This is my view where I am trying to show the title and description of the article.

这是我的观点,我试图显示文章的标题和描述。

    @extends('layouts.master')

    @section('title', 'All articles')

    @section('content')



    <h1>{{$article->title}}</h1>


    <article>

        {{$article->description}}

    </article>

   @stop

When I try to load the view I get the following error:

当我尝试加载视图时,出现以下错误:

Trying to get property of non-object (View: /var/www/resources/views/article/show.blade.php)

试图获取非对象的属性(视图:/var/www/resources/views/article/show.blade.php)

回答by Glad To Help

find()method does not work with any attribute you want. You have to pass the id of the article to achieve that. Also finding articles by title instead of id is much much slower. But if you really want to do it, you can write something like:

find()方法不适用于您想要的任何属性。您必须传递文章的 id 才能实现这一点。此外,通过标题而不是 id 查找文章要慢得多。但如果你真的想这样做,你可以这样写:

$article = Article::where('title', $title)->first();

Then in your view, do something like:

然后在您看来,执行以下操作:

    @extends('layouts.master')

@section('title', 'All articles')

@section('content')

@if($article)

<h1>{{$article->title}}</h1>


<article>

    {{$article->description}}

</article>

@else
    Article not found
@endif

@stop

@停止

Check the docs for further reference: http://laravel.com/docs/5.1/eloquent#retrieving-single-models

检查文档以获取进一步参考:http: //laravel.com/docs/5.1/eloquent#retrieving-single-models

回答by Grzegorz Gajda

1. Find article with specific title

1. 查找特定标题的文章

Article::where('title', '=', $title)

Method find()is looking for primary key defined in Modelclass (by default is idcolumn).

方法find()正在寻找在Model类中定义的主键(默认是id列)。

2. compact()function

2.compact()功能

View throws an error because you send an array to view, not the object.

View 会抛出错误,因为您发送的是数组而不是对象。

(PHP 4, PHP 5, PHP 7)

compact — Create array containing variables and their values

(PHP 4、PHP 5、PHP 7)

compact - 创建包含变量及其值的数组

Then in your Controllerchange compact('article')to $articleor change syntax in your blade file to array ($article['description']).

然后在你的Controller变化compact('article')$article或改变语法在刀刃文件阵列($article['description'])。

回答by wantedwahab

Try

尝试

$article = Article::all()->find($title);

Laravel 5.3

Laravel 5.3