2017-03-31 1 views
0

에서 나는 Laravel에서 초보자입니다, 나는 붙어입니다 :) 나는 오히려 간단한 웹 사이트와 의상에 대한 간단한 CMS를 구축하고Laravel 출력 단일 행 데이터베이스

. 사이트 전체에 걸쳐 편집 할 수 있도록하려는 모든 텍스트에 대한 데이터베이스를 만들었습니다. 모든 CRUD가 작동하지만 블레이드가있는 데이터베이스의 내용을 실제보기로 출력 할 수 없습니다. 변수가 없다는 오류가 발생합니다.

{{$ text -> "특정 ID를 삽입하십시오"}} 내 테이블에는 사이트에 해당하는 텍스트 콘텐츠를 식별 할 수있는 ID 문자열이 있습니다.

텍스트 요소의 CRUD를 처리하는 MainController와 TextController가 있습니다.

나는 아마도 간단한 것을 잊어 버릴 것이지만, 나는 그것을 해결할 수없는 것 같습니다.

<?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'); 
} 

뭔가를 그 할 수없는 오전 생각?

미리 감사드립니다. 세션 콘텐츠 with() 깜박를 사용

+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

같은 방식으로 : 'public function edit ($ id) { $ text = Text :: findOrFail ($ id); 보기 되돌리기 ('texts.edit', compact ('text')); }'다음보기에서 : '$ 텍스트 -> 아이디 $ 텍스트 -> content' 블록 –

+0

하지만 나던 나에게 특정 ID를 제공합니다. 이 테이블에는 en id : "forside_tekst"문자열과 "Example"콘텐츠가 있습니다. 내용을 출력해야합니다. forside_tekst가 원하는 ID입니다. –

1

는 오히려 당신이보기 $data 데이터베이스에서 데이터입니다

view('index', $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}}