简体   繁体   中英

How to call an array from a data file in a blade template with Laravel?

I'm learning Laravel and I need your help to display an array in a blade template. First,I created a data file containing an array in App (I don't use a database in my project yet => everything is in a file).But in my template, I can't include this file and then loop over it. I tried with @includes, required and use but nothing works .Besides, I don't quite understand the difference between the three if someone can explain it to me.. However, my dd works very well. Can I have your help pleaz? Thank you very much

App/Data.php

<?php
namespace App;
class Data{

    public static function getSkills()
    {
        return [
            // array data
            array("name" => "HTML5"),
            array("name" => "CSS3"),
            array("name" => "Sass"),
            array("name" => "Bootstrap"),
            array("name" => "JavaScript"),
            array("name" => "MySQL"),
            array("name" => "PHP"),
            array("name" => "React JS"),
            array("name" => "Redux"),
            array("name" => "GitHub"),
            array("name" => "Trello"),
            array("name" => "VSCode"),
            array("name" => "Linux Ubuntu"),
        ];
    }
}

SkillsController

<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;

use App\Data;

class SkillsController extends Controller
{

    public function allSkills()
    {
        return view('skills', [
            'skills' => Data::getSkills()
        ]);
    }
}

Skills.blade.php

@include('layouts/navbar')

<div id="competences">
    <div className="skills">
      <h1 className="skills-title"><span>*</span>Skills</h1>
      <h2 className="subtitle"><span>•</span> Languages and frameworks</h2>
      <ul className="skills-list">
      @foreach ($skills as $skill)
    <p>This is user {{ $skill->name }}</p>
        @endforeach

          <li className="skills-list-skill" key={skill.icon}>
            <img src={skill.icon} alt="" className="skills-list-skill-icon" />
            <p className="skills-list-skill-title">{skill.title}</p>
          </li>

      </ul>
    </div>
    <div className="secondary-skills"
    </div>
  </div>
@include ('layouts/footer')

文件

Finally found an issue! My routing was :

Route::get('/competences',SkillsController@allSkills);

Actually the good method is :

Route::get('/competences',[SkillsController::class, 'allSkills']);

Nothing was working until I found this on Laracast's forums

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM