0

I am trying to show the related applications to abstract, I have used the code below but I am getting this error

Array to string conversion

My controller

public function show($A_ID){

  $abstract = Project::find($A_ID);

  // I believe the issue is caused by the line below but I am not sure what is wrong about it 

  $applications = Application::find($A_ID);
  return view('Abstracts.show')->with('abstract', $abstract)
                             ->with($applications);
}

EDIT: (add model v1.0 and v1.1)

My model (v1.0) which show the error of Array to string conversion

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;
use Traits\HasCompositePrimaryKey; 

class Application extends Model{


    //Table name
    protected $table = 'student_application';

    //composite key
    protected $primaryKey = array('A_ID', 'S_ID');
    protected $fillable = ['S_Justification' ];
    public $incrementing = false;}

My edited Model (V1.1)

    <?php
    namespace App;

    use Illuminate\Database\Eloquent\Model;
    use App\Traits\HasCompositePrimaryKey; 

    class Application extends Model{
    use HasCompositePrimaryKey; 


    //Table name
    protected $table = 'student_application';

    //composite key
    protected $primaryKey = array('A_ID', 'S_ID');
    protected $fillable = ['S_Justification' ];
    public $incrementing = false;}

I want to note that the composite key is declared using this answer number two with currently 59 votes

For more information here is my view

@if (count($applications)>0)
@foreach ($applications as $application)
<tr>
        <td><h5>{{$application->S_ID}}</h5></td>
</tr>
@endforeach
@else 
<p> This project has no applications </p>
@endif
ALI ISMAEEL
  • 115
  • 1
  • 14

2 Answers2

0

You are passing string to view.

return view('Abstracts.show')->with(['abstract'=> $abstract)];

give it a try.

Edit: Or you can use like that.

with(array('order' => function($query)

Anyway you need to pass array in here. If you are just want to use ->with('abstract'); you need to add abstract function. For example:

public function deliveries() {
       // this might be $this->hasOne... depends on what you need
     return $this->hasMany('Abstracts', 'conditions', 'id')->where('foo', '!=', 'bar');
    } 
Ali Özen
  • 1,525
  • 2
  • 14
  • 29
  • there is no issue with my return view with abstracts, the issue with applications – ALI ISMAEEL Dec 29 '18 at 08:37
  • You can do samethings to the applications. Just give it a try. you can import applications in function($query) that works for sure. – Ali Özen Dec 29 '18 at 08:43
  • can you clarify this line a little bit more `with(array('order' => function($query) ` I am not where and how to use it – ALI ISMAEEL Dec 29 '18 at 08:49
0

$applications is an object in your controller but you are accesing $applications as collection in your view file. You may try this:

$applications = Application::where('id', $A_ID)->get();

return view('Abstracts.show', compact('abstract', 'applications'));
Mahbub
  • 4,812
  • 1
  • 31
  • 34