2

I'm trying to figure out how I can get the query result like $residence into the data array. because whem im doing this is gives me the error Array to string conversion. Is there any possible way to convert the query result to a normal string?

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function insert(Request $request)
{




    $id = auth()->user()->id;
    $title = $request->input('title');
    $clientid = $request->input('client');
    $startdate = $request->input('startdate');
    $enddate = $request->input('enddate');
    $starttime = $request->input('starttime');
    $endtime = $request->input('endtime');
    $description = $request->input('description');
    $firstname = DB::select('select firstname from clients where id='.$clientid);
    $lastname = DB::select('select lastname from clients where id='.$clientid);
    $housing = DB::select('select housing from clients where id='.$clientid);
    $housenr = DB::select('select housenr from clients where id='.$clientid);
    $residence = DB::select('select residence from clients where id='.$clientid);


    $residencestring = json_encode($residence);






    $data=array(
        "uuid"=>$id,
        "title"=>$title,
        "residence"=>$residencestring,
        "startdate"=>$startdate,
        "enddate"=>$enddate,
        "starttime"=>$starttime,
        "endtime"=>$endtime,
        "description"=>$description,
        "firstname"=>$firstname,
        "lastname"=>$lastname,
        "housing"=>$housing,
        "housenr"=>$housenr

    );
    //dd($data);
    DB::table('tasks')->insert($data);
    return redirect('/todo');
}
Plance Development
  • 45
  • 1
  • 1
  • 13
  • `$residence = DB::select('select residence from clients where id='.$clientid)->toSql()` Wait, do you want a whole query or only residence name from the query ? – Akhtar Munir May 23 '20 at 13:10
  • So I have these queries `$firstname = DB::select('select firstname from clients where id='.$clientid); $lastname = DB::select('select lastname from clients where id='.$clientid); $housing = DB::select('select housing from clients where id='.$clientid); $housenr = DB::select('select housenr from clients where id='.$clientid); $residence = DB::select('select residence from clients where id='.$clientid);` and they need to be strings to insert it – Plance Development May 23 '20 at 13:24
  • because now the query result is given in an array – Plance Development May 23 '20 at 13:24

2 Answers2

0

Notice how you are doing one query for each field? Also, you are getting an array on each query, since the DB::select returns an array with one row, not the row directly as you think.

I would use Query Builder for this for a more elegant solution:

$client = DB::table('clients')->where('id', $clientid)->first();

With this, you have an object named $client that has all the fields from that row. Then, you can just update the row as follows:

$data = [
   'lastname' => $client->lastname,
   'firstname' => $client->firstname
];

You could even make it more "Laravel" by using Models.

App/Models/Client.php

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Client extends Model {
   protected $guarded = ['id'];
}

Then your code would look something like this:

<?php
use App\Models\Client;

public function insert(Request $request)
{
    ....
    $client = Client::findOrFail($clientid);

    $data = [
       'lastname' => $client->lastname,
       'firstname' => $client->firstname
    ];
    ....
}

The findOrFail function gives you the first register it finds on the table based, equivalent to a "where id=$clientid"

You could go even further and insert using Eloquent as well, as so:

$task = new Task;
$task->lastname = $client->lastname;
$task->firstname = $client->firstname;
$task->save();

or:

$task = Task::insert($data);

where Task is a Model as described previously.

JoeGalind
  • 3,545
  • 2
  • 29
  • 33
  • I think you misunderstood me, residence, firstname, lastname, housing and housenr are colums in the table clients. and for example the row with id = 1 is the row wherte I want to get these values from.3 – Plance Development May 23 '20 at 13:45
  • Yes. Exactly. You have a table named "clients" with the firstname, lastname columns. Then with the code provided you get all the fields from a certain id, and you have them available in that $client object. You don't need one query for each field, you can get them all on one query – JoeGalind May 23 '20 at 13:47
  • I found it! big thanks! I just had to replace residence with '*' ! – Plance Development May 23 '20 at 13:48
  • O yeah, you are right.. or you may remove the select statement all together. Look into the other options I suggested. It leads on the longrun to a very clean code. Plesae mark as answered if it helped you. – JoeGalind May 23 '20 at 14:24
0

So big thanks to @JoeGalind1! The solution was pretty simple, I had to use the build-in query builder. Instead of using an oldschool query.

This is the solution that worked for me!

$client = DB::table('clients')->select('*')->where('id', $clientid)->first();

once you made this query you can easily call it like this:

$data=array(
        "residence"=>$client->residence,
            );

Now there are no problems with string conversion and arrays when you need to insert afterwards.

Plance Development
  • 45
  • 1
  • 1
  • 13