R
R
romentigo2020-04-13 16:29:21
Perl
romentigo, 2020-04-13 16:29:21

How to get data from a table in Laravel?

Hello!
There is such a controller with the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class LectViewController extends Controller
{
    public function index(){
        $lecturers = DB::table('Преподаватели')->select('id_Преподавателя', 'ФИО')->get();
        $id = $lecturers->with('id_Преподавателя')->get();
        $name = $lecturers->with('ФИО')->get();
        return view('account')
        ->with(compact('id'))
            ->with(compact('name'));
    }
}


There is a table with information about teachers. In this case, I'm making a request to get a table with two columns: teacher_id and full name . Everything works fine, but I need to take the columns separately from the $lecturers array and write them to two other arrays: $id and $name . Those. to have 2 column arrays. What for? You need to pass them to the Vue component. Its code is like this:
<template>
    <table class="ViewData">
        <thead>
            <tr>
                <th>ID</th>
                <th>ФИО</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="info in lecturers">
                <td>{{info.id}}</td>
                <td>{{info.name}}</td>
            </tr>
        </tbody>
    </table>
</template>

<script>
    export default {
        name: "ViewTable",
        props: [
            'lecturers'
        ]
    }
</script>

<style scoped>

</style>


I looked at various information on the Internet about the transfer of data information from Laravel to Vue, but everywhere they print information themselves, like this:
$data = [
    ['id' => 1, 'name' => 'Admin'],
    ['id' => 2, 'name' => 'Truehero'],
    ['id' => 3, 'name' => 'Truecoder'],
];

return view('test', ['data' => $data]);


Those. here we know everything, but what to do when we do not know this and climb into the database for data? How to get two columns separately from $lecturer array ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alienator, 2010-11-23
@nikitad

Sys::Path

A
Anton Anton, 2020-04-13
@romentigo

Why exactly two columns are needed is not clear at all. The method of obtaining data is tin.
And for the transfer of the data itself, https://laravel.com/docs/7.x/blade#displaying-data will help , you need to scroll a little lower and see:


The @jsondirective is also useful for seeding Vue components or data-* attributes:
<example-component :some-prop='@json($array)'></example-component>

Using @jsonin element attributes requires that it be surrounded by single quotes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question