0

I tried, but don't work: i don't know why...

Page.php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class takeData extends Controller
{
    public function selectdata()
    {
        $data_users = DB::select('select * from persons where id = :id', ['id' => 1]);
        return view('page', ['persons' => $data_users]);
    }
}
....

page.blade.php

....
@foreach ($persons as $person)
    <p>$person</p>
@endforeach

routes.php

Route::get('takeData', 'Page@selectdata');

Error: Whoops, looks like something went wrong.

If I remove '@foreach ($ persons as $ person)

$ person @endforeach' in page.blade.php everything works correctly, why is it wrong? Thanks a lot!!!

1 Answer 1

1

On your view, change <p>$person</p> to this:

 <p>{{ $person }}</p>

The {{ }} is used to output data as specified in the Blade Documentation.

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

4 Comments

{{ $person->id }} or {{ $person }} have same problem
@Helpme The only other area I can think of that could go wrong is in your query. Do you know if it works correctly? If it doesn’t, you can read more about it in the documentation here: laravel.com/docs/5.6/queries#introduction But, I’d try doing something like $data_users = DB::table(‘persons’)->where(‘id’, 1)->get();
Solved, solution: "array('per..." instead of "['per..."
@Helpme ah okay. Good luck with your app.

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.