我想引用项目中 views 文件夹中其中一个文件中的数据库表之一



我想引用项目中 views 文件夹中其中一个文件中的一个数据库表。我该怎么做?例如:显示名为 sub 的列的值,该列位于我的数据库的配置表中

请注意,我知道html不是服务器端语言;我只想显示我的信息,而不是编辑它

如果要使用 MVC 原则,可以使用 Eloquent 模型映射数据库信息,使用控制器将模型数据发送到视图,并使用 Blade 在视图中显示该数据。

只是一个基本的例子:

Config.php(App文件夹中的模型(:

class Config extends Model
{
    /**
     * The table associated with the model, only needs to be defined 
     * if your table name isn't a plural of your model name
     *
     * @var string
     */
    protected $table = 'configs';
}

HomeController(AppHttpControllers中的控制器(

use AppConfig;
...
class HomeController extends Controller
{
    public function index()
    {
        $configs = Config::all();
        return view('index', ['configs' => $configs]);
    }
}

index.blade.php (查看resources/views内部(

@foreach ($configs as $config)
    {{ $config->sub }} // This prints the value of the sub column for every row in the configs table
@endforeach

好吧,您可以通过DB外观访问它。

$column =  DB::table('configs')->pluck('sub');

如果你有一个Config模型,你可以像这样得到它:

$config = Config::find(1);
$config->sub;

您也可以在此处查看文档:https://laravel.com/docs/5.7/queries

如果你想显示数据,你只需做一个回显对于单个元素,请执行此操作

{{ $mode->colum_name }}

如果你有多个元素,你有

这样的
@foreach($models as $model)
    {{$model->column_name}}
@endforeach

相关内容

最新更新