Slug, slug, slug has slugged me.
I re-watched the below video, and have re-implemented this in the AppServiceProvider:
https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/13
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
View::share('job', Job::all());
}
It's giving an error here when doing a payment post, e.g.
select * from `jobs` where `slug` = '22' limit 1
or when switching:
select * from `jobs` where `bidded` = 0 and `jobs`.`id` = 'algolia-test' limit 1
I tried changing the calls from $job->id to $job->slug however it still gives errors such as:
Method Illuminate\Database\Eloquent\Collection::bidReserves does not exist.
//Job.php model
/**
* Get a string path for the job
*
* @return string
*/
public function path()
{
// return "/jobs/{$this->id}";
return "/jobs/{$this->slug}";
}
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName(): string
{
return 'slug';
}
If getRoutKeyName() is uncommented then the slug doesn't work.
I also added, as you mentioned, this to the JobsController
$job = Job::where('id', $job->id)
->orWhere('slug', $job->slug)
->firstOrFail();
Below is a payment store function which attaches an order to the sale. One thing about slugs is that I didn't know to start using them immediately. A ton of logic is based on the $job->id.
Adding more difficulty, this app uses the Spatie Media-Library which bases the routing of media objects to the id. However, if in the browser a slug is present the media shows. So, I'm not sure if this is based on the View::share(). However, if you do a browser request such as bidbird.test/jobs/3 a 404 is returned. so I question if View::share() is working properly.
// BidReservesController.php
/**
* Store a newly created resource in storage.
*
* @param $job
* @return Response
* @throws ValidationException
*/
public function store(Job $job)
{
$job = Job::Incomplete()
// ->where('id', $job->id) // added line - may be removed
// ->orWhere('slug', $job->slug) // added line - may be removed
->findOrFail($job);
It seems mostly to be a path() issue. I had even tried:
//Job.php model
/**
* Get a string path for the job
*
* @return string
*/
public function path()
{
if ($this->id == 'job_id') {
return "/jobs/{$this->id}";
}
elseif ($this->slug == 'job_slug'){
return "/jobs/{$this->slug}";
}
//if ('job_id' == $this->id) {
// return "/jobs/{$this->id}";
//}
//elseif ('job_slug' == $this->slug){
// return "/jobs/{$this->slug}";
//}
}
but it was causing errors.
Do you have any more ideas? @bugsysha