V
V
Vova1357982022-01-08 00:49:45
Laravel
Vova135798, 2022-01-08 00:49:45

How to get one record from the database?

How can I get one specific record from the database? My solution doesn't work because I don't understand how to put in the id variable, which specifies the post number. If instead $idof writing some number, then everything works

<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class TestController extends Controller
{
    public function show($id)
    {
        $result = DB::select('select * from users where id = ?', [$id]);
        foreach($result as $item){
                $login = $item->login;
                $password = $item->password;
            }
        return view('test',[
            'login' => $login,
            'password' => $password,
        ]);
    }
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ildar-meyker, 2022-01-08
@ildar-meyker

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\User;

class TestController extends Controller
{
    public function show($id)
    {
       $user = User::findOrFail($id);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question