Php 如何在laravel数据库的表中获取已验证用户存储的数据
我试图在控制器中使用此代码检索数据,以便在blade.php文件中创建一个foreach表Php 如何在laravel数据库的表中获取已验证用户存储的数据,php,laravel,Php,Laravel,我试图在控制器中使用此代码检索数据,以便在blade.php文件中创建一个foreach表 <?php namespace App\Http\Controllers; use illuminate\Http\Resquest; use App\Http\Requests; use App\Http\Controllers\Controller; use App\user; use DB; use Auth; use Illuminate\Http\Request; class H
<?php
namespace App\Http\Controllers;
use illuminate\Http\Resquest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\user;
use DB;
use Auth;
use Illuminate\Http\Request;
class HistoryController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function show($user)
{
$user_id = auth()->user()->id;
$history = DB::table('funds')->where('user_id', $user_id);
return view('history.index')->with('funds',$history);
}
}
使用get()
public function show($user)
{
$user_id = auth()->user()->id;
$history = DB::table('funds')->where('user_id', $user_id)->get();
return view('history.index')->with('funds',$history);
}
或
return view("history.index", compact("history "));
$history = DB::table('funds')->select('bankname')->where('user_id', $user_id)->get();
public function show($user)
{
$user_id = auth()->user()->id;
$history = DB::table('funds')->where('user_id', $user_id)->get();
return view('history.index', compact('history'));
}
或
return view("history.index", compact("history "));
$history = DB::table('funds')->select('bankname')->where('user_id', $user_id)->get();
public function show($user)
{
$user_id = auth()->user()->id;
$history = DB::table('funds')->where('user_id', $user_id)->get();
return view('history.index', compact('history'));
}
Blade.php
@foreach($history as $item)
{
<p>{{$item->bankname}}</p>
<p>{{$item->updated_at}}</p>
}
@foreach($history as$item)
{
{{$item->bankname}
{{$item->updated_at}
}
控制器
return view("history.index", compact("history "));
$history = DB::table('funds')->select('bankname')->where('user_id', $user_id)->get();
public function show($user)
{
$user_id = auth()->user()->id;
$history = DB::table('funds')->where('user_id', $user_id)->get();
return view('history.index', compact('history'));
}
在刀片服务器中,只需在foreach中使用变量$history
刀片
@foreach($history as $item)
{
<p>{{$item->bankname}}</p>
<p>{{$item->updated_at}}</p>
}
@foreach($history as$item)
{
{{$item->bankname}
{{$item->updated_at}
}
您必须执行查询,将->get()
添加到分配给$history
的语句中,它在我使用dd()时显示数据,但显示以下错误:尝试获取非对象的属性“bankname”(视图:C:\Users\user pc\Desktop\dkn\resources\views\history\index.blade.php){{$history???'->bankname}
因为'
不是一个对象,变量也没有命名为history
您已经将它作为资金传递到视图中了
谢谢它给了我一个json文件,但我只想选择一个您想要得到的列?\n您可以使用查询生成器的链接->选择('columnName')在get()之前。您还应该阅读laravel文档,这对您很有用。$history=DB::table('funds')->select('bankname')->where('user\u id',$user\u id)->get();你能得到你的答案吗?/谢谢,但我还想显示用户最后更新的行。你只需删除select chain方法即可获得所有列,但只显示所需的数据。$history=DB::table('funds')->where('user_id',$user_id)->get();