我创建了一个资源控制器(CRUD操作),设置了我的Route::resource('clients', 'ClientController');
,除非我使用Show方法,否则它们似乎都正常工作。
访问localhost:8000 / clients / 1会为client
生成未定义的变量错误。我不确定视图是如何或为什么没有接收到我从控制器传递给它的$ client变量。
路线:
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)
{
//
}
}
查看(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
我可以在我的控制器(show)中执行dd($client)
并看到我需要的数据就在那里,所以它从数据库中取出它。
答案 0 :(得分:1)
试试这个:
public function show($id)
{
//
$client = Client::find($id);
return view('clients.show', compact('client'));
}
紧凑方法使用不带$符号的引号保存变量,您将在视图中获得 $ client
的变量答案 1 :(得分:0)
问题在于:
return view('clients.index', [ // the view you are referring is clients.index
'clients' => $clients,
]);
在这里,您尝试获取 clients / show.blade.php 中的数据。怎么可能。
如果您想在clients / show.blade.php中访问$ client,则必须在返回视图()方法中更改视图名称
答案 2 :(得分:0)
首先想,
我认为它会对你有所帮助
答案 3 :(得分:0)
Try this
`return view('clients.index')->with('clients', $clients);`
它会起作用。
答案 4 :(得分:0)
试试这个
return view('clients.index')->with('clients', $clients);
在clients / show.blade.php 中
像这样使用
@foreach($clients as $row)
$row->your col name;
@endforeach