1
votes

I'm starting to learn Laravel, but not surprisingly, I'm bumping into problems. This one I know is basic, but I can't solve. I'm trying to make a simple SELECT from the database but it shows nothing!

This is the code

web.php

Route::get('list', ['uses' => 'robotController@index']);

robotController

public function index()
{
    $robots = DB::select('select * from robots where Name = ?', [1]);
    return view('ListaRobots', ['robots' => $robots]);
}

listaRobots.blade.php

@foreach ($robots as $robot) {{ $robot['Name'] }} @endforeach

I'm trying to make a foreach that gets the "Name" of all the robots but I can't get anything. The screen goes all blank. But I have two entries in the robots table. I have changed the .env to connect to the DB, and managed to use seed to create entries in the table but cant make queries.

2
You're selecting only robots which have the name 1. - tkausl
I tried that before (putting 1 to the name) and then i get this error: "Cannot use object of type stdClass as array" - Adato

2 Answers

2
votes

It looks like you are trying to get a list of robots by name. So change a few things.

Routes:

Route::get('/robots/{name}', ['uses' => 'robotController@show']);

Controller:

function show($name) {
    $robots = DB::table('robots')->where('name', '=', $name)->get();
    return view('ListaRobots', ['robots' => $robots]);
}
1
votes

You are selecting all rows where Name equals 1. Are you sure that's what you want?

You might want to rename Name to id.