0

I am working on a Laravel project that does custom SQL commands from input. I have the following functions.

Route

 Route::patch('/form-submit', function(Request $request){
  $query = $request->input('query');
  return view('welcome', compact('query'));
});

Welcome.blade.php

<form method="POST" enctype="multipart/form-data" action="/form-submit" class="text-end">
   {{ method_field('PATCH') }}
   @csrf
   <input name="query" value="{{ $query }}" id="input" class="form-control w-20">
   <button type="submit" id="button" class="btn btn-sm btn-info mt-3">Submit</button>
 </form>

<?php
  $querys = DB::select($query);
?>

Now I want to loop through all the values that the SQL command gives me. For example, for the input "SELECT * from users" I will get all the users and I can show their name using:

@foreach($querys as $data)

  {{$data->name}}

@endforeach

But I would like to show all the data for the user, including name, sex, id, age, etc. I tried multiple options but I didn't find yet a solution. The query will always be different so I will not know what data do I have to show. For example, if I type "SELECT * from notifications" I want to add all the informations from the database in a table.

Can anyone help me?

Thank you

8
  • where is the query you are executing? where is the code Commented Jan 28, 2021 at 20:11
  • the code is in the blade, I have an input with name query: <input name="query" value="{{ $query }}" id="input" class="form-control w-20"> Commented Jan 28, 2021 at 20:36
  • show the code where you are calling the query ... which should not be in a view Commented Jan 28, 2021 at 20:37
  • I updated now the code Commented Jan 28, 2021 at 20:44
  • 1
    you know this is a horrible idea right? to take a sql query directly from a user and run it against the database Commented Jan 28, 2021 at 20:51

1 Answer 1

1

You can return array from Model use ->toArray() method and then you will have associative array. You can do something like that

@foreach($querys as $field=>$value)

<p> {{$field}} = {{$value}} </p>

@endforeach

Where $field it's field name in table and $value it's column value. Also you need check nesting, if you have several results

Sign up to request clarification or add additional context in comments.

5 Comments

I tried this but I get the following error: tmlspecialchars() expects parameter 1 to be string, object given
Yes if you have a several result, you will have array where all element is object. So you need two loop, in the second loop you can get your results
This is what I meant about nesting. I think you should do you query then parse result in your service (or where you have business logic) and then return result array to your template
It works, But now I can get just the values. For example for user with {"id":2,"name":"user2"} I can show just 2 and user2. Any idea on how can I show also id and name?
Do you use loop like this @foreach($querys as $field=>$value) ? Here $field must be your keys id and name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.