gpt4 book ai didi

php - laravel 模型获取具有自定义值的数据

转载 作者:行者123 更新时间:2023-11-30 21:55:17 27 4
gpt4 key购买 nike

我对 laravel 有疑问。我创建了名为 Zlecenia 的模型,需要使用自定义变量获取数据。我需要向该模型添加一个 url 值。

我的 Controller

$zlecenia = new Zlecenia();
$q = $zlecenia->with('users')->withLinks()
->orderBy('zlecenia.id', 'desc')
->where('active', 1)
->where('end',0)
->where('kategorie', '>', 0)
->take(5)
->get();

我的模型

public function withLinks($id,$slug){
return route('ZleceniaZlecenie',['id'=>$id,'slug'=>$slug]);
}

我如何获取数据,其获取后的 View 如下:

{
"id": 21,
"created_at": "2017-08-01 08:51:08",
"updated_at": "2017-08-01 08:51:08",
"name": "fasd asdf",
"active": 1,
"user_id": 3,
"description": "asd fasdf",
"winner_id": null,
"end": 0,
"kategorie": 4,
"link": "slugValue",
"users": {
"id": 3,
"name": "test",
"email": "kontakt@teamwant.pl",
},
"url": "http://127.0.0.1:8000/test/3/slugValue"
},

如果你看到我添加了一个“url”值,我需要从函数 withLinks() 生成这个值。

插入 withLinks == 来自模型/数据库的链接值

id in withLinks == id value from model/db

最佳答案

首先,在 Controller 中进行事件记录查询是一种不好的做法,如果我理解正确的问题,你需要在你的 View 中路径两个模型(模型数组),首先:

public static function getSomeModel() {

return self::with('users')
->orderBy('zlecenia.id', 'desc')
->where('active', 1)
->where('end',0)
->where('kategorie', '>', 0)->take(5)->get();

}

第二个 - 网址

Controller 看起来像这样:

public function getSomeRoute() {

$input = Input::all();

isset($input["id"]) $id = $input["id"];
isset($input["slug"]) $slug = $input["slug"];

$models = ["SomeModel" => SomeModel::getSomeModel(), "url" => "http://example.com/".$id."/".$slug];

return view("view", ["models" => $models]);

}

关于php - laravel 模型获取具有自定义值的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45479160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com