dongzongzi0379 2019-04-30 05:12
浏览 88

我做的路线突然添加了“嗯”

I am making a resource controller. Here's my code. api.php:

Route::group([ 'prefix' => 'admin', 'namespace' => 'Admin'], function () {


Route::resource('penyedia', 'PenyediaController');

});

Following is the contents of the controller PenyediaController.php:

<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\InvalidRouteException;
use App\Http\Requests\ValidatePenyedia;
use App\Http\Requests\ValidateList;
use App\Models\Penyedia;
use App\Http\Controllers\Controller;

class PenyediaController extends Controller
{
/**
 * Penyedia model.
 *
 * @var Penyedia
 */
private $penyedia;

/**
 * PenyediaController constructor.
 *
 * @param Penyedia $penyedia
 */
public function __construct(Penyedia $penyedia)
{
    $this->penyedia = $penyedia;
}

/**
 * Display a listing of the Penyedia.
 *
 * @param ValidateList $request
 * @return \Illuminate\Http\Response
 */
public function index(ValidateList $request)
{
    $validated = $request->validated();

    $filter = array_key_exists('filter', $validated) ? $validated['filter'] : [];
    $keyword = array_key_exists('keyword', $validated) ? $validated['keyword'] : null;

    $datatable = array_key_exists('per_page', $validated) ?
        $this->penyedia->datatable($filter, $keyword)->paginate($validated['per_page'])
        :
        $this->penyedia->datatable($filter, $keyword)->get();

    return response()->json([
        'error' => false,
        'data' => $datatable
    ]);
}

/**
 * Show the form for creating a new Penyedia.
 *
 * @return void
 * @throws InvalidRouteException
 */
public function create()
{
    throw new InvalidRouteException();
}

/**
 * Store a newly created Penyedia in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(ValidatePenyedia $request)
{
    $validated = $request->validated();

    $this->penyedia = $this->penyedia->create($this->penyedia->prepareData($validated));

    return response()->json([
        'error' => false,
        'data' => $this->penyedia->output()
    ]);
}

/**
 * Display the specified Penyedia.
 *
 * @param int $id
 * @return void
 * @throws InvalidRouteException
 */
public function show(Penyedia $penyedia)
{
    //dd('z');
    dd($penyedia->nama);
    throw new InvalidRouteException();
}

/**
 * Show the form for editing the specified Penyedia.
 *
 * @param int $id
 * @return void
 * @throws InvalidRouteException
 */
public function edit($id)
{
    throw new InvalidRouteException();
}

/**
 * Update the specified Penyedia in storage.
 *
 * @param ValidatePenyedia $request
 * @param Penyedia $penyedia
 * @return \Illuminate\Http\Response
 */
public function update(Penyedia $penyedia)
{
    dd($penyedia->id);
    //dd("z");
    $validated = $request->validated();
    //dd("hehe");
    dd($penyedia);
    $penyedia->update($this->penyedia->prepareData($validated));
    dd($this->penyedia->prepareData($validated));
    return response()->json([
        'error' => false,
        'data' => $penyedia->output()
    ]);
}

/**
 * Remove the specified Penyedia from storage.
 *
 * @param Penyedia $penyedia
 * @return \Illuminate\Http\Response
 * @throws \Exception
 */
public function destroy(Penyedia $penyedia)
{
    $penyedia->delete();

    return response()->json([
        'error' => false,
        'data' => $penyedia->output()
    ]);
}
}

This is the contents of Penyedia.php:

<?php

namespace App\Models;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Penyedia extends Model
{
use SoftDeletes;

protected $table = 'penyedia';


protected $fillable = [
    'nama', 'penanggung_jawab', 'deskripsi', 'category', 'user_id', 'kontak'
];

protected $dates = ['deleted_at'];

/**
 * Relation to User.
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */

public function scopeDatatable($query, $filters = [], $keyword = null){
   // dd("sampesini");
    foreach ($filters as $filter) {
        $query->orderBy($filter['field'], $filter ['order']);
    }

    if ($keyword) {
        $query->where('nama', 'LIKE', '%' . $keyword . '%')
              ->orWhere('deskripsi', 'LIKE', '%' . $keyword . '%')
              ->orWhere('penanggung_jawab', 'LIKE', '%' . $keyword . '%')
              ->orWhere('kontak', 'LIKE', '%' . $keyword . '%');
    }

    //return $query->select('id', 'nama');
     return $query->with(['user:id,name'])->select('id', 'nama', 'penanggung_jawab', 'deskripsi', 'category', 'user_id', 'kontak');
}

public function prepareData($data)
{
    return [
        'nama' => $data['nama'] ? $data['nama'] : '',
        'penanggung_jawab' => isset($data['penanggung_jawab']) ? $data['penanggung_jawab'] : '',
        'deskripsi' => isset($data['deskripsi']) ? $data['deskripsi'] : '',
        'category' => isset($data['category']) ? $data['category'] : '',
        'user_id' => isset($data['user_id']) ? $data['user_id'] : '',
        'kontak' => isset($data['kontak']) ? $data['kontak'] : '',
    ];
}

public function output()
{
    return $this->with(['user:id,name'])->select('id', 'nama', 'penanggung_jawab', 'deskripsi', 'category', 'user_id', 'kontak')->first();
}
public function user()
{
    return $this->belongsTo(User::class);
}

/**
 * Relation to Beasiswa.
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function beasiswa()
{
    return $this->hasMany(Beasiswa::class);
}



}

Then when I open cmd then I type php artisan route: list suddenly there was an addition of 'um' to the router's parameters. How can it be like that ? Here's the screenshot:

https://i.imgur.com/kZLUiGm.png

  • 写回答

1条回答 默认 最新

  • dqmdlo9674 2019-04-30 07:17
    关注

    I found solution, I just edit my route like this :

    Route::resource('penyedia', 'PenyediaController', [
        'parameters' => ['penyedia' => 'penyedia']
    ]);
    

    And my parameter is back to 'penyedia'

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害