通过在mysql查询后返回空标签,html的大小增加了三倍|如何解决-Laravel [英] html tripled in size by returning empty tags after a mysql query | how to solve - laravel

查看:61
本文介绍了通过在mysql查询后返回空标签,html的大小增加了三倍|如何解决-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何列出未经身份验证的用户(公开页面)的数据以查找其名称?我还想知道是否可以在内部通过id转换用户名.

How to list data from an unauthenticated user (public page) looking for their name? I would also like to know if it is possible to convert the username by id internally.

我有两个表,第一公司和第二个人,在我的代码中,我从URL中获取字符串"name",并在第一表(公司)中进行搜索,如果是肯定的,我将此名称发送给视图,在这里我需要列出个性表中存在的该用户的数据

I have two tables, 1st companies and 2nd personalities,in my code, I take the string "name" from the URL and do a search on the 1st table (companies), if positive, I send this name to the view, where I need to list data this user who are present in the personalities table

在此处输入图片描述

我不知道我做错了什么,但是代码使html变成了三倍,并且没有返回我想要的方式.

I don't know what I did wrong, but the code triples the html and doesn't return the way I want.

我需要这样返回:

<div class="bg-danger"><br><br></div>
<div class="bg-primary"><br><br></div>
<div class="bg-success"><br><br></div>
<div class="bg-info"><br><br></div>

遵循代码

路由器

Route::get('company/{name}', 'CompanyController@searchByName');

控制器

public function searchByName($name)
{
    $company = Company::where('name', $name)->first();
    return view('company.base.index', compact('company', 'name'));
}

视图

<!--- Lochlite: version 3.0.0 country Brazil, lang PT-BR, official site Gameloch Brasil © 2015 - 2019 Gameloch All Right Reserved. --->
<!doctype html>
<html xmlns:og="https://ogp.me/ns#" itemscope="" itemtype="https://schema.org/Corporation" class="no-js" lang="pt pt-BR_ALL" user-region="">
<head data-info="" itemscope="" itemtype="https://schema.org/Organization">
    <meta charset="utf-8">
    <meta content="origin" name="referrer">
    <meta name="geo.country" content="BR">
    <meta name="csrf-token" content="4rtTcBa4csPFlBtHECAmTw6MAh8D5y4ni0H5h49S">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
</body>
    @foreach($company as $company)
        <div class="{{ DB::table('personalities')->select('div_class_1')->where('name', '=', $name)->get() }}"><br><br></div>
        <div class="{{ DB::table('personalities')->select('div_class_2')->where('name', '=', '$name')->get() }}"><br><br></div>
        <div class="{{ DB::table('personalities')->select('div_class_3')->where('name', '=', '$name')->get() }}"><br><br></div>
        <div class="{{ DB::table('personalities')->select('div_class_4')->where('name', '=', '$name')->get() }}"><br><br></div>
    @endforeach      
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>

返回

<div class="[{&quot;div_class_1&quot;:&quot;bg-danger&quot;}]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[{&quot;div_class_1&quot;:&quot;bg-danger&quot;}]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[{&quot;div_class_1&quot;:&quot;bg-danger&quot;}]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[{&quot;div_class_1&quot;:&quot;bg-danger&quot;}]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[]"><br><br></div>
<div class="[]"><br><br></div>

推荐答案

您应该将视图所需的内容传递到视图中,包括在personalities上的查询:

You should be passing what you need for the view into the view, including the query on personalities:

public function searchByName($name)
{
    $company = Company::where('name', $name)->firstOrFail();
    $personality = DB::table('personalities')->where('name', $name)->first();

    return view('company.base.index', compact('company', 'name', 'personality'));
}

查看:

<div class="{{ $personality->div_class_1 }}"><br><br></div>
<div class="{{ $personality->div_class_2 }}"><br><br></div>
<div class="{{ $personality->div_class_3 }}"><br><br></div>
<div class="{{ $personality->div_class_4 }}"><br><br></div>

不需要循环,因为没有要迭代的东西.

No loop needed as there is nothing to iterate over.

这篇关于通过在mysql查询后返回空标签,html的大小增加了三倍|如何解决-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