草庐IT

laravel-broadcast

全部标签

php - 如何通过模型 ID 和模型类型在 Laravel 中创建关系

我有一个关联的表,使用它们的id和id表类型表1编号,type_id,类型,数数,creteated_at,updated_at其他表格id名称,.......crteated_atupdated_at模型类型IDmodel2=type_id=2;model3=type_id=3;Inmodel1publicfunctionmodel2(){return$this->belongsTo(model2::class,'type_id','id');}publicfunctionmodel3(){return$this->belongsTo(model3::class,'type_id',

php - Laravel:如何验证 DB::transaction 函数是否正常工作?

我在我的Controller中使用了DB::transaction函数,publicfunctionstore(){$plant=newPlant;DB::transaction(function(){Plant::create(request(['name','plant_code','place']));});}我想知道我使用该功能的方式是否正常,我需要验证它是否正常工作? 最佳答案 作为documentation说明你有两个选择:带Closure的自动交易:YoumayusethetransactionmethodontheD

php - Laravel 中第三方 API 连接的最佳位置

当我突然想到时,我正在使用一个或多个第三方API编写Laravel应用程序。Laravel应用程序结构中哪里是设置API连接以从我的Controller使用它的最佳位置?您会使用服务还是将逻辑放在其他地方?$this->api=newRestApi();->setUrl(getenv('API_REST_URL'))->setUsername(getenv('API_USERNAME'))->setPassword(getenv('API_PASSWORD'))->connect(); 最佳答案 在Laravel中使用服务将是更好的

javascript - 将onclick函数设置为laravel中的 anchor 元素

我正在开发一个laravel应用程序,我想为Htmlanchor元素设置一个onclick函数。所以我按以下方式创建了它。在我的BladeView中{{$value[0]}}我的resources/assets/js/app.jsfunctionmyFunction(){console.log('whatever');returntrue;}这是行不通的,(我还仔细检查了app.js文件是否已使用laravel-mix(npmrundev)并且函数myFunction()在那里。)我想知道为什么它不起作用?blade有什么问题吗?注意:当我将此脚本插入我的同一个BladeView而不添

php - Ajax 成功后刷新/重新加载页面 - Laravel

在我的laravel项目中,我想在ajax成功后刷新我的页面,但我的页面会在成功后刷新。我尝试在Controller中使用laravelredirect进行刷新,但没有用,我也尝试在ajax中进行刷新,但什么也没发生?我该怎么做?我该怎么做?Controllerif(request()->ajax()){//dosomethingreturn['success'=>'successfullydone'];returnredirect('admin/all')->with('status','Successfullydone!');JS$('#master').on('click',fu

php - Laravel phpunit assertViewHas 与预期的测试数据不匹配

我正在为我的搜索方法编写测试,但目前有一个assertSee并且想将其更改为assertViewHas以获得更好的测试结果。但是我从搜索方法中获得的数据与我期望的数据不匹配。这是我设置$user的地方:protected$user;publicfunctionsetUp(){parent::setUp();$this->seed();$this->user=factory(User::class)->create(['role_id'=>3]);}这是我做出断言的地方:$response=$this->followingRedirects()->actingAs($this->user

php - 如何在 laravel 5.5 中监听 messageSent 事件

大家好,我是laravel事件和监听器的初学者。所以请向我解释如何实现这一目标:目标:向用户发送电子邮件。并知道电子邮件是否已发送。我的理解:Laravel有内置事件Illuminate\Mail\Events\MessageSent在电子邮件发送后触发,我必须编写一个监听器来监听该事件。我做了什么:发送电子邮件:Mail::to($receiverAddress)->send(newSendNewUserPassword($content));这工作正常。能够成功地向用户发送电子邮件。为了监听messageSent事件,我创建了这个监听器:message;}}注册事件:protect

php - Laravel 已售商品数量

我想计算每个卖家的已售商品。我尝试更改此代码:whereIn('seller_id',[4])->count();echo$count;?>我可以在其中添加其他$DB命令吗?{{$product->seller->id}}例如像这样而不是[4]:whereIn('seller_id',[{{$product->seller->id}}])->count();echo$count;?> 最佳答案 试试这个:在你的卖家模型中publicfunctionpurchases(){return$this->hasMany(Purchase::

php - Laravel 获取最后插入的数据

请找到下面的代码,我在这里尝试创建主记录并根据我添加相关数据的id。$country=newCountry;$country->name=$request->post('country-name');$country->xyz=$request->post('xyz');if($country->save()){$n=count($request->post('bname'));for($i=0;$iname=$request->post('bname')[$i];$dimention->xyz=$request->post('xyz')[$i];$dimention->cid=$co

php - 如何使用 Laravel 5.5 从多态关系中检索数据?

我是Laravel的新手。我正在使用PolymorphicRelations开发一个演示,遵循documentation我设置了3个表:--Users--id--username--Posts--id--summary--published--Images--id--link--alt--image_id--image_typeApp\Models\UserspublicfunctionscopeWithImages(){returnstatic::with('images')->get();}publicfunctionimages(){return$this->morphMany(