Derek Derek - 8 days ago 7
PHP Question

Undefined Variable in View but var_dump shows data in Laravel

I created a resource controller (CRUD actions), setup my

Route::resource('clients', 'ClientController');
and all seem to be working fine except when I use the Show method.

Visiting localhost:8000/clients/1 produced an undefined variable error for
client
. Im not sure how or why the view is not picking up on the $client variable I am passing to it from controller.

Route:
Route::resource('clients', 'ClientController');


ClientController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Client;

class ClientController extends Controller
{
/**
* Create a new controller instance
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$clients = Client::where('user_id', $request->user()->id)->get();

return view('clients.index', [
'clients' => $clients,
]);
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
return view('clients.create');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'organization' => 'required|max:255',
]);

//Create the task

$request->user()->clients()->create([
'organization' => $request->name,
]);

return redirect('/clients');
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
$client = Client::find($id);
return view('clients.show', compact($client));
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

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


View (clients/show.blade.php):

@extends('admin_template')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>

<div class="panel-body">
<strong>Organization: </strong> {{ $client->organization }}
</div>
</div>
</div>
</div>
</div>
@endsection


I can do
dd($client)
in my controller (show) and see that the data I need is there so it is fetching it from the database.

Answer

Try this :

 public function show($id)
    {
        //
        $client = Client::find($id);
        return view('clients.show', compact('client'));
    }

compact method hold the variable with quote without $ sign and you will get the variable in the view as $client