2016-12-14 443 views
0

使用laravel中的find()方法,您可以通過主鍵(默認爲id)進行搜索和查找。 我需要的方法,讓博客模型的所有實例與同一個標籤像laravel中的find()方法

public function blog($tag) 
{ 

    $table = Blog::where('tag', $tag)->get(); 
    return view('Blog',[ 
     'blogs'=>$table 
    ]); 

} 

,這是我的模型細節

Schema::create('blogs', function(Blueprint $table) 
    { 
     $table->increments('id'); 
     $table->timestamps(); 
     $table->string('title'); 
     $table->text('body'); 
     $table->text('pics'); 
     $table->string('tag'); 

    }); 

我用DD(「$表」)看到的結果 結果是空的

Collection {#175 ▼ 
    #items: [] 
      } 
+0

什麼是問題? – tkausl

+0

使用where方法時的結果是什麼? –

+0

@tkausl沒有返回dd() –

回答

0

我覺得有一些可能出現的問題:

  1. 在數據庫中沒有數據

  2. 標籤 - 如果你增加了一些博客 - 中保存

  3. 你有型號爲路由綁定,並tag1被綁定到模型將被截斷,所以使用它與where條款當您使用get方法時不會導致結果。

0

您的查詢一切正常。

確保您的$tag變量包含在Blog條目中設置的實際標籤。