How to pass data from two tables in one View?

Asked

Viewed 1,006 times

0

I’m trying to pass data from two tables(SQL) in a table structure HTML. When I try to pass to mine View, the Laravel does not recognise the relationship method.

View - atualizador.blade.php

@extends('layouts.sidebar')
@extends('layouts.app')
@section('content')


<table class="table">

  <table style="width:80%; margin-left: 10%;">
 <tr>

   <th>Imagem</th>
   <th>Código</th>
   <th>Produto</th>
   <th>Custo</th>
   <th>Preço</th>
   <th>Atualização</th>
   <th>Status</th>
   <th>Estoque</th>
   <th>Distruibuidor</th>
   <th>Ações</th>
 </tr>
 <tr>
   @foreach($produtos as $produto)
   <td>Imagem</td>
   <td>{{$produto->erp_productid}}</td>

  <td>{{$produto->produtosdescricao->erp_name}}</td>
   <td>{{$produto->erp_cost}}</td>
   <td>{{$produto->erp_price}}</td>
   <td>{{$produto->erp_modifieddate}}</td>
   <td>{{$produto->erp_status}}</td>
   <td>{{$produto->erp_quantity}}</td>
   <td>{{$produto->erp_distributor}}</td>
   <td></td>

 </tr>

@endforeach
</table>
</table>
<center>{{$produtos->links()}}</center>
@endsection

Controller - ProdutosController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Produtos;
use App\ProdutosDescricao;

class ProdutosController extends Controller
{
    public function index()
    {
      $id = 3;
      $produtos = DB::table('erp_product')
                    ->paginate(25);
      $p = Produtos::find(3);
      return view('atualizador')
              ->with('produtos', $produtos)
              ->with('p', $p);
    }
}

Model - Produtos.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Produtos extends Model
{
    protected $table = 'erp_product';
    protected $primaryKey = 'erp_productid';

    public function produtosdescricao()
    {
        return $this->hasOne('App\ProdutosDescricao', 'erp_name');
    }
}

Rota

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

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

Route::get('/atualizador', 'ProdutosController@index');
Route::get('/atualizaBanco', 'HomeController@atualiza');
Route::get('/insereBanco', 'HomeController@insere');

The code returns the error:

Undefined property: stdClass::$produtosdescricao (View: C:\comerciourbano\resources\views\atualizador.blade.php)

Any suggestions?

  • 1

    How’s your product/product relationship?

  • Hello, Miguel, I declared as hasOne in Products and Model Products: <?php&#xA;&#xA;namespace App;&#xA;&#xA;use Illuminate\Database\Eloquent\Model;&#xA;&#xA;class ProdutosDescricao extends Model&#xA;{&#xA; protected $table = 'erp_product_description';&#xA; protected $primaryKey = 'erp_productid';&#xA;&#xA; public function produto()&#xA; {&#xA; return $this->belongsTo('App\Produtos');&#xA; }&#xA;}

  • But there you go belongsTo, you don’t have hasOne, and you should do the same in the product model

1 answer

0


Do so:

Instead of using the classe DB use the classe Produtos:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Produtos;
use App\ProdutosDescricao;

class ProdutosController extends Controller
{
    public function index()
    {
      $id = 3;
      $produtos = Produtos::with('produtosdescricao')
                           ->paginate(25);

      $p = Produtos::find(3);
      return view('atualizador')
              ->with('produtos', $produtos)
              ->with('p', $p);
    }
}

this way the data is loaded (including with good performance) for your View.

In relation, you need to see how your tables are configured.

References:

  • You spoke "even with good performance" in the case of the use of return view('my.view', compact('var1', 'var2'). Is less performatic?

  • @Luitame is not at this point is at the point where you want to use a technique that does not exist in the DB, but, yes in the Eloquent which is charging when accessing the method this is not performative, it is in this aspect. Exactly here: <td>{{$produto->produtosdescricao->erp_name}}</td>, when using the with is performatic, when not used is not because each interaction is generated a new SQL and with with are only two SQL.

  • Thank you Virgilio Novic, I did it the way you said it and it worked!

  • @Viniciusrosa do not forget to accept with reply.

  • @Viniciusrosa pasta I asked not as hater but to learn from your feedback, because I was curious. I also use Laravel in some projects.

  • 1

    @Luitame I started using Laravel in college, but I didn’t go too deep, I still have many difficulties, but with time getting the hang of it, if you are interested in forming some group so that we can exchange ideas about the platform, I accept :)

  • @Viniciusrosa already exist groups with enough volume and quality. Search for Laravel Brasil. It has in Telegram, Slack and even in the forum on Github. I’m already there join the tribe.

Show 2 more comments

Browser other questions tagged

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