How to pass data from two unrelated tables in a view?

Asked

Viewed 75 times

1

I’m trying to move to a view, data from two unrelated tables. Appear only the data of one, the other, They are invisible, but you can notice the change in the layout when these data are loaded. Another fact that I found curious, is that there is no mistake.

For better insight, here’s the code:

Layout inserir a descrição da imagem aqui Controller.php

abstract class Controller extends BaseController
{
    use DispatchesJobs, ValidatesRequests;

    protected function getLemas() {
        return Lema::where('status_lm', 'Activo')->get();
    }

    protected function getIgreja() {
        return Lema::All();
    }
}

Lemacontroller

public function create()
    {

        return view('lema.create-lema')->with('lema', $this->getLemas())->with('igreja',$this->getIgreja());
    }

    public function store(Request $request)
    {
        $lema=new Lema();
        $lema->texto_lm = $request->texto;
        $lema->refBilblica_lm = $request->refBiblica;
        $lema->status_lm = $request->status;
        $lema->save();
        return redirect('/');
    }

Igrejacontroller

 public function create()
    {
        return view('igreja.create-igreja')->with('lema', $this->getLemas())->with('igreja',$this->getIgreja());
    }


    public function store(Request $request)
    {
        if($request->hasFile('logo'))
        {
            $logo = $request->file('logo');
            $extensao = $logo->getClientOriginalExtension();

            if($extensao != 'jpg' && $extensao != 'jpeg' && $extensao != 'png')
            {
                return back()->with('erro','Erro: Este arquivo não é uma imagem JPG ou PNG');
            }
        }

        $igreja = new Igreja();
        $igreja->nome_ig = $request->nome;
        $igreja->missao_ig = $request->missao;
        $igreja->valores_ig = $request->valores;
        $igreja->visao_ig = $request->visao;
        $igreja->historial_ig = $request->historial;
        $igreja->endereco_ig = $request->endereco;
        $igreja->telefone_ig = $request->telefone;
        $igreja->telefone1_ig = $request->telefone1;
        $igreja->email_ig = $request->email;
        $igreja->logo_ig = "";
        $igreja->save();

        if(Input::file('logo'))
        {
            File::move($logo,public_path().'/imagem-logo/igreja-id_'.$igreja->id.'.'.$extensao);
            $igreja->logo_ig = '/imagem-logo/igreja-id_'.$igreja->id.'.'.$extensao;
            //$post->imagem = public_path().'/imagem-post/post-id_'.$post->id.'.'.$extensao;
            $igreja->save();
        }

        return redirect('/');
    }

Homecontroller

public function index()
    {

        return view('home')->with('lema', $this->getLemas())->with('igreja',$this->getIgreja());

    }

Route

Route::get('/', 'HomeController@index');

Route::post('criar-lema', 'LemaController@store');
Route::get('criar-lema', 'LemaController@create');

Route::get('criar-igreja', 'IgrejaController@create');
Route::post('criar-igreja', 'IgrejaController@store');

View banner.blade.php

<div class="page-header page-header-home">

    <div class="container" style="margin-bottom: 0px; background-color: #85B200">
        <?php foreach ($igreja as $key => $value): ?>
            <img src="{!! url ($value->logo_ig) !!}" alt="...">
        <?php endforeach; ?>

        <a href="#"> <i class="glyphicon glyphicon-user" style="float: right; position: relative; margin-top: 100px; margin-right: 20px; color: #FFFFFF">Iniciar Sessão</i></a>
        <div style="padding: 2px; background-color: #4C6600; width: 450px; height: 90px; border-radius: 15px; position: relative; float: right; margin-right: 190px; margin-top: 22px">
            <?php foreach ($lema as $key => $value): ?>
            <p style="padding: 0px; text-align: center; font-family: 'Tw Cen MT', Arial, 'Helvetica LT Std';font-size: 22px; color: #ffffff; line-height: 90%;">{!! $value->texto_lm  !!}</p>

            <p style="padding: 0px; text-align: center; font-family: 'Tw Cen MT', Arial, 'Helvetica LT Std';font-size: 15px; color: #ffffff; line-height: 90%;">{!! $value->refBilblica_lm  !!}</p>
            <?php endforeach; ?>

        </div>

    </div>

</div>

Please, I need help, and I thank you.

  • Which route are you trying to access?

  • The first "Route::get('/', 'Homecontroller@index');"

  • before the Return of your Homecontroller@index method put dd( $this->getLemas()) and then dd($this->getIgreja()), and place the results so we can see what is happening

  • Just bringing me the dd($this->getLemas data()).

  • Both dd($this->getLemas()), and dd($this->getIgreja()), are returning me only the data from the table "lemmas".

  • Yes, because $this->getLemas() is returning data from the Lemma table where 'status_lm' is equal to 'Active' and $this->getIgreja() is returning all data from the Lemma table, this is not the behavior you want?

  • No. What I want is the $this->getIgreja() to return the data from the "church" table, which contains the logo that has to appear on the banner. And $this->getLemas(), returns the data from the Lemma table where 'status_lm' is equal to 'Active''

Show 2 more comments

1 answer

1


Looking fast at your code both the getLemas() as to the getIgreja() are searching for the data of Lemas

protected function getLemas() {
    return Lema::where('status_lm', 'Activo')->get();
}

protected function getIgreja() {
    return Lema::All();
}

The getIgreja() shouldn’t be:

protected function getIgreja() {
    return Igreja::all();
}

As in the banner.blade.php you are passing the value to the src image, apparently does not give error and the image does not load, because it is probably rendered as:

<?php foreach ($igreja as $key => $value): ?>
    <img src="{!! url ($value->logo_ig) !!}" alt="...">
<?php endforeach; ?>

//no html deve ficar algo como:
<img src="null" alt="...">
  • It worked!!!! Actually the fault was in the protected Function getIgreja() { Return Lemma::All(); }, when it should be protected Function getIgreja() { Return Church:::all(); }

  • @Danieldossantos taking advantage I recommend you to use the syntax of Blade instead of codes <?php ?>, so you could take advantage of the benefits of the framework and leave your code standardized. https://laravel.com/docs/5.5/blade

  • Thank you @Andregusmao!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.