簡體   English   中英

使用 Laravel 和 Eloquent 從表中選擇所有內容

[英]Select all from table with Laravel and Eloquent

我正在使用 Laravel 4 來設置我的第一個模型,以從名為posts的表中提取所有行。

在標准 MySQL 中,我會使用:

SELECT * FROM posts;

我如何在我的 Laravel 4 模型中實現這一點?

請參閱下面的我的完整模型源代碼:

<?php

class Blog extends Eloquent 
{

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'posts';

    public function getAllPosts()
    {

    }

}

你只需打電話

Blog::all();

//example usage.
$posts = Blog::all();

$posts->each(function($post) // foreach($posts as $post) { }
{
    //do something
}

從您的應用程序的任何地方。

閱讀文檔會有很大幫助。

有 3 種方法可以做到這一點。

1 - 使用 all();

$entireTable = TableModelName::all();

例如,

$posts = Posts::get(); 

2 - 使用數據庫外觀

將此行放在控制器中的類之前

use Illuminate\Support\Facades\DB; // this will import the DB facade into your controller class

現在在課堂上

$posts = DB::table('posts')->get(); // it will get the entire table

3 - 使用帶有選擇的數據庫外觀

將此行放在控制器中的類之前

*Same import the DB facade like method 2*

現在在控制器中

$posts = DB::select('SELECT * FROM posts');

去你的控制器寫這個功能

public function index()
{
  $posts = \App\Post::all();

  return view('yourview', ['posts' => $posts]);
}

為了展示它

@foreach($posts as $post)
  {{ $post->yourColumnName }}
@endforeach

好吧,要用 eloquent 做到這一點,你會這樣做:

Blog:all();

在您的模型中,您可以:

return DB::table('posts')->get();

http://laravel.com/docs/queries

如何使用laravel從數據庫中獲取所有數據以查看,我希望這個解決方案對初學者有所幫助。

在你的控制器里面

public function get(){
        $types = select::all();
        return view('selectview')->with('types', $types);}

在您的控制器中導入數據模型,在我的應用程序中,數據模型命名為 select。

use App\Select;

包括我的控制器看起來像這樣

use App\Select;
class SelectController extends Controller{                             
    public function get(){
    $types = select::all();
    return view('selectview')->with('types', $types);}

選擇型號

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Select extends Model
{
    protected $fillable = [
        'name', 'email','phone','radio1','service',
    ];


    protected $table = 'selectdata';
    public $timestamps = false;
}

路由器內部

Route::get('/selectview', 'SelectController@get');

selectview.blade.php

@foreach($types as $type)
    <ul>
    <li>{{ $type->name }}</li>
    </ul>

    @endforeach
Query
// Select all data of model table
Model::all();
// Select all data of model table
Model::get();

Model::where('foo', '=', 'bar')->get();

Model::find(1);
Model::find([1, 2, 3]);
Model::findOrFail(1);
$posts = DB::select('SELECT * FROM table_x');

或獲取特定列:

$posts = DB::select('SELECT col_a, col_b, col_c FROM table_x');
 public function getAllPosts()
 {
   return  Blog::all();        
 }

看看文檔,這可能是他們解釋的第一件事。

這對我有用。

$posts = Blog::get()->all();

使用DB 門面,您可以執行 SQL 查詢

 public function index()
{
    return DB::table('table_name')->get();
}

如果您的表非常大,您還可以通過“小包”(並非全部在 oce)處理行(laravel 文檔:Eloquent > Chunking Results

Post::chunk(200, function($posts)
{
    foreach ($posts as $post)
    {
        // process post here.
    }
});

在 Laravel Eloquent 中,您可以在控制器中提供以下查詢以獲取所需表中的所有數據:

$posts = Post::all();
return view('post', compact('posts'));

或者

$posts = Post::orderBy('id')->get();
return view('post', compact('posts'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM