Routing issue laravel 5.1 - php

Hi guys i am not able to see what am doing wrong even after doing research. Please assist. i am getting a NotFoundHttpException in RouteCollection.php line 161: I am trying to access the getShow method or rather the stove.show route
public function getIndex()
{
$stoves = Stove::all();
return view('stoves.index');
}
public function anyData()
{
$stoves = Stove::select(['id','stoveno', 'refno', 'manufactuerdate', 'created_at']);
return Datatables::of($stoves)
->addColumn('action', function ($stoves) {
return '<i class="glyphicon glyphicon-edit"></i> History';
})
->make(true);
}
public function addData()
{
//
return view('stoves.new');
}
public function store(AddStove $request)
{
Stove::create($request->all());
return redirect('stove');
}
public function getShow($id)
{
$stove = Stove::findorFail('$id');
return view('stoves.view', compact('stove'));
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
My route looks like
Route::controller('stove', 'StoveController', [
'anyData' => 'stove.data',
'getShow' => 'stove.show',
'getIndex' => 'stove',
]);
Route::get('newstove', 'StoveController#addData');
Route::post('newstove', 'StoveController#store');
my view folder contains index.blade, new.blade and finally view.blade
Thanks i appreciate

Try using this:
Route::get('getShow/{id}','stoveController#getShow');
If problem exist, remove this line
'getShow' => 'stove.show',

Related

Laravel policy send 403 for update and 201 for create

I have this policy :
class ProjectPagePolicy
{
use HandlesAuthorization;
public function viewAny(User $user)
{
return true;
}
public function view(User $user, ProjectPage $projectPage)
{
return true;
}
public function create(User $user)
{
return $user->isAdmin() || $user->isDeveloper();
}
public function update(User $user, ProjectPage $projectPage)
{
return $user->isAdmin() || $user->isDeveloper();
}
..........
}
ProjectPageController :
class ProjectPageController extends Controller
{
public function __construct()
{
$this->authorizeResource(ProjectPage::class, 'project-page');
}
public function index(Request $request)
{
return response(
[],
HttpStatusCode::OK
);
}
public function store(ProjectPageRequest $projectPageRequest)
{
$inputs = $projectPageRequest->validated();
$projectPage = ProjectPage::create($inputs);
return response()->json([
'data' => new ProjectPageResource($projectPage)
], HttpStatusCode::Created);
}
public function update(
ProjectPageRequest $projectPageRequest,
ProjectPage $projectPage
) {
$inputs = $projectPageRequest->validated();
$projectPage->fill($inputs)->save();
return response(status: HttpStatusCode::NoContent);
}
In the routes file :
Route::middleware(['auth:sanctum'])->group(function () {
Route::post(
'/refresh-token',
fn () => app(RefreshTokenResponse::class)
);
Route::apiResources([
'project-page' => ProjectPageController::class,
]);
});
When I try to save a project page, I received 201 CREATED so all good in this case.
When I try to update it I have 403 forbidden.
Where the problem is ? Why is working on creation and not on updated ? Have an idea about that ?
TL;DR Remove the second argument ('project-page') from your authorizeResource call.
When using Route::resource(...), Laravel will convert a hyphenated route parameter to be snake-case (this will not have any affect on the URL itself, just how laravel accesses the parameter). This will mean that that when you call authorizeResource with project-page, it won't match. This will in-turn cause the authorize method to fail.
You can view your routes via the CLI with the following:
php artisan route:list
which should show your route param for your project-page routes to be project_page e.g. project-page/{project_page}

Attempt to Read Property on String Error Codeigniter

I am trying to display data through a query on Codeigniter, but return an error
"ATTEMPT TO READ PROPERTY "JENIS_PRODUK" ON STRING"
Here's my code on controller :
public function minimalist($id = 'minimalist')
{
$data["minimalis"] = $this->welcome_model->getMinimalis($id);
$this->load->view('frontend/minimalis.php', $data);
}
My code on Model :
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->row();
}
On view I added foreach to display those data
Can you tell me what's wrong?
Please change this
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->row();
}
To
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->result();
}

How resolve BadMethodCallException, Call to undefined method Illuminate\Database\Query\Builder::filter()

