laravel user.php,php – 如何从laravel中的user_id获取用户名?

我在注册会员的资金表user_id中有2个表第1基金第2用户我在用户表中注册了会员名.我想在一个表中显示用户名,我可以在其中显示其user_id.

NameDateCurrencyAmount

@foreachFund::with('user')->where('created_at', '>', Carbon::now()->subHours(24))->orderBy('id', 'desc')->take(10)->get()

{{ $fund->user_id }}{{ $fund->updated_at}} {{$basic->currency}} {{ $fund->total }}

@endforeach

我已将以下代码粘贴到我的基金模型中.但它没有用.

public function user(){

return $this->belongsTo('App\User', 'user_id');

}

解决方法:

使用关系访问用户对象.

@foreach(Fund::with('user')->where('created_at', '>', Carbon::now()->subHours(24))->orderBy('id', 'desc')->take(10)->get() as $fund)

{{ $fund->user->name }}

@endforeach

标签:php,laravel,laravel-5

来源: https://codeday.me/bug/20190710/1426220.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Laravel 框架,可以使用 Laravel 自带的身份验证系统来实现用户名登录。具体步骤如下: 1. 配置数据库 在 `.env` 文件配置数据库连接信息,如下所示: ``` DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_database_username DB_PASSWORD=your_database_password ``` 2. 创建用户表 运行以下 Artisan 命令来创建用户表: ``` php artisan make:model User -m ``` 该命令将同时生成 `app/User.php` 模型文件和 `database/migrations/xxxx_xx_xx_xxxxxx_create_users_table.php` 迁移文件。在迁移文件定义用户表的字段和属性,如下所示: ```php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } public function down() { Schema::dropIfExists('users'); } } ``` 运行以下命令来执行迁移: ``` php artisan migrate ``` 3. 创建控制器 运行以下 Artisan 命令来创建控制器: ``` php artisan make:controller AuthController ``` 在 `AuthController` 定义登录和退出登录方法,如下所示: ```php use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class AuthController extends Controller { public function showLoginForm() { return view('auth.login'); } public function login(Request $request) { $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { // Authentication passed... return redirect()->intended('/dashboard'); } return back()->withErrors([ 'email' => 'The provided credentials do not match our records.', ]); } public function logout(Request $request) { Auth::logout(); $request->session()->invalidate(); $request->session()->regenerateToken(); return redirect('/'); } } ``` 4. 创建路由 在 `routes/web.php` 文件定义登录和退出登录路由,如下所示: ```php Route::get('login', [AuthController::class, 'showLoginForm'])->name('login'); Route::post('login', [AuthController::class, 'login']); Route::post('logout', [AuthController::class, 'logout'])->name('logout'); ``` 5. 创建登录表单 在 `resources/views/auth/login.blade.php` 文件定义登录表单,如下所示: ```html <form method="POST" action="{{ route('login') }}"> @csrf <div> <label for="email">Email</label> <input id="email" type="email" name="email" value="{{ old('email') }}" required autofocus> @error('email') <span>{{ $message }}</span> @enderror </div> <div> <label for="password">Password</label> <input id="password" type="password" name="password" required autocomplete="current-password"> @error('password') <span>{{ $message }}</span> @enderror </div> <div> <button type="submit">Login</button> </div> </form> ``` 以上就是使用 Laravel 框架实现用户名登录的步骤,希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值