Laravel: how do I pass obj std class to my view and call it and the difference with passing a simple array?

StackOverflow https://stackoverflow.com/questions/19331438

  •  30-06-2022
  •  | 
  •  

문제

I would like to ask on how will I display this type of array in laravel blade view?

Array(
[0] => stdClass Object
    (
        [id] => 2
        [email] => email@email.com
        [username] => dodie
        [firstname] => Jhon
        [lastname] => David
        [password] => $2y$08$x12gVo54SsUW0U1aHTHaXu7VbgHFG8So9.fd/DfczT7KOyk4J1nLS
        [location] => {'long':47.061469, 'lat': 8.321743}
        [photo] => 1.jpg
        [hometown] => England
        [Updated] => 1
        [description] => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        [role] => client
        [deleted_at] => 
        [created_at] => 2013-10-10 06:00:18
        [updated_at] => 2013-10-10 07:24:22
        [reviewer_id] => 1
        [company_id] => 2
        [title] => Very Bad Ass
        [content] => badass
        [review_rating] => 1
        [helpful] => 0
        [logo] => 2.jpg
        [contactperson] => Jhon Doe
        [companyname] => Hardy Contractor
        [address] => makati Philippines
        [type] => Heating and Airconditioning
        [stars] => 2
        [phone] => 554-9266
        [fax] => Hardy Contractor
        [website] => www.hardycontractors.com
    )

This is how I pass this In my Controller:

$data = DB::select($query); //fetching data using Query Builder
return View::make('home')->with('users', $data);

this is how I call it on my blade view:

$users->website

Every time I call this on my blade can you please enlighten me? :) I want to understand more on how I can handle this kind of problem.

도움이 되었습니까?

해결책

What you have is an array of std class object.

In your view you can simply loop through them:

@foreach($users as $user) 
 {{ $user->website }}
@endforeach

In case you have one and only one object in the array, you might want to consider passing $users[0] to the view and then work with it as a single object.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top