I'm trying to filter the results from eloquent query, but appear the next error BadMethodCallException. According to me, I'm doing everything right.
I'm using Laravel 5.4
The error details:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::filter()
in Builder.php line 2445
at Builder->__call('filter', array(null))
in Builder.php line 1254
at Builder->__call('filter', array(null))
in web.php line 459
at Router->{closure}()
in Route.php line 189
at Route->runCallable()
in Route.php line 163
at Route->run()
in Router.php line 572
I have the next code:
public function index(SearchRequest $searchRequest, ConfigurationFilter $filters)
{
$filtered_configurations = Configuration::whereTrash(false)->with(['customs.properties', 'properties'])->filter($filters);
$types = $this->getConfigurationTypes();
$authors = $this->getAuthors();
return view('configuration.assistant.index', [
'configurations' => $filtered_configurations->paginate(10),
'authors' => $authors,
'types' => $types,
]);
}
Where SearchRequest is:
class SearchRequest extends FormRequest {
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return Auth::user()->author != null;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
];
}
}
Where ConfigurationFilter is:
class ConfigurationFilter extends ModelFilter
{
public function name($value)
{
return $this->builder->where('name', 'like', "%{$value}%");
}
public function author($id)
{
return $this->builder->whereIn('user_id', explode(',', $id));
}
public function type($type)
{
return $this->builder->whereIn('category_id', explode(',', $type));
}
public function status($status)
{
return $this->builder->whereEnabled($status == 'enabled');
}
}
Where ModelFilter is:
class ModelFilter extends QueryFilter implements Filter
{
public function id($id)
{
return $this->builder->whereIn('id', explode(',', $id));
}
public function from($date)
{
return $this->builder->where('created_at', '>=', $date);
}
public function to($date)
{
return $this->builder->where('created_at', '<=', $date);
}
public function enabled($status)
{
return $this->builder->whereEnabled($status === 'true');
}
public function trash($status)
{
return $this->builder->whereTrash($status === 'true');
}
public function take($limit = 100)
{
return $this->builder->take($limit);
}
}
Where Filter is:
interface Filter {
public function id($id);
public function from($date);
public function to($date);
public function enabled($status);
public function trash($status);
public function take($limit = 100);
}
What will I be missing?
Thanks in advance
I already resolved it, I added the next function to model:
public function scopeFilter($query, QueryFilter $filters)
{
return $filters->apply($query);
}

Trying to get property 'title' of non-object (View: /opt/lampp/htdocs/commonroom/resources/views/home.blade.php

App\Classes
public function enroll()
{
return $this->hasMany(Enrolls::class,'cid');
}
App\Enrolls
public function classes()
{
return $this->belongsTo(Classes::class);
}
controller
public function index()
{
$enrolls = Enrolls::all();
return view('home')->with('enrolls', $enrolls);
}
blade
{{$enroll->classes->title}}
I was trying to get data from enrolls table. this contains two foreign Keys.
SCREENSHOT
You should try this:
public function index()
{
$enrolls = Enrolls::with('classes')->get();
return view('home',compact('enrolls'));
}

In Laravel how can I replace a controller/method/id with just /slug in the URL?

What is the best way I could replace /controller/method/id in the URL with just /slug?
For example: trips/1 would become /honduras-trip
This is what I am doing but couldn't their be a better way?
My routes:
Route::get('/{slug}', 'HomeController#show');
My Controller:
public function show($slug)
{
$class = Slug::where('name', '=', $slug)->firstOrFail();
if($class->slugable_type == 'Trip')
{
$trip = Trip::find($class->slugable_id);
return $trip;
}
if($class->slugable_type == 'Project')
{
$project = Project::find($class->slugable_id);
return $project;
}
if($class->slugable_type == 'User')
{
$user = User::find($class->slugable_id);
return $user;
}
}
My Slug Model:
class Slug extends Eloquent {
public function slugable()
{
return $this->morphTo();
}
}
The other models all have this method:
public function slugs()
{
return $this->morphMany('Slug', 'slugable');
}
In your routes.php just give
Route::get('slug', array('uses' => 'HomeController#show'));
In your controller, write show() function
public function show() {
return View::make('welcome');
}
In your view give,
<li>slug</li>

Categories