我最近一直在开发一个使用 Laravel 和 livewire 的项目,目的是测试 domCrawler 组件。我的应用程序从商店网页获取有关产品的数据并检索产品信息。我正在使用一个加载有产品类型对象的数组。
这是模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public $name;
public $price;
public $storeId;
public $img;
public $discount;
public $discountText;
public function __construct(array $attributes = []){
$this->name = $attributes["name"];
$this->price = $attributes["price"];
$this->storeId = $attributes["storeId"];
$this->img = $attributes["img"];
$this->discount = $attributes["discount"];
$this->discountText = $attributes["discountText"];
}
}
该页面使用带有文本输入和按钮的表单,用户输入产品名称,按下搜索按钮时,它会激活我的控制器的搜索功能。
这是视图的blade.php 文件:
<div >
<form wire:submit="search">
<p class="text-green-200 text-2xl">hola</p>
<label class="text-white">Que producto quiere buscar?</label>
<input class="" type="text" name="product" wire:model="product">
<button class="text-green-200 hover:text-red-400" type="submit">Buscar</button>
</form>
<button class="text-green-200 hover:text-red-400" wire:click="back">Volver</button>
@if($searched)
@foreach($productsAnswer as $product)
<p class="text-green-200 text-2xl">{{$product -> name}}<p>
@endforeach
@endif
</div>
按下提交按钮后,搜索函数会加载名为 $productsAnswer 的数组以及产品类型对象:
<?php
namespace App\Livewire;
use Livewire\Component;
use Livewire\Attributes\Layout;
use Symfony\Component\BrowserKit\HttpBrowser;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpClient\HttpClient;
use App\Models\Product;
use Exception;
use function Livewire\Volt\mount;
#[Layout("layouts.app")]
class Inicio extends Component
{
public $productsAnswer = array();
public $product = "";
public $searched = false;
public function render()
{
return view('livewire.inicio');
}
public function search(){
$this->searched = false;
$prods = [];
$coto = [];
$browser = new HttpBrowser(HttpClient::create());
//Coto
$crawler = $browser->request('GET', 'https://www.cotodigital3.com.ar/sitios/cdigi/browse?_dyncharset=utf-8&Dy=1&Ntt='.$this->product.'&Nty=1&Ntk=&siteScope=ok&_D%3AsiteScope=+&atg_store_searchInput='.$this->product.'&idSucursal=200&_D%3AidSucursal=+&search=Ir&_D%3Asearch=+&_DARGS=%2Fsitios%2Fcartridges%2FSearchBox%2FSearchBox.jsp');
foreach($crawler->filter("[class='clearfix']") as $prod){ array_push($prods, $prod); }
foreach($crawler->filter("[class='clearfix first']") as $prod){ array_push($prods, $prod); }
foreach($crawler->filter("[class='clearfix first_max']") as $prod){ array_push($prods, $prod); }
foreach($prods as $prod){
$crawlerProd = new Crawler($prod);
$name = $crawlerProd->filter("[class='descrip_full']")->text();
$price = $crawlerProd->filter("[class='atg_store_newPrice']")->text();
$img = $crawlerProd->filter("[class='atg_store_productImage'] > img")->attr('src');
$discountText = $crawlerProd->filter("[class='info_discount']") -> children() -> text();
if($discountText == $price){
$discount = 'No';
$discountText = 'None';
}
else{
$discount = 'Yes';
$discountText = strstr($discountText, 'OFERTA');
}
$newProd = new Product([
"name"=> $name,
"price"=> $price,
"storeId" => 'coto',
"img" => $img,
"discount" => $discount,
"discountText" => $discountText
]);
array_push($coto, $newProd);
}
$this -> productsAnswer = $coto;
$this -> searched = true;
}
}
问题是,当您第一次搜索某些内容时,该应用程序运行得非常好:
但是如果你尝试搜索其他内容,就会发生这种情况:
我想这与我使用数组的方式有关。也许它不喜欢被覆盖(?
我没主意了。我很感激任何帮助。
我在之前成功搜索后尝试搜索产品。
我希望看到新的名称数组出现。
这导致了未定义的数组键“名称”错误
皮波发表的评论https://stackoverflow.com/users/21380206/pippo 蒂姆·刘易斯https://stackoverflow.com/users/3965631/tim-lewis回答了我的问题。
简历 删除了名为product的模型,而是使用了一个简单的数组:
$newProd = ([
"name"=> $name,
"price"=> $price,
"storeId" => 'coto',
"img" => $img,
"discount" => $discount,
"discountText" => $discountText
]);
然后在foreach中使用wire:key:
@if($searched)
@foreach($productsAnswer as $product)
<div x-init="console.log(item)"><div>
<p class="text-green-200 text-2xl" wire:key="{{$product['name']}}">{{$product['name']}}<p>
@endforeach
@endif
问题解决了。