0

i'm new to Laravel, but I've been working at it. I've searched the site but can't find specific help. I've created a variable in my Controller (and checked it a 1000 times against a tutorial) and passed it to the view, but the browser gives an error "Undefined variable".

$company holds the table row after posting. I suspect it may have something to do with it being an array.

Controller resource function show

public function show($comp_id)
{
    $company = Company::find($comp_id);
    return view('companies.show')->with('company', $company);   
}

And in the view:

@section('content')
    <h1>{{ $company->CompanyName }}</h1>

I'm sure it's a simple error, but any help please?

2
  • I saw many syntax errors in there. Commented Jan 21, 2017 at 14:16
  • Check if dd($comp_id) in the controller return the right value Commented Jan 21, 2017 at 14:31

3 Answers 3

0

As mentioned by Sanzeeb, your syntax has several errors. Let's try fixing those first.

In your controller please modify your method like so:

public function show($comp_id)
{
    $company = Company::find($comp_id);

    return view('companies.show')->with('company', $company);
}

Then please modify your fiew like so:

@section('content')

<h1>{{ $company->CompanyName }}</h1>

And let's see if that helps.

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

2 Comments

Eitan, your quote of my code shows a semi-colon between the variable and the class, but in my submission it did have an equal sign. Your bracketing in the 'return view' is different from what Tom Kopah suggested. I tested both, but still get: ErrorException in 67ad97908c79b9baf76653c370849a03da33d1aa.php line 5: Undefined variable: company (View: C:\Sites\AI\resources\views\companies\show.blade.php)
Sorry gents, this is my first question on SO so I'm still learning the syntaxes and protocol. It seems @Pauwelyn made some errors in transcribing my code from the images I posted in my question. There is a bracket at the end of the return view line and I do have a @stop at the end of my View code.
0

You need to fix some things. try this please.

In your controller please modify your method like so:

public function show($comp_id)
{
    $company = Company::find($comp_id);

    return view('companies.show', compact('company'));
}

And in the view:

@section('content')
<h1>{{ $company->CompanyName }}</h1>
@stop

4 Comments

I believe this is syntax for Laravel 5, while PDevH's question implies he uses Laracel 4.
@ТомицаКораћ well, in that case lets wait for him to tell us. and if so, Then we will change :)
I'm using Laravel 5.3. I've tested all variations of your suggestions, specifically the bracketing and use of compact vs with. No luck.
Did you added 'use App\Company;` to the namespace?
0

Turns out I had to set the show($id) to the specific id name in my table column.
public function show($id) $company = Company::where('Comp_id', $id)->first(); return view('companies.show')->withCompany($company);
Thanks for your help anyways.

2 Comments

All is well that ends well. Could you please accept your answer so that others are aware that this question was resolved?
Ok, didn't know I could accept my own answer, but it makes sense.

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.