Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在url中显示名称而不是id?_Php_Laravel 5.5 - Fatal编程技术网

Php 在url中显示名称而不是id?

Php 在url中显示名称而不是id?,php,laravel-5.5,Php,Laravel 5.5,我正在Laravel中构建一个小应用程序,但我需要一些帮助。我读过类似的任务,但我没有看到它们与我所拥有的相关 我正在构建的应用程序是一个字典,我真的不知道在URL中通过id显示单词的部分。我试图还原curds(存储、更新、删除、显示)的逻辑,以便它们使用单词名称而不是单词id执行操作。这很有效。在操作之后重定向到routeapp/{name}也起作用 现在,我想这不是应该做的,当凝乳没有在id上采取行动的时候 我也读过关于slug的文章,但是因为我正在编一本字典,我不明白为什么我需要用一个“-

我正在Laravel中构建一个小应用程序,但我需要一些帮助。我读过类似的任务,但我没有看到它们与我所拥有的相关

我正在构建的应用程序是一个字典,我真的不知道在URL中通过id显示单词的部分。我试图还原curds(存储、更新、删除、显示)的逻辑,以便它们使用单词名称而不是单词id执行操作。这很有效。在操作之后重定向到routeapp/{name}也起作用

现在,我想这不是应该做的,当凝乳没有在id上采取行动的时候

我也读过关于slug的文章,但是因为我正在编一本字典,我不明白为什么我需要用一个“-”来分隔这个单词?这是一个单词,不像帖子。既然slug必须等于名称,为什么我的表单中需要为slug添加一个额外的字段?我发现一个错误:slug->unique()字段不能为null

如果有人有时间,请向我解释在URL中显示单词的名称的最佳方法。谢谢,我很感激

编辑

以下是我的更新crud代码:

public function update(UpdateWordRequest $request, $name)
{
    Word::query()->where('name', $name)->firstOrFail()->update($request->input());
    return redirect()->route('dictionary.show', ['name' => $name])->with('success', 'Edited successfully!');
}
它工作正常,但当我重新编辑单词并将其更改为带有数字的word2时,我会出现以下错误:试图获取非对象的属性“name”


我需要如何理解它?谢谢你:)

我认为你应该能够利用一种特质来解决这个问题

我将创建一个trait调用hasSlug,并具有以下代码:

<?php

namespace App\Traits;

trait HasSlug
{

protected static function bootHasSlug()
{
    static::creating(function ($model) {

        $columnName = static::getSlugColumn();

        $model->$columnName = str_slug($model->name); //convert your name to slug
    });
}


public function getSlugAttribute()
{
    $columnName = static::getSlugColumn();

    return (string) $this->attributes[$columnName];
}

protected static function getSlugColumn()
{
    if (isset(static::$slugColumn)) {
        return static::$slugColumn;
    }

    return 'slug';
}
}

我认为您应该能够使用trait来解决这个问题

我将创建一个trait调用hasSlug,并具有以下代码:

<?php

namespace App\Traits;

trait HasSlug
{

protected static function bootHasSlug()
{
    static::creating(function ($model) {

        $columnName = static::getSlugColumn();

        $model->$columnName = str_slug($model->name); //convert your name to slug
    });
}


public function getSlugAttribute()
{
    $columnName = static::getSlugColumn();

    return (string) $this->attributes[$columnName];
}

protected static function getSlugColumn()
{
    if (isset(static::$slugColumn)) {
        return static::$slugColumn;
    }

    return 'slug';
}
}

这是我最后想到的

Word.php

<?php

namespace App\Http\Controllers;

use App\Word;
use App\Http\Requests\CreateWordRequest;
use App\Http\Requests\UpdateWordRequest;

class WordController extends Controller
{

    /**
     * Store a newly created resource in storage.
     * @param CreateWordRequest $request
     * @return mixed
     */
    public function store(CreateWordRequest $request)
    {
        $word = Word::query()
            ->create($request->input());
        return redirect()
            ->route('dictionary.show', $word->slug)
            ->with('success', 'Created successfully!');
    }

    /**
     * Display the specified resource.
     * @param $slug
     * @return mixed
     */
    public function show($slug)
    {
        $word = Word::whereSlug($slug)
            ->firstOrFail();
        return view('word.show')
            ->with('word', $word);
    }

    /**
    * Update the specified resource in storage.
    * @param UpdateWordRequest $request
    * @param $slug
    * @return mixed
    */
    public function update(UpdateWordRequest $request, $slug)
    {
        Word::whereSlug($slug)
            ->firstOrFail()
            ->update($request->input());
        return redirect()
            ->route('dictionary.show', $slug)
            ->with('success', 'Edited successfylly!');
    }

    /**
    * Remove the specified resource from storage.
    * @param $slug
    * @return mixed
    */
    public function destroy($slug)
    {
        Word::whereSlug($slug)
            ->delete();
        return redirect()
            ->route('dictionary.index')
            ->with('success', 'Deleted successfully');
    }
}
Route::get('/dictionary/{slug}', 'WordController@show')
    ->name('dictionary.show')
    ->where('slug', '[-A-Za-z0-9_-]+');
我使用了@usrnotfind

创建、更新、销毁新单词效果良好。最终还是有一个小问题。当我想更新某个单词并更改其名称时,保存该单词后,我会

很抱歉,找不到您要查找的页面


如果我查看url,我会看到旧名称而不是新名称这与我的更新方法有关,我如何修复它?

这是我最后想到的

Word.php

<?php

namespace App\Http\Controllers;

use App\Word;
use App\Http\Requests\CreateWordRequest;
use App\Http\Requests\UpdateWordRequest;

class WordController extends Controller
{

    /**
     * Store a newly created resource in storage.
     * @param CreateWordRequest $request
     * @return mixed
     */
    public function store(CreateWordRequest $request)
    {
        $word = Word::query()
            ->create($request->input());
        return redirect()
            ->route('dictionary.show', $word->slug)
            ->with('success', 'Created successfully!');
    }

    /**
     * Display the specified resource.
     * @param $slug
     * @return mixed
     */
    public function show($slug)
    {
        $word = Word::whereSlug($slug)
            ->firstOrFail();
        return view('word.show')
            ->with('word', $word);
    }

    /**
    * Update the specified resource in storage.
    * @param UpdateWordRequest $request
    * @param $slug
    * @return mixed
    */
    public function update(UpdateWordRequest $request, $slug)
    {
        Word::whereSlug($slug)
            ->firstOrFail()
            ->update($request->input());
        return redirect()
            ->route('dictionary.show', $slug)
            ->with('success', 'Edited successfylly!');
    }

    /**
    * Remove the specified resource from storage.
    * @param $slug
    * @return mixed
    */
    public function destroy($slug)
    {
        Word::whereSlug($slug)
            ->delete();
        return redirect()
            ->route('dictionary.index')
            ->with('success', 'Deleted successfully');
    }
}
Route::get('/dictionary/{slug}', 'WordController@show')
    ->name('dictionary.show')
    ->where('slug', '[-A-Za-z0-9_-]+');
我使用了@usrnotfind

创建、更新、销毁新单词效果良好。最终还是有一个小问题。当我想更新某个单词并更改其名称时,保存该单词后,我会

很抱歉,找不到您要查找的页面



如果我查看url,我会看到旧名称而不是新名称这与我的更新方法有关,我如何修复它?

如果你所有的“名称”都是唯一的,那么使用名称作为ID没有什么错,它们确实是唯一的,但是当我添加例如word1和word2时,我得到一个错误:尝试获取非对象的属性。没有代码很难判断;)给我们看一些代码我一会儿会给你提供我的代码。我正在尝试修复此错误:App\Word::language必须返回关系实例。我不知道问题出在哪里。它在几个小时前确实起作用。如果您所有的“名称”都是唯一的,那么将名称用作ID没有什么错。它们确实是唯一的,但是当我添加例如word1和word2时,我得到一个错误:尝试获取非object的属性。没有代码很难判断;)给我们看一些代码我一会儿会给你提供我的代码。我正在尝试修复此错误:App\Word::language必须返回关系实例。我不知道问题出在哪里。几个小时前它还有用。好吧,但是积垢呢。我是否需要类似“
Word::query()->where('slug',$slug)->firstOrFail()…
;或仅
first()
”之类的内容。再说一次,迁移又如何呢。是否需要一个必须唯一的slug字符串(),是否需要为slug字段添加一个表单?非常感谢。首先添加一个迁移来创建slug字段。不,您不必在表单中有一个slug字段,因为您可以看到它将接受您的name字段并为其创建slug,是的,您可以执行Word::query()。。。。。好吧,看起来没那么难。我将在我的phpstorm中尝试一下,看看它是否有效。另外,我假设变量
$model
就是我在show视图中所看到的,比如
$words as$word
?非常感谢。模型就是您将要使用的。例如,如果您在Word.php中使用HasSlug,那么如果其他内容是其他内容,那么模型将是$Word
字段“slug”没有默认值。它也必须是
nullable()
吗?好的,但是积垢呢。我是否需要类似“
Word::query()->where('slug',$slug)->firstOrFail()…
;或仅
first()
”之类的内容。再说一次,迁移又如何呢。是否需要一个必须唯一的slug字符串(),是否需要为slug字段添加一个表单?非常感谢。首先添加一个迁移来创建slug字段。不,您不必在表单中有一个slug字段,因为您可以看到它将接受您的name字段并为其创建slug,是的,您可以执行Word::query()。。。。。好吧,看起来没那么难。我将在我的phpstorm中尝试一下,看看它是否有效。另外,我假设变量
$model
就是我在show视图中所看到的,比如
$words as$word
?非常感谢。模型就是您将要使用的。例如,如果您在Word.php中使用HasSlug,那么如果其他内容是其他内容,那么模型将是$Word
字段“slug”没有默认值。它是否也需要是
nullable()