Я делаю контроллер ресурса. Вот мой код
api.php:
Route::group([ 'prefix' => 'admin', 'namespace' => 'Admin'], function () {
Route::resource('penyedia', 'PenyediaController');
});
Ниже приведено содержимое контроллера 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()
]);
}
}
Это содержимое 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);
}
}
Затем, когда я открываю cmd, я набираю php artisan route: list
внезапно к параметрам роутера добавилось «гм». Как это может быть так? Вот скриншот:
https://i.imgur.com/kZLUiGm.png