Bring data and save to a variable in Controller - Laravel API

Asked

Viewed 296 times

-1

public function getListaPresencial(Request $request, Presencial $Presencial)
{
    if ($Presencial->id) {
        return response()->json(['data' => $Presencial->toArray()]);
    }

    $query = Presencial::query();
    $tipo = 1;
    $query->select('id', 'nome_professor', 'nome_disciplina', 'nome_curso', 'nome_turma', 'data_inicio');
 // $query->where('id','=',1 );
    $query->where('tipo','=',$tipo );
    $query->whereDate('data_inicio', date('Y-m-d'));

    return response()->json($query->paginate($request->get('limit')));

}

I need to bring the result of the search in a variable:

Example:

$nomeprofessor = ?
$nomedisciplina = ?

so I can write in another table only this data.

  • Just to make sure what version of your Laravel?

2 answers

0

I think if you only want a few fields, it would be better to select only by the desired fields:

$query->select('nomeprofessor');
$professores = $query->get();
$query->select('nomedisciplina');
$disciplinas = $query->get();
  • It probably needs all the ones you already have, and you need to take some specific ones to save to another table

  • Exactly that, I have 6 fields and need to store in variables 4 of them to send to another table.

0

I took the opportunity to make a syntax improvement in your code, leaving more standardized.

You already have the result of your search on $query->paginate($request->get('limit')) but if you want to take all the search values without paging, you can do:

public function getListaPresencial(Request $request, Presencial $Presencial)
{
    if ($Presencial->id) {
        return response()->json(['data' => $Presencial->toArray()]);
    }

    $tipo = 1; // não entendi porque não colocou isso diretamente na query, ma vamos manter

    $query = Presencial::select('id', 'nome_professor', 'nome_disciplina', 'nome_curso', 'nome_turma', 'data_inicio')
        ->where('id', '=', $tipo)
        ->whereDate('data_inicio', today());

    $presenciais = $query->get();
    dd($presenciais);

    return response()->json($query->paginate($request->get('limit')));

}

Thus the variable $presenciais will get the values you need.

The method ->get() returns a Collection with all the values of the search then you can treat it as a Collection in the Laravel.

An example of using a Collection is:

$presenciais->each(static function ($presencial){
    $professor = $presencial->nome_professor;
    $disciplina = $presencial->nome_disciplina;
    // Agora você pode usar as variáveis "$professor" e "$disciplina" para salvar na outra tabela que você precisa

})
  • #perPage: 15 +exists: true +wasRecentlyCreated: false #Attributes: array:6 [ "id" => 5 "teacher name" => "AVELAR SILVA TEST" "discipline name" => "DISCIPLINE TEST" "course name" => "TEST 1" "classname" => "FIFTH SERIES" "start_date" => "2019-09-09" ] #original: array: ==> How can I separate each variable string? $discipline name = get(?) ? I’m new and I’m lost, thanks for the help

  • https://laravel.com/docs/5.8/collections#available-methods have ways to use a Collection, remember that your search can (and will) bring more than one result, so you need to treat each result of your collection, I recommend you use the each()

  • edited my answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.