2

So this a really newbie question but I need a better understanding of how does work Laravel and PHP.

I'd like to access an array from the blade with something like {{$article->title}} or {{$article['title']}}

The only way I found it was to create an stdClass() and attribute each of the value of the array to the new object.

    $article = new \stdClass();

    $article->id = "";
    $article->title = "";
    $article->content = "";
    $article->created_at = "";
    $article->updated_at = "";

    if($var != null){
        $tmp = DB::select('select * from articles where id = ' . $var);
        $article->id = $tmp[0]->id;
        $article->title = $tmp[0]->title;
        $article->content = $tmp[0]->content;
        $article->created_at = $tmp[0]->created_at;
        $article->updated_at = $tmp[0]->updated_at;
    }

However this is pretty ugly and I'd like to know what is the proper way of doing it.

I tried to keep it simple with

$article = DB::select('select * from articles where id = ' . $var);

But I dont find a way to access $article in my blade template.

This is the var_dump of it :

array (size=1)
  0 => 
    object(stdClass)[170]
      public 'id' => int 1
      public 'title' => string 'Test' (length=4)
      public 'content' => string '<p>Test</p>' (length=151)
      public 'created_at' => string '2016-12-21 10:00:04' (length=19)
      public 'updated_at' => string '2016-12-21 10:15:57' (length=19)
1
  • $article = $tmp[0]; would simplify your example above. But ->first() would also be more correct as Alexey suggests. Commented Dec 21, 2016 at 10:52

2 Answers 2

2

I'd recommend you to learn Eloquent and Collections. It's the proper way. Using these you can simply get the data:

$article = Article::where('id', $var)->first();

Then pass $article to the view and access it with:

{{ $article->title }}
Sign up to request clarification or add additional context in comments.

Comments

0

Explainations

Laravel is really intuitive. Think of it as if it was something from the real life. You ask your intern to deliver all the articles. You have now all the articles and you can now proceed on displaying them in your paperwork.

This is mostly the same with Laravel. You ask the facade DB to deliver you ALL THE ARTICLES. You will then cast the result in a variable most of the time. So your variable should be named $articles and not $article.

Then, you can use the @foreach () syntax in Laravel Blade to iterate through all your articles. It's really that simple and intuitive. Just work like if you were in the real world asking something. That's how Laravel has been conceived.

So, for each articles that the facade got you (only one), you'll have to make something out of it.

There is a great (if not awesome) course for free you can check on the Laracast webpage with host Jeffrey Way that is explaining all of that with a simple example with cards.

Source-code

<!-- I consider the variable you return in the view to be $articles -->

@foreach ($articles as $article)

    <h1>{{ $article->title }}</h1>

    <!-- some more HTML code as you wish to display it -->

@endforeach

Documentation

Laravel 5.3 : Blade.

Laracast : Laravel From Scratch (Fetching datas).

Comments

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.