简体   繁体   English

Laravel-数组到字符串的转换

[英]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) 编辑:(添加模型v1.0和v1.1)

My model (v1.0) which show the error of Array to string conversion 我的模型(v1.0)显示了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) 我编辑的模型(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 我想指出,使用当前答案为59的第二个答案来声明组合键

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'); 如果您只想使用- >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. $applications是控制器中的一个对象,但是您将$applications作为集合添加到视图文件中。 You may try this: 您可以尝试以下方法:

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

return view('Abstracts.show', compact('abstract', 'applications'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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