简体   繁体   中英

Understanding laravel relations and their usage

I've had a few struggles with relations in laravel and how to use them to later get information from those database tables.

I would love if someone will correct me and tell how o correct my mistakes to something that i have not understood.

I will go through a problem that i have and tell how i understand this and that works, and please do correct me if i'm wrong.

So, my problem:

I have this uni semesters final project to do - e-commerces website. I have now come across a problem that my view controller does not understand varriables that i want to use.

I will go throu it all now.

So, i have two tables that i am using to get goods that i have places in DB.

This is my subcategory table from where i will get each subcategories goods

Schema::create('apakskategorijas', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nosaukums');
            $table->integer('kategorijas_id')->unsigned()->index();
            $table->foreign('kategorijas_id')->references('id')->on('kategorijas');
            $table->timestamps();
        });

This table references a field that is connected with main category table, but that is not important right now.

And this is a table, where i store my goods.

Schema::create('preces', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nosaukums');
            $table->decimal('cena');            
            $table->string('path');
            $table->integer('apakskategorijas_id')->unsigned()->index();
            $table->foreign('apakskategorijas_id')->references('id')->on('apakskategorijas');
            $table->timestamps();
        });

As you can see, i have referenced id field from subcategory table. So far so good.

Now, i have created model for each table, where i place my relations.

This is my subcategory class in model

class apakskategorijas extends Model
{
    //relācija ar modeli kategorijas
    public function kategorija()
    {
        return $this->belongsTo('kategorijas');


    }
    public function prece()
    {
        return $this->hasMany(preces::class,);
    }
}

I hope that i got this right - I have a function prece that creates a reference to subcategory table. Correct me if im wrong (preces::class,'apakskategorijas_id') - this piece of code shows that i use preces class where i have reference to this subcategory class through column 'apakskategorijas_id' which is referenced from goods table to subcategory table.

And here is class for my goods

class preces extends Model
{
    public function apakskategorija()
    {
        $this->belongsTo('apakskategorijas');
    }
}

Here i tell, that this class belongs to subcategory table because of the reference, so that it would know where i comes from and so on.

Now comes the part that i struggle with the most, the usage of previous information, how to use it to to get each good from goods table.

I have a route for my controller, that will get id of subcategory and with help of relation will return all goods.

Route::get('kategorija/apakskategorija/preces/{id}','routes@preces');

And here is the controller

public function preces($id)
    {
        $apakskat = apakskategorijas::with('prece')->get();

        $precess = preces::where('apakskategorija_id',$id)->get();

        return view ('prece.preces',compact('apakskat','precess'));
    }

And here i already know that the code is so wrong that its hard to look at. I want to understand how can i now work with previously made information to return goods in my view. As far as i understand, i need to make a variable that references a function where i made my hasMany relation. But in that function i told that i would use foreign key to subcategory table. And that is probably how i get the subcategory id that i need to return goods from hat specific subcategory. And now i have no clue how to work further. How do i use $id and everything else that i need? It shows me error that variables that im trying to pass to view are undefined.

Here is my view

@foreach($apakskat as $prec)

    <div class="col-md-4 kat">

        <img src="{{ URL::to($prec->path) }}">        
        <p>{{$prec->nosaukums}}</p>
        <a href="{{ url('kategorija/apakskategorija/prece'.$prec->id) }}"><p>{{$prec->cena}}</p></a>

    </div>

    @endforeach

Any help will be appreciated. Thank you

In Your Model

class apakskategorijas extends Model
{
    public function kategorija()
    {
       return $this->belongsTo('kategorijas');
    }
    public function prece()
    {
        return $this->hasMany(preces::class);
    }
}

In Your Controller

public function preces($id)
{
    $apakskat = apakskategorijas::where('id',$id)->with('prece')->get();

    return view ('prece.preces',compact('apakskat'));
}

This will return a subcategory of the $id with all goods that belong to subcategory $id.

In Your View

@foreach($apakskat->prece as $prec) //this loops for foods.

<div class="col-md-4 kat">

    <img src="{{ URL::to($prec->path) }}">        
    <p>{{$prec->nosaukums}}</p>
    <a href="{{ url('kategorija/apakskategorija/prece'.$prec->id) }}"><p>
    {{$prec->cena}}</p></a>

</div>

@endforeach

I hope this will work :)

在此处输入图片说明

When i did {{dd($apakskat->toArray())}} it showed me subcategory and its item, prece. But still says that Property [prece] does not exist on this collection instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM