0

how to create a function to fetch variables, retrieve them, and access them on the preview page, getting data from a user by the related id in another table?

I need to create a function that looks for the parameter passed by url in the database, more specifically in the table "company", column "name"

the user types company / name in the address bar and laravel should search for this name and find it (if any), with that name in hand, I need it sent to the view page, where it will load bootstrap classes from the personalite table, that is related to the user through the id, I've tried everything, but I don't have much experience

I found this code that does not have much to do with my question, but it was the closest I came, but I didn't copreendi very well, in the end, it didn't work out and I need to finish my project :(

public function retorna_disciplina($id)
    {
        $prontuario = session('prontuario');
        $cargo = session('cargo');

        if($cargo == "P")
        {
            $disciplina = DB::table('oferecimento_disciplina')
                ->where('id_professor','=', $prontuario)
                ->where('dsa', '=', $id)
                ->first();

            if(count($disciplina)>0)
            {
                $postagens = DB::table('postagens')
                    ->where('dsa', '=', $id)
                    ->get();
                return view('disciplinas.disciplina')->with([
                        'disciplina' => $disciplina,
                        'postagens' => $postagens
                    ]);
            }
            else{
                Redirect::to('/perfil')->withErros("A disciplina não existe ou você não tem permissão de acesso");
            }
        }
}

I created a route and it seems to work well

Route::get('/company/{name}', "company/index_controller@search_base_home(name)");

I created a model, a controller and 2 views, 1 if the laravel finds the name and another if not (a search page)

my problem is really creating the function, I'm not getting

tell me what i need to do to put this into practice i would be very grateful for the help

  • There may be something wrong with the code, disagreements or something, sorry if that is the case, just an example for you to understand my question better

Right now I'm trying hard with this here, but nothing, I don't know, doesn't seem to get the url name

$this->validate($request, [
        'name' => 'required|unique:company|name',
    ]); 

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

the view code is this (companybase_lg.blade.php)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>iofrm</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="https://brandio.io/envato/iofrm/html/css/fontawesome-all.min.css">
    <link rel="stylesheet" type="text/css" href="https://brandio.io/envato/iofrm/html/css/iofrm-style.css">
    <link rel="stylesheet" type="text/css" href="https://brandio.io/envato/iofrm/html/css/iofrm-theme1.css">
</head>
<body>
    <div class="form-body">
        <div class="website-logo">
            <a href="index.html">
                <div class="logo">
                    <img class="logo-size" src="images/logo-light.svg" alt=""><!--- db class here --->
                </div>
            </a>
        </div>
        <div class="row">
            <div class="img-holder"><!--- db class here --->
                <div class="bg"></div>
                <div class="info-holder">

                </div>
            </div>
            <div class="form-holder"><!--- db class here --->
                <div class="form-content">
                    <div class="form-items">
                        <h3>Get more things done with Loggin platform.</h3>
                        <p>Access to the most powerfull tool in the entire design and web industry.</p>
                        <div class="page-links">
                            <a href="login1.html" class="active">Login</a><a href="register1.html">Register</a>
                        </div>
                        <form>
                            <input class="form-control" type="text" name="username" placeholder="E-mail Address" required><!--- db class here ---><!--- db class here --->
                            <input class="form-control" type="password" name="password" placeholder="Password" required><!--- db class here --->
                            <div class="form-button">
                                <button id="submit" type="submit" class="ibtn">Login</button> <!--- db class here --->
                                <a href="forget1.html">Forget password?</a>
                            </div>
                        </form>
                        <div class="other-links">
                            <span>Or login with</span><a href="#">Facebook</a><a href="#">Google</a><a href="#">Linkedin</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<script src="https://brandio.io/envato/iofrm/html/js/jquery.min.js"></script>
<script src="https://brandio.io/envato/iofrm/html/https://brandio.io/envato/iofrm/html/js/popper.min.js"></script>
<script src="https://brandio.io/envato/iofrm/html/js/bootstrap.min.js"></script>
<script src="https://brandio.io/envato/iofrm/html/js/main.js"></script>
</body>
</html>

FINAL PART

I tried to get the data to fill in with the classes, but returns nothing related to the name, I don't know if I made a mistake somewhere ...

         @foreach($companies as $company)
                   <div class="{{ $companies->personalite->div_class_1 }}"></div>
                   <div class="{{ $companies->personalite->div_class_2 }}"></div>
                   <div class="{{ $companies->personalite->div_class_3 }}"></div>
                   <div class="{{ $companies->personalite->div_class_4 }}"></div>
         @endforeach

so I need to take div_class_1 which is in the personalite table and put in div

each user has their classes in this table and is related to it by an ID number, I created a column "name" in the table to do the test but so far nothing

  • What you get by running the code? any error message or something that can help? the first obvious problem is that you should not use count($disciplina) but $disciplina->count() because disciplina is a collection not an array – Ali Khalili Oct 13 '19 at 03:59
  • anything I do returns the t_public syntax error or says the parameter is invalid – Macedo_Montalvão Oct 13 '19 at 04:10
  • I read a lot, I tried to find a solution here: https://stackoverflow.com/questions/27095090/laravel-checking-if-a-record-exists but I couldn't – Macedo_Montalvão Oct 13 '19 at 04:13

1 Answers1

1

I don't understand the whole question. But I can answer this part of your question.

Question : I need to create a function that looks for the parameter passed by url in the database, more specifically in the table "company", column "name"

Route

Route::get('companies/{name}', 'CompaniesController@searchByName');

Controller

public class CompaniesController extends Controller
{
    public function searchByName($name)
    {
        $company = Company::where('name', $name)->first();

        return view('your_blade_view', compact('company'));
    }
}
Tharaka Dilshan
  • 4,371
  • 3
  • 14
  • 28
  • thank you very much for the answer, which already helps a lot, but the other part of the question was to get $ company and send it to company.base (.blade.php), I don't know if it's possible anymore, but I wanted to get that name and list everything that is related to him in the personality table – Macedo_Montalvão Oct 13 '19 at 13:23
  • Only this last part of the visualization is missing so that the question can be 100% resolved, if you can help me I would appreciate it very much :) – Macedo_Montalvão Oct 13 '19 at 13:27
  • Now $company is found and it has sent to the view. So explain the next requirement a little bit, So I can help – Tharaka Dilshan Oct 13 '19 at 14:45
  • updated! I put it at the end for you to see, "end part" – Macedo_Montalvão Oct 13 '19 at 15:40
  • Ok, Seems like there are many companies with the same name, right? – Tharaka Dilshan Oct 13 '19 at 15:52
  • yes my fear is this there might be a conflict and it shows the wrong layout page (loading the wrong classes) – Macedo_Montalvão Oct 13 '19 at 15:55
  • Tell us about `personalite` table structure. columns and constrains? – Tharaka Dilshan Oct 14 '19 at 03:08
  • Hello! Tharaka, you helped me a lot, thanks, I already decided here: https://stackoverflow.com/questions/58368641/html-tripled-in-size-by-returning-empty-tags-after-a-mysql-query-how-to-solve/58368763?noredirect=1 – Macedo_Montalvão Oct 14 '19 at 03:24