0

I am a beginner at Laravel, and I am stuck :)

I am building a simple CMS for a costumer with a rather simple website. I have created a database for all the text throughout the site, which I want to be able to edit. All the CRUD is working, but I can't seem to output the content from the database, with blade, to the actual view, as I get the error that the variable doesnt exist.

I thought I would be doing something like this {{ $text->"insert specific id" }} My table has strings for id's to identify the respective text-content thoughout the site.

I have a MainController and a TextController, which is the one dealing with the CRUD of the text-elements.

I am probably forgetting a simple thing, but I just can't seem to work it out.

<?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)
{
    //
}

}

I would think that I am missing something in the 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');
}

Could that be?

Thanks in advance!

1

3 Answers 3

1

Using with() flashes content to a session, rather you need to pass in the variables to the view

view('index', $data)

Where $data is the data from the database.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help! Do I need to change anything in the TextController or would I be able to do this in the MainController alone?
You should really consider reading the docs, it's amazing how simple things like this are explained over there. laravel.com/docs/5.4/views.
You are right! I tried reading a lot of search results from the doc, but my problem was that I never found the right one :) But thank you very much!
0
public function index()
{
    $texts = Text::all();

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

Then in your view:

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

also compact() accept an array. You can do something like this compact(['first', 'second', 'third'])

6 Comments

Thank you! I see what I did wrong before :) but now I am not sure how to output a specific ID from that text database?
Same way: public function edit($id) { $text = Text::findOrFail($id); return view('texts.edit', compact('text')); } then in your view: $text->id $text->content
But that doesnt give me a specific id. The table has a string for en id: "forside_tekst" and then the content: "Example", and I need to output the content, how do I specify that the "forside_tekst" is the id that I want
Can you explain with more details what exactly want to do.
I have a table with a string as id and then the content. These rows represent the bits of text around the website I want to be able to edit I have made the CRUD so that this is possible. But now, I need to output the content of a specific id to the place in the view, where it is supposed to be shown. For instance, the first id is called front-headline and the content is: Welcome to my page. How do I get this to be the content of the <h1>{{ "the content from the database" }}</h1> on the index page?
|
0

In controller you have to select data you want to pass to view

$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

//and then pass it to view as second parameter (array) in view function)

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

//then in view

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

or
{{$text->content}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.