Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php][mysql]model i pobieranie list
snerf
post
Post #1





Grupa: Zarejestrowani
Postów: 74
Pomógł: 3
Dołączył: 30.03.2014

Ostrzeżenie: (0%)
-----


Witam

modele:
  1. class Product extends Model
  2. {
  3. protected $fillable = [
  4. 'category_id',
  5. 'product_number',
  6. 'img'
  7. ];
  8. public $table = 'products';
  9.  
  10. public function categories(){
  11. return $this->belongsToMany('App\Category');
  12. }
  13. public function getCategoryIdAttribute(){
  14. dd($this->categories->lists('id')->ToArray()); <-- tu jest błąd
  15.  
  16. }
  17. }

  1. class Category extends Model
  2. {
  3. protected $fillable = [
  4. 'name',
  5. 'img'
  6. ];
  7. public $table = 'categories';
  8.  
  9. public function products(){
  10. return $this->hasMany('App\Product');
  11. }
  12. }

mysql:
-categories
:id
:name

-products
:id
:category_id

error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'lavarel.category_product' doesn't


Rozumiem że błąd mam z pivotami?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
Pyton_000
post
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Tak
Go to the top of the page
+Quote Post
snerf
post
Post #3





Grupa: Zarejestrowani
Postów: 74
Pomógł: 3
Dołączył: 30.03.2014

Ostrzeżenie: (0%)
-----


Miałem nadzieje że odpowiesz (IMG:style_emoticons/default/tongue.gif)

Pivoty nie są po to by zrobić osobną tabelke? w moim przypadku była by to:

-category_product
:id
:product_id
:category_id


Tylko że ja jej nie potrzebuje gdyż u mnie jeden produkt ma posiadać tylko jedną kategorie.
Go to the top of the page
+Quote Post
Pyton_000
post
Post #4





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


zamień w Product na "belongsTo"

PS. Nie długo zaczną mnie nazywać tu LaraMaster (IMG:style_emoticons/default/tongue.gif)

Ten post edytował Pyton_000 9.09.2015, 13:20:04
Go to the top of the page
+Quote Post
snerf
post
Post #5





Grupa: Zarejestrowani
Postów: 74
Pomógł: 3
Dołączył: 30.03.2014

Ostrzeżenie: (0%)
-----


Próbowałem już tak i niby można lecz wtedy nie zadziała lists:

  1. public function getCategoryIdAttribute(){
  2. dd($this->categories->lists('id')->ToArray());
  3.  
  4. }

Nie rozpoznaje funkcji lists. Bo nie zwraca mi l.mnogiej ?:/

Go to the top of the page
+Quote Post
Pyton_000
post
Post #6





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


a co Ci zwraca ->categories ?
tak w ogóle to po co konwertujesz to na array? Z Collection też się bardzo fajnie korzysta.
Go to the top of the page
+Quote Post
snerf
post
Post #7





Grupa: Zarejestrowani
Postów: 74
Pomógł: 3
Dołączył: 30.03.2014

Ostrzeżenie: (0%)
-----


na belongsto zwraca null a na many error z 1 postu.
na array bo chce wyświetlić to w formularzu by podświetlić dany select z bazy danych


@edit
Dobra mam... wystarczyło przenieść
  1. public function getCategoryIdAttribute(){
  2. return $this->categories->lists('id')->ToArray();
  3.  
  4. }

do modelu category i tyle.

nie rozumiem czemu. ale działa póki co.

Dzięki pyton za chęć niesienia pomocy! +++

Ten post edytował snerf 9.09.2015, 23:57:00
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.08.2025 - 00:22