0

stuck on collection array

I am getting the specfic array. but can't loop throught in blade

this is my controller:

public function appliedJob($id)
{

   $apllied = AplliedJob::with('user','job')->find($id);


    return view('dashboardviews.page.apllied-job',compact('apllied'));
 }

this my model:

   class AplliedJob extends Model
   {

   use Notifiable;
   protected $fillable = [
    'name', 'email', 'message','aplied_cv',
   ];
   protected  $table='appllied_jobs';
   //protected $primaryKey = 'user_id';

   protected $primaryKey = 'user_id';

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

   return $this->belongsTo('App\Job','job_id');
   }

   }

this the array I am getting I want to access relation# [job] but it throws an error

   AplliedJob {#257 ▼
   #fillable: array:4 [▶]
   #table: "appllied_jobs"
   #primaryKey: "user_id"
   #connection: "mysql"
   #keyType: "int"
   +incrementing: true
   #with: []
   #withCount: []
   #perPage: 15
   +exists: true
   +wasRecentlyCreated: false
   #attributes: array:9 [▶]
   #original: array:9 [▶]
   #changes: []
   #casts: []
   #dates: []
   #dateFormat: null
   #appends: []
   #dispatchesEvents: []
   #observables: []
   #relations: array:2 [▼
   "user" => User {#263 ▼
   #fillable: array:4 [▶]
   #table: "users"
   #hidden: array:2 [▶]
   #casts: array:1 [▶]
   #connection: "mysql"
   #primaryKey: "id"
   #keyType: "int"
   +incrementing: true
   #with: []
   #withCount: []
   #perPage: 15
   +exists: true
   +wasRecentlyCreated: false
   #attributes: array:9 [▶]
   #original: array:9 [▶]
   #changes: []
   #dates: []
   #dateFormat: null
   #appends: []
   #dispatchesEvents: []
   #observables: []
   #relations: []
   #touches: []
   +timestamps: true
   #visible: []
   #guarded: array:1 [▶]
   #rememberTokenName: "remember_token"
   }
   "job" => Job {#261 ▼
   #fillable: array:4 [▶]
   #table: "jobs"
   #connection: "mysql"
   #primaryKey: "id"
   #keyType: "int"
   +incrementing: true
   #with: []
   #withCount: []
   #perPage: 15
   +exists: true
   +wasRecentlyCreated: false
   #attributes: array:10 [▼
    "id" => 1
    "user_id" => 2
    "jb_name" => "web developer"
    "jb_type" => "software"
    "jb_salary" => "12000"
    "jb_exp" => "12"
    "jb_description" => "djghnbjkguyfgykgvkvbuykgbkub g uiygjghpiu p;"
    "jb_loc" => "lahore"
    "created_at" => "2019-05-15 01:18:46"
    "updated_at" => "2019-05-15 01:18:46"
     ]
     #original: array:10 [▶]
     #changes: []
     #casts: []
     #dates: []
     #dateFormat: null
     #appends: []
     #dispatchesEvents: []
     #observables: []
     #relations: []
     #touches: []
     +timestamps: true
     #hidden: []
     #visible: []
     #guarded: array:1 [▶]
      }
     ]
     #touches: []
     +timestamps: true
     #hidden: []
     #visible: []
     #guarded: array:1 [▶]
      }

i have tried this but it gives an exception

     @foreach($apllied as $aplil)

     {{$aplil->job->jb_name}}

     @endforeach

this the error I am getting and can't figure out what am I doing wrong

     ErrorException (E_ERROR)
     Trying to get property 'job' of non-object (View: 
    D:\xampp\htdocs\locojobs\resources\views\dashboardviews\page\apllied- 
    job.blade.php)
1
  • You have to review your table structure. can you paste your table structure? Also, belongsTo() return one row not many. You have to take a look in relations in Laravel docs. Commented May 17, 2019 at 3:08

3 Answers 3

1

find() return one model so you need to do this:

     {{$apllied ->job->jb_name}} //Remove the loop

or use get() instead of find()

Sign up to request clarification or add additional context in comments.

4 Comments

get() feteches all the record in the table but i need specific job which user have applied. i have try this but it will only show one job applied
You need to add some conditions if you don't won't to get all records like ->where('job_id',5), find will always return one record based on applied id not job id
i tried this but whenever i use get() it return all records $apllied = AplliedJob::with(['user.job' => function ($query) use ($id) { $query->find($id);}])->get();
You have multiple issues ['user.job'=>] should be ['user','job'=>], you shouldn't use find inside the subquery use ->where instead, the id is already in main model(belongsto) so no need to add sub queries, anyways I advise you to read the elequent relationship again.
0

What i get from this code, each AplliedJob instance has one Job. So as @Anas Bakro says, you need the remove @foreach from your code. If you want to build different architecture please write down your tables.

Comments

0

Try to use get() or paginate() instead of find(), because the find() returns single value.

Try the below code,

public function appliedJob($id)
{

   $apllied = AplliedJob::with('user')->with('job')->get();

    return view('dashboardviews.page.apllied-job',compact('apllied'));
}

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.