简体   繁体   中英

Laravel - Array to string conversion

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

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');
    } 

$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'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM