我刚刚开始学习 nuxtjs (有一些 vue.js 经验),我遇到了以下问题:
我有一个简单的todo-[id].vue页面。它应该从 API 获取待办事项并在页面上呈现:
<script setup>
import Section from '~~/components/UI/containers/section.vue';
import ButtonLink from '~~/components/UI/buttons/button-link.vue';
const route = useRoute()
const todoId = parseInt(route.params.id)
const { data: todo } = await useFetch(`https://jsonplaceholder.typicode.com/todos/${todoId}`)
</script>
<template>
<Section>
<div class="mt-4 flex space-x-4">
<ButtonLink :to="{name: 'todo-id', params: {id: todoId - 1}}" type="secondary" v-show="todoId !== 1">previous</ButtonLink>
<ButtonLink :to="{name: 'todo-id', params: {id: todoId + 1}}">next</ButtonLink>
</div>
<div class="mt-6">
<p class="text-lg" :class="[todo.completed ? 'text-green-600' : 'text-red-600']">{{ todo.title }} (#{{ todo.id }})</p>
</div>
</Section> …Run Code Online (Sandbox Code Playgroud) 我得到以下代码:
$popularProducts = PopularProduct::with('product')->get();
这就是我从popular_products(仅包含product_id)表中获取所有产品的方式,其中包含表的关系products。但我有这个结果:
Collection {#409 ?
#items: array:1 [?
0 => PopularProduct {#411 ?
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:4 [?]
#original: array:4 [?]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [?
"product" => Product {#462 ?}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: …Run Code Online (Sandbox Code Playgroud) I'm working on an laravel project. There're a lot of vue components and I need to make SSR due to SEO. I can't use nuxt.js or something like it because of my app is not SPA. I have default mix settings:
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.sass', 'public/css');
mix.disableSuccessNotifications();
Run Code Online (Sandbox Code Playgroud)
I put the vue components to laravel views (blade templates) like <example-component></example-component>. How may I implement SSR for there components only in this case?
I need to add this …
我得到以下收藏:
$this->items = collect([$productId => [
'name' => $product->title,
'price' => $product->price,
'is_sale' => $product->is_sale,
'sale_price' => $product->sale_price,
'sale_percent' => $product->sale_percent,
'can_use_promocode' => $product->can_use_promocode,
'qty' => 1,
]);
]);
Run Code Online (Sandbox Code Playgroud)
如何用钥匙搜索物品?在文档(https://laravel.com/docs/5.2/collections)中,我看不到任何方法
UPD:例如,用户将商品添加到购物车($this->items)。我想检查购物车中物品的存在(需要用钥匙做)。模拟php功能array_key_exists,但用于集合。
我有一个 vue.js SPA 应用程序。我的目标是在令牌通过 axios 拦截器过期时刷新令牌。当用户向api发送请求时,我需要首先检查令牌过期时间,如果过期则刷新它,然后完成用户的请求。
我有一个刷新功能:
const refreshToken = () => {
return new Promise((resolve, reject) => {
return axios.post('/api/auth/token/refresh/').then((response) => {
resolve(response)
}).catch((error) => {
reject(error)
})
})
}
Run Code Online (Sandbox Code Playgroud)
和axios请求拦截器:
axios.interceptors.request.use((config) => {
let originalRequest = config
if (jwt.isTokenExpired()) {
return api.refreshToken()
.then(res => {
if (res.data.error == 'TOKEN_BLACKLISTED' && res.headers.authorization) {
//get the token from headers without "Bearer " word
let token = res.headers.authorization.slice(7)
//save the token in localStorage
jwt.setToken(`"${token}"`)
//refresh "Authorization" header with new token
api.setHeader() …Run Code Online (Sandbox Code Playgroud) 我在网上商店。我有3个页面,其中产品过滤条件相似-目录页面,父类别页面和子类别页面
site/catalog/ // catalogController@index
site/catalog/parentCategory/childrenCategory //childrenCategoryController@index
site/catalog/parentCategory //parentCategoryController@index
Run Code Online (Sandbox Code Playgroud)
过滤器是通过get请求制作的,例如:site/catalog?price_from=1&price_to=9999&color=red。如何使这个过滤器成为一个单独的功能?在产品模型中做到这一点会很好吗?以及如何使用它?我认为这应该是一个接受2个参数(请求参数和当前查询模型)并返回的函数$this。
控制器中的代码:
$products = Product::filter($products, $request)->paginate(20);
Run Code Online (Sandbox Code Playgroud)
模型:
public function filter($model, Request $request) {
if ($request->has('price_from')) {
$model->where('price', '>', $request->get('price_from'));
}
if ($request->has('color')) {
$model->where('color', '>', $request->get('color'));
}
return $model;
}
Run Code Online (Sandbox Code Playgroud)
但是怎么做对呢?如何$products在控制器代码中传递电流?
我正在尝试使用 laravel 和 dompdf lib 生成 pdf 文件。这是我的控制器代码:
public function create(Request $request) {
$pdf = new Dompdf();
$html = Storage::disk('local')->get('public/pdf/template.html');
$pdf->loadHtml($html, 'UTF-8');
$pdf->setPaper('A4', 'portrait');
$pdf->render();
$filename = "Hi!";
return $pdf->stream($filename, ["Attachment" => false]);
}
Run Code Online (Sandbox Code Playgroud)
我template.html得到了一些 html,在某个地方我得到了这样的图像:
<img src="img/logo.png" alt="BTS">
但 dompdf 无法写入此图像:Image not found or type unknown。我的文件系统是:
.pdf
...img
......here is images
...template.html
...*some_fonts_files*
但与此同时,字体加载得很好。我应该怎么做才能在 template.html 中渲染图像?
使用后端(laravel)和前端 SPA(vue.js、vue-cli 3)进行服务。我需要通过 httpOnly cookie (不是 localStorage)进行身份验证。我使用tymondesigns/jwt-auth作为 api auth 包。
\n\n我的环境是:
\n\nhttp://brideplanner.test/apihttp://app-test.brideplanner.test:81/我的登录路径是/api/auth/login,控制器方法是:
public function login()\n {\n $credentials = request([\'email\', \'password\']);\n $user = User::where(\'email\',\'=\',$credentials[\'email\'])->first();\n if (!$user || !$token = auth()->claims([\'sub\' => $user->id, \'csrf-token\' => str_random(32) ])->attempt($credentials)) {\n return response()->json([\'error\' => \'Unauthorized\'], 401);\n }\n return response()\n ->json(\'success\')\n ->withCookie(\'token\', $token, config(\'jwt.ttl\'), ".brideplanner.test", null, false, false);\n }\nRun Code Online (Sandbox Code Playgroud)\n\ntoken但是当我尝试向 API 发送请求时, cookie 存储中没有任何项目。这里出了什么问题?为什么没有令牌?我应该怎么办?
UPD:我通过邮递员测试了请求,并得到了令牌:
\n\nSet-Cookie …Run Code Online (Sandbox Code Playgroud) 我需要将字符串转换0x2fe84e3113d7b为浮点类型。该字符串来自infura.io API作为帐户余额。我尝试使用https://github.com/mbezzanov/ethereum-converter,但在这种情况下没有任何意义(它总是以任何方式返回 0.00000 )。如何0.000842796652117371用php将此字符串转换为?
use Bezhanov\Ethereum\Converter;
...
$this->converter = new Converter();
$weiValue = '0x1e1e83d93bb6ebb88bbaf';
dump($this->converter->fromWei($weiValue)); // returns 0.00000000000000000000000000000000000
$hexValue = hexdec($weiValue); // returns 2.2757423599815E+24
dump($this->converter->fromWei($hexValue)); // returns the same
Run Code Online (Sandbox Code Playgroud)
我猜这是由于值太长造成的$hexValue(我的意思是转换器无法转换长整数)。但是如何从这个十六进制中获取以太值呢?
我得到了以下服务提供商:
class CartServiceProvider extends ServiceProvider {
public function boot() {
}
public function register() {
$this->app->singleton(\Alexxosipov\Cart\Cart::class, function($app) {
return new \Alexxosipov\Cart\Cart($app['request']);
});
}
}
Run Code Online (Sandbox Code Playgroud)
还有我的 Cart 课,我在那里得到了这个:
$this->id = (request()->cookie('cart_id')) ? request()->cookie('cart_id') : false;
Run Code Online (Sandbox Code Playgroud)
但request()->cookie('cart_id')返回加密字符串。如果我将在任何控制器中执行此操作,它都可以正常工作。为什么?我应该怎么做才能在 Cart 类中使用它? Laravel 5.5