繁体   English   中英

Laravel-数组到字符串的转换

[英]Laravel - Array to string conversion

我试图显示相关的应用程序以抽象,我使用了下面的代码,但出现此错误

Array to string conversion

我的控制器

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

编辑:(添加模型v1.0和v1.1)

我的模型(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;}

我编辑的模型(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;}

我想指出,使用当前答案为59的第二个答案来声明组合键

欲了解更多信息,这是我的看法

@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

您正在传递字符串以查看。

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

试试看。

编辑:或者,您可以像这样使用。

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

无论如何,您需要在此处传递数组。 如果您只想使用- >with('abstract'); 您需要添加抽象功能。 例如:

public function deliveries() {
       // this might be $this->hasOne... depends on what you need
     return $this->hasMany('Abstracts', 'conditions', 'id')->where('foo', '!=', 'bar');
    } 

$applications是控制器中的一个对象,但是您将$applications作为集合添加到视图文件中。 您可以尝试以下方法:

$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