简体   繁体   English

了解laravel关系及其用法

[英]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. 我在laravel中的关系以及如何使用它们稍后从那些数据库表中获取信息方面遇到了一些挣扎。

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. 因此,我有两个表用于获取在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. 如您所见,我从子类别表中引用了ID字段。 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. 我希望我做对了-我有一个函数prece,该函数创建对子类别表的引用。 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. 如果我错了,请纠正我(preces::class,'apakskategorijas_id') -这段代码显示我使用preces类,其中我通过从商品表到子类别表引用的列'apakskategorijas_id'引用了此子类别。

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. 我为我的控制器设置了一条路线,该路线将获得子类别的ID,并且在关联的帮助下将退还所有货物。

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. 据我了解,我需要做一个变量,该变量引用我建立了hasMany关系的函数。 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. 这可能就是我要从帽子特定子类别退货所需的子类别ID的获取方式。 And now i have no clue how to work further. 现在我不知道如何进一步工作。 How do i use $id and everything else that i need? 如何使用$id和我需要的其他所有内容? 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. 这将返回$ id的子类别,其中包含属于$ 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. 当我执行{{dd($apakskat->toArray())}}它向我显示了子类别及其项,然后。 But still says that Property [prece] does not exist on this collection instance. 但是仍然说Property [prece] does not exist on this collection instance.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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