Laravel DB选择查询

时间:2016-09-17 22:50:59

标签: laravel-5

我正在学习Laravel 5.需要有关查询和刀片打印的帮助。 我有一个包含用户信息的表和一个包含Steam信息的表,其中我有user_id行。 如何构建此查询:

SELECT username FROM steam WHERE id = user_id;

user_id是会话

中的用户

用户表

id | email | username | password | 

蒸汽表

id | user_id | username | steam_id | avatar

Steam.php

<?php

namespace SGN\Models;

use SGN\Models\User;
use Illuminate\Database\Eloquent\Model;
use Invisnik\LaravelSteamAuth\SteamAuth;

class Steam extends Model
{
    protected $table = 'steam';

    protected $fillable = [
        'user_id',
        'username',
        'avatar',
        'steamid',
    ];

    protected $hidden = [
        'remember_token',
    ];
}

SteamController.php

<?php

namespace SGN\Http\Controllers;

use Auth;
use SGN\Models\Steam;
use Invisnik\LaravelSteamAuth\SteamAuth;

class SteamController extends Controller
{
    private $steam;

    public function __construct(SteamAuth $steam)
    {
        $this->steam = $steam;
    }

    public function login()
    {
        if ($this->steam->validate()) {
            $user_id = Auth::user()->id;
            $info = $this->steam->getUserInfo();
            if (!is_null($info)) {
                $user = Steam::where('steamid', $info->getSteamID64())->first();
                if (is_null($user)) {
                    $user = Steam::create([
                        'user_id' => $user_id,
                        'username' => $info->getNick(),
                        'avatar'   => $info->getProfilePictureFull(),
                        'steamid'  => $info->getSteamID64()
                    ]);
                }
                //Auth::login($user, true);
                return redirect('/'); // redirect to site
            }
        }
        return $this->steam->redirect(); // redirect to Steam login page
    }

    protected function create(array $data)
    {
        return Steam::create([
            'username' => $data['username'],
            'steamid' => $data['steamid'],
            'avatar' => $data['avatar'],
        ]);
    }
}

edit.blade.php

@section('content')
    <h3>Update your profile</h3>
    <div class="row">
        <div class="col-lg-3">
              Here print steam username from DB
        </div>
    </div>
@stop

0 个答案:

没有答案