0

It's quite peculiar to show this "behavior", since i'm done this before, nonetheless here it is: I'm trying to show specific record from db in Laravel.

Here is my code so far:

Job model:

<?php

namespace App;

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

class Job extends Model
{
    protected $fillable = [
        "title", "description", "email"
    ];

    public function user(){
        return $this->belongsTo("App\User","user_id");
    }

}

User model:

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Role;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',"role_id"
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function role(){
        return $this->belongsTo("App\Role", "role_id");
    }

    public function job(){
        return $this->hasMany("App\Job");
    }

And show method in JobController:

public function show(Job $job)
    {
        $jobx = Job::find(4)->get();
        dd($jobx);
        return view("jobs.show")->with(compact("jobx"));
    }

Argument in show method is array, but should be a number(id). But i can see clearly in the url a number. Where its getting this array and why? Also how to rectify for show method so it can get an id?

Also php artisan route:list shows job instead id as parameter in route:

-----------------------------+--------------+
|        | GET|HEAD  | /                      |                  | Closure
                             | web          |
|        | GET|HEAD  | api/user               |                  | Closure
                             | api,auth:api |
|        | GET|HEAD  | dashboard              | dashboard        | App\Http\Controllers\DashboardController@index                         | web,auth     |
|        | POST      | jobs                   | jobs.store       | App\Http\Controllers\JobController@store
                             | web,auth     |
|        | GET|HEAD  | jobs                   | jobs.index       | App\Http\Controllers\JobController@index
                             | web          |
|        | GET|HEAD  | jobs/create            | jobs.create      | App\Http\Controllers\JobController@create
                             | web,auth     |
|        | PUT|PATCH | jobs/{job}             | jobs.update      | App\Http\Controllers\JobController@update
                             | web,auth     |
|        | GET|HEAD  | jobs/{job}             | jobs.show        | App\Http\Controllers\JobController@show
                             | web          |
|        | DELETE    | jobs/{job}             | jobs.destroy     | App\Http\Controllers\JobController@destroy                             | web,auth     |
|        | GET|HEAD  | jobs/{job}/edit        | jobs.edit        | App\Http\Controllers\JobController@edit
                             | web,auth     |
|        | POST      | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | GET|HEAD  | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST      | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | GET|HEAD  | password/confirm       | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    | web,auth     |
|        | POST      | password/confirm       |                  | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            | web,auth     |
|        | POST      | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web          |
|        | GET|HEAD  | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web          |
|        | POST      | password/reset         | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web          |
|        | GET|HEAD  | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web          |
|        | POST      | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
|        | GET|HEAD  | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
+--------+-----------+------------------------+------------------+-------------------------------------------
2
  • 1
    refer laravel route model binding. Laravel automatically inject the model instances directly into your routes. You don't need to fetch from db, laravel does it for you. Please note that Job $job is used as the argument. If you need to manually fetch from db use $id as argument. Commented Nov 8, 2019 at 17:32
  • your method should be public function show(Job $job) { return view("jobs.show")->with(compact("job")); } Commented Nov 8, 2019 at 17:33

2 Answers 2

2

You don't need to use get() if you already used find().

$jobx = Job::find(4);
Sign up to request clarification or add additional context in comments.

3 Comments

True, and garners an upvote from me. That said, in OP's specific code, $job is already the job from the ID in the URL, anyways, due to route model binding. So even the Job::find call is unnecessary.
You're correct. I didn't really understand his question. I just saw that query and assumed he was asking about the unexpected result.
Thanks. I'm losing my eyesight and what is embarrassing is that i done this before and currently looking in my old project with correct syntax and not seeing a error in my current one. Should just copied code and change names of variables ...
2

When you type-hint variable names as you did in your example code, Laravel automatically resolves the model instance for you using what it calls "Route Model Binding". So all you need to do is to pass the resolved model to the view.

public function show(Job $job)
{
    return view('jobs.show', compact($job));
}

If you, however, want to fetch the record from the DB yourself, do the following without type-hinting the variable:

public function show($id)
{
    $job = Job::find($id); // or even better ::findOrFail($id);

    return view('jobs.show', compact($job));
}

Docs: https://laravel.com/docs/6.x/routing#route-model-binding

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.