【问题标题】:I want to access the values of an object to Laravel Blade file from Controller我想从控制器访问对象的值到 Laravel Blade 文件
【发布时间】:2020-07-13 13:52:59
【问题描述】:

这是我的代码。我正在尝试访问bookNamebookAuthor。但是变量是静态设置的。我不想将其更改为公开。但我想访问这些值。我该怎么做?

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Book
{
    private $bookName;
    private $bookAuthor;

    public function __construct($name, $author)
    {
        $this->bookName = $name;
        $this->bookAuthor = $author;
    }
    public function getNameAndAuthor()
    {
        return $this->bookName . ' - ' . $this->bookAuthor;
    }
}
class BookFactory
{
    public static function create($name, $author)
    {
        return new Book($name, $author);
    }
}

class FactoryController extends Controller
{
    public function index()
    {
        $book1 = BookFactory::create('Laravel', 'Imrul');
        $book2 = BookFactory::create('ReactJS', 'Hasan');

        $book1->getNameAndAuthor();
        $book2->getNameAndAuthor();
        // dump($book1);
        // dd($book1);
        return view('home', compact(['book1', 'book2']));
    }
}

home.blade.php

<h3>{{ $book1->bookName }}</h3>
<h3>{{ $book1->bookAuthor }}</h3>

【问题讨论】:

  • 制作一个模型,它会更容易,你试图访问一个模型的属性,实际上你想要访问的是你的变量:bookName,bookName。如果您尝试从 tee 控制器访问模型的属性,它会显示什么吗?

标签: php laravel static laravel-blade


【解决方案1】:

我建议你创建一个模型: php artisan make:model Book -a,使用 -a 会在模型之外为你创建迁移和控制器。

在您的迁移中:

public function up()
{
    Schema::table('books', function (Blueprint $table) {
        $table->increments('id');
        $table->string('author');
        $table->string('title');
    });
}

在您的模型上:

class Book extends Model
{
   protected $table = 'books';

   protected $fillable = [
   'author', 'title'
];


}

在您的控制器上:

public function create()
{
    $book1 = Book::create([
        'author' => 'Henry',
        'title' => 'Smith',
    ]);
    $book2 = Book::create([
        'author' => 'Antony',
        'title' => 'Gjj',
    ]);
    return view('home', compact(['book1', 'book2']));

}

在你的刀片上:

 <h3>{{ $book1->title }}</h3>
 <h3>{{ $book1->author }}</h3>

【讨论】:

  • 感谢兄弟的支持....这个错误来自Facade\Ignition\Exceptions\ViewException Trying to get property 'title' of non-object (View: E:\github\Design Patterns\Design-Patterns\resources\views\home.blade.php)
【解决方案2】:
class Book
{
    private $bookName;
    private $bookAuthor;

public function __construct($name, $author)
{
    $this->bookName = $name;
    $this->bookAuthor = $author;
}
public function getNameAndAuthor()
{
    return $this->bookName . ' - ' . $this->bookAuthor;
}

 public function getBookNameAttribute()
    {
        return $this->bookName;
    }
 public function getBookAuthorAttribute()
    {
        return  $this->bookAuthor;
    }

}

现在您在刀片中的代码应该可以工作了:

<h3>{{ $book1->bookName }}</h3>
<h3>{{ $book1->bookAuthor }}</h3>

【讨论】:

    猜你喜欢
    • 2021-08-29
    • 1970-01-01
    • 2019-01-29
    • 2017-08-31
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    相关资源
    最近更新 更多