2017-03-31 60 views
0

我在Laravel一個初學者,我堅持:)Laravel輸出單列從數據庫

我建立一個簡單的CMS的負荷消費有一個相當簡單的網站。 我已經爲整個網站的所有文本創建了一個數據庫,我希望能夠編輯它。所有的CRUD都在工作,但我似乎無法用刀片將數據庫中的內容輸出到實際視圖,因爲我得到了變量不存在的錯誤。

我以爲我會做這樣的事情{{$ text - >「insert specific id」}} 我的表格中有id的字符串來標識整個網站中的相應文本內容。

我有一個MainController和一個TextController,它是處理文本元素的CRUD。

我可能忘記了一件簡單的事情,但我似乎無法解決這個問題。

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\Text; 
use Session; 

class TextController extends Controller 
{ 
/** 
* Display a listing of the resource. 
* 
* @return \Illuminate\Http\Response 
*/ 
public function index() 
{ 
    $texts = Text::all(); 

    return view('texts.index')->withTexts($texts); 
} 

/** 
* Show the form for creating a new resource. 
* 
* @return \Illuminate\Http\Response 

/** 
* Store a newly created resource in storage. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\Response 
*/ 
public function store(Request $request) 
{ 
    $this->validate($request, array(

     'content' => 'required' 
     )); 

    $text = new Text; 
    $text->content = $request->content; 
    $text->save(); 


    return redirect()->route('texts.index'); 
} 


/** 
* Show the form for editing the specified resource. 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function edit($id) 
{ 
    $text = Text::find($id); 
    return view('texts.edit')->withText($text); 
} 

/** 
* Update the specified resource in storage. 
* 
* @param \Illuminate\Http\Request $request 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function update(Request $request, $id) 
{ 
    $text = Text::find($id); 
    $this->validate($request, array(

     'content' => 'required' 
     )); 

    $text->content = $request->content; 
    $text->save(); 

    Session::flash('success', 'Teksten blev ændret!'); 

    return redirect()->route('texts.index'); 
} 

/** 
* Remove the specified resource from storage. 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function destroy($id) 
{ 
    // 
} 

}

我會認爲我失去了一些東西在MainController

<?php 

namespace App\Http\Controllers; 
use Illuminate\Http\Request; 
use Validator; 
use Mail; 
use Illuminate\Support\Facades\DB; 
use Session; 
use App\Text; 


class MainController extends Controller 
{ 
public function index(){ 

    return view('index'); 
} 

可能?

提前致謝!

+0

這些視頻應該有所幫助:https://laracasts.com/series/laravel-from-scratch-2017/episodes/5和https://laracasts.com/series/laravel-from -scratch-2017/episodes/8 –

回答

0
public function index() 
{ 
    $texts = Text::all(); 

    return view('texts.index', compact('texts')); 
} 

然後在你看來:

@foreach ($texts as $text) 
    {!! $text->content !!} 
@endforeach 

也compact()接受一個數組。你可以這樣做compact(['first', 'second', 'third'])

+0

謝謝!我看到我之前做錯了什麼:)但現在我不知道如何從該文本數據庫輸出特定的ID? –

+0

同一方法: '公共功能編輯($ id) { $ text = Text :: findOrFail($ id); return view('texts.edit',compact('text')); }' 然後在您的視圖: '$文本 - > ID $文本 - > content' –

+0

但是,這並不能給我一個特定的ID。該表有一個字符串爲EN ID:「forside_tekst」,然後是內容:「示例」,我需要輸出內容,我該如何指定「forside_tekst」是我想要的ID –

1

使用with()閃爍內容到會話,而你需要的變量傳遞給視圖

view('index', $data) 

哪裏$data是從數據庫中的數據。

+0

感謝您的幫助!我是否需要在TextController中更改任何內容,或者是否可以單獨在MainController中執行此操作? –

+0

你真的應該考慮閱讀文檔,這是如此簡單的東西在那裏解釋是驚人的。 https://laravel.com/docs/5.4/views。 – Ian

+0

你是對的!我試着從文檔中讀取很多搜索結果,但是我的問題是我從來沒有找到合適的文檔:)但非常感謝! –

0

在控制器,你必須選擇數據要傳遞給查看

$texts = Text::get(); //or Text::all(); for all results 
$text = Text::find($id); //if you want one item get by ID 
$text = Text::where('column', 'value')->first(); //first item matching condition 

//然後通過它,以查看在視圖函數的第二個參數(陣列))

return view('index', ['texts' => $texts, 'text' => $text] /* or compact('texts', 'text') */); 

//然後在視圖

@foreach($texts as $t) 
{{$t->content}} 
@endforeach 

or 
{{$text->content}}