0

After using my form for creating projects, I can see a list of the created projects ( title(string), code(string), domains (drop-down list), owners (drop-down list). instead of showing the name of the domain or owner selected previously in the drop-down list, I can only see the id. I don't know how to fix that.

here's my migration create_projects_table :

public function up()
{
    Schema::create('pros', function (Blueprint $table) {

    $table->increments('id', true);
    $table->string('title');
    $table->string('code');
    $table->integer('domain_id')->unsigned();
    $table->foreign('domain_id')->references('id')->on('domains');          
    $table->integer('owner_id')->unsigned();
    $table->foreign('owner_id')->references('id')->on('owners');            
    table->timestamps();
  });
}
public function down()
{
      Schema::dropIfExists('projects');
}

ProjectController :

public function index(Request $request)
{
//
 $projects = Project::orderBy('id','DESC')->paginate(5);
          return view('projects.index',compact('projects'))
           ->with('i', ($request->input('page', 1) - 1) * 5);
  }
 public function create()
 {
   $domains = Domaine::all('nameDomain', 'id');
   $owners = Owner::all('nameOwner', 'id');
   return view('projects.create', compact('domains', 'owners'));
 }
 public function store(Request $request)
 { 
      $this->validate($request, [
        'title' => 'required',
        'code' => 'required',
        'owner_id' => 'required',
        'domain_id' => 'required'
 ]);

 Project::create($request->all());
 return redirect()->route('projects.index') 
                ->with('success','Project created successfully');

 }
 public function show($id)
 {
   $domains = Domain::all('nameDomain', 'id');
   $owners = Owner::all('nameOwner', 'id');
   $project = Project::find($id);
   return view('projects.show',compact('project', 'domains', 'owners'));
 }

show.blade.php :

<div class="col-xs-12 col-sm-12 col-md-12">
   <div class="form-group">
      <strong>Quartier:</strong>
       {{ $pro->quartier_id }}
   </div>
</div>

<div class="col-xs-12 col-sm-12 col-md-12">
  <div class="form-group">
     <strong>Responsable:</strong>
      {{ $pro->responsable_id }}
  </div>
</div>

index.blade.php :

<!-- ........ -->
<table class="table table-bordered">
<tr>
    <th>No</th>
    <th>title</th>
    <th>code</th>
    <th>domain_id</th>
    <th>owner_id</th>
    <th width="280px">Action</th>
 </tr>
 @foreach ($projects as $key => $project)
  <tr>
   <td>{{ ++$i }}</td>
   <td>{{ $project->title }}</td>
   <td>{{ $project->code }}</td>
   <td>{{ $project->domain_id}}</td>
   <td>{{ $project->owner_id}}</td>


 <!-- ...... -->

Project.php :

class Pro extends Model
{
//
public $fillable = ['title','code','domain_id', 'owner_id'];

}

this is what I got after adding dd($projects); :

LengthAwarePaginator {#285 ▼
#total: 3
#lastPage: 1
#items: Collection {#327 ▼
  #items: array:3 [▶]
}
#perPage: 5
#currentPage: 1
#path: "http://127.0.0.1/Projet_PAC/Projet_PAC/public/pros"
#query: []
#fragment: null
#pageName: "page"
}

and then after adding {{ dd($project->domain) }} as first line in for each loop

Domain {#339 ▼
#table: "domains"
+fillable: array:1 [▼
0 => "domain"
]
#connection: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:4 [▶]
#original: array:4 [▼
"id" => 2
"nameDomain" => "domaine2"
"created_at" => "2017-02-01 22:55:25"
"updated_at" => "2017-02-01 22:55:25"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▼
0 => "*"
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
 +exists: true
 +wasRecentlyCreated: false
}
6
  • can you show what dd($project) shows before your return line in index function? Commented Feb 3, 2017 at 12:12
  • Sorry, I don't understand what you asked me to do, can you please explain it more ? Commented Feb 3, 2017 at 12:16
  • after this line $projects = Project::orderBy('id','DESC')->paginate(5); in your ProjectController add this dd($projects) . Then go to your index page on your website. And you should be able to see what the $project contains, and paste it in your question so we can see it. Commented Feb 3, 2017 at 12:19
  • please check the post again I edited to add the output I got Commented Feb 3, 2017 at 12:27
  • great now add you model Project.php so we can see how your eloquent relationships Commented Feb 3, 2017 at 12:34

2 Answers 2

1

You need to set proper eloquent relationships for the models like:

namespace App\Project;

Class Project {
  public function domain() {
    return $this->belongsTo('App\Domaine');
  }
  public function owner() {
    return $this->belongsTo('App\Owner');
  }
}

Now in the view, you can use:

@foreach ($projects as $key => $project)
  <tr>
   <td>{{ ++$i }}</td>
   <td>{{ $project->title }}</td>
   <td>{{ $project->code }}</td>
   <td>{{ $project->domain->name }}</td>
   <td>{{ $project->owner->name }}</td>
  </tr>
@endforeach

Note: I'm assuming that you have "name" column in your owners and domains table

Please have a look to this: https://laravel.com/docs/master/eloquent-relationships#one-to-many

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

6 Comments

yes, I have column name in the tables. I tried what you suggested, but, I got this error : ErrorException in 1f60125c79899e814ca59f79fba4340134d63b14.php line 60: Trying to get property of non-object (View: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\PC\resources\views\projects\index.blade.php)
Write this in your view as first line in for each loop: {{ dd($project->domain) }}
Got it, your column name is nameDomain not name
So it will be $project->domain->nameDomain in view file
Also make sure that you are using correct column name for owner
|
0

You don't have relations to your other models. try this:

Your Pro.php model should have this functions:

public function domaine() {
    return $this->hasOne(Domaine::class);
}

public function owner() {
    return $this->hasOne(Owner::Class);
}

In the view use this:

@foreach ($projects as $project)
  <tr>
   <td>{{ ++$i }}</td>
   <td>{{ $project->title }}</td>
   <td>{{ $project->code }}</td>
   <td>{{ $project->domaine->id }}</td>
   <td>{{ $project->owner->id }}</td>
  </tr>
@endforeach

(remove the dd($projects) i asked you to add earlier)

9 Comments

this is exactly what I tried, but it says : ErrorException in 1f60125c79899e814ca59f79fba4340134d63b14.php line 62: Trying to get property of non-object (View: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\PC\resources\views\projects\index.blade.php)
in your index function try to return this return view('projects.index',compact('projects')) and remove this part ->with('i', ($request->input('page', 1) - 1) * 5) to see if it works, also remove this lin eform your view just to test <td>{{ ++$i }}</td>
still getting same error: "Trying to get property of non-object"
I have edited my answer to get the id intead of name in your view maybe its tha name it cant find. try it
here's what I got : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'domaines.project_id' in 'where clause' (SQL: select * from domaines where domaines.project_id = 25 and domaines.project_id is not null limit 1) (View: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\PC\resources\views\projects\index.blade.php)
|

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.