如何显示控制器';s数据


How to display controller's data

我无法在视图中显示数据,我认为我的控制器出了问题,但我不明白。

我的控制器

namespace App'Http'Controllers;
use Illuminate'Http'Request;
use App'Http'Requests;
use App'Projects;
use Auth;

class WelcomeController extends Controller
{
    public function __construct(Projects $projects)
    {
        $this->middleware('auth');
        $this->projects = $projects;
    }
    public function index()
    {
        $projects = Projects::get();
        $this->$projects;
        return view('welcome')->with('projects', '$projects');
    }
}

路线:

Route::get('test', [
    'uses' => 'WelcomeController@index',
    'as' => 'welcome',
    ]);

视图:

                <div class="panel-body">
                <p>Projects: </p>
                <p>Users:  </p>
                <h3>Project: {{ $project->title }} </h3>

我得到了什么:http://188.166.166.143/test

您的控制器:

namespace App'Http'Controllers;
use Illuminate'Http'Request;
use App'Http'Requests;
use App'Projects;
use Auth;

class WelcomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function index()
    {
        $projects = Projects::get();
        return view('welcome')->with('projects', $projects);
    }
}

正如科马尔所说,你的观点应该是这样的。

<table id="table-projects" class="table table-hover">
 <thead class="text-center">
   <tr>
     <th width="10%"><center>Project Name</center></th>
    </tr>
 </thead>
 <tbody>
    @foreach($projects as $project)
      <tr>
       <td>{{$project->title}}</td>
      <tr>
    @endforeach   
  </tbody>
</table>

$projects = Projects::get();将给出项目的集合。

@foreach($projects as $project)
   <h3>Project: {{ $project->title }} </h3>
@endforeach 

这将给出每个项目的标题。

public function index()
{
  $projects = Projects::all();
  return view('welcome')->with('projects', '$projects');
}

路线

Route::get('/test', 'WelcomeController@getIndex');

您的html

<table id="table-projects" class="table table-hover">
  <thead class="text-center">
    <tr>
      <th width="10%"><center>Project Name</center></th>
    </tr>
  </thead>
  <tbody>
    @foreach($projects as $project)
      <tr>
        <td>{{$project->title}}</td>
      <tr>
    @endforeach   
  </tbody>
</table>