Validation input array laravel
Bác thử thay lại các select xem sao
<select name="question[{{ $id }}]">
Xử lý dữ liệu trong ajax
Bạn tìm hiểu thêm từ khóa jquery event dynamic element
xem sao nhé, trường hợp của bạn thử sửa như sau xem có giải quyết được bài toán của bạn không
<script type="text/javascript">
$(document).on('click', '#fTangDiem', function() {
$("#btnTangDiem", this)
.html("Đang xử lý...")
.attr("disabled", "disabled");
return true;
});
</script>
Laravel API và setInterval AJAX
Em đoán do lần đầu $.ajaxSetup()
được gọi do khi load trang. Từ lần thứ 2 nó không bắt được event load trang nên không gọi vào hàm này nữa dẫn đến lỗi csrf token mismatch
Bác thử xử lí theo kiểu set cái token vào data khi call ajax xem sao
$.ajax({
url: "savechange",
method: "PUT",
data: {
id: 123332423,
"_token": $('meta[name="csrf-token"]').attr('content')
},
});
Công nghệ được sử dụng trong Viblo là gì các bác!
Đúng đó bạn ơi, trước mình xem một video anh Thắng có nói qua về cách viblo đang vận hành thì phải
Link: https://viblo.asia/p/viblo-deployment-day-web-development-best-practices-Eb85oJGWl2G
Tại sao từ Eloquent có thể call được function định nghĩa cho query builder?
This is because Eloquent usesIlluminate\Database\Query\Builder
behind the scenes and you can find all those methods in this Builder class.
When you run function that does not exist in Users model in this case, magic method __call(https://www.php.net/manual/en/language.oop5.overloading.php#object.call) is used:
public function __call($method, $parameters)
{
if (in_array($method, ['increment', 'decrement'])) {
return call_user_func_array([$this, $method], $parameters);
}
$query = $this->newQuery();
return call_user_func_array([$query, $method], $parameters);
}
as you see in this method newQuery method is executed. And when you look at this method definitions you can see here:
public function newQuery()
{
$builder = $this->newQueryWithoutScopes();
foreach ($this->getGlobalScopes() as $identifier => $scope) {
$builder->withGlobalScope($identifier, $scope);
}
return $builder;
}
and further, when you look at newQueryWithoutScopes you'll see there:
$builder = $this->newEloquentBuilder(
$this->newBaseQueryBuilder()
);
and this newEloquentBuilder
method returns new Builder($query);
so it's an Illuminate\Database\Query\Builder
object
Config APP_URL trong .env laravel không nhận @@????
Phần gửi gửi mail bạn có dùng queue không ? Nếu có thử restart queue chạy
php artisan queue:restart
Rồi gửi mail lại xem sao
Các pro Laravel cho em hỏi chút về Query Builder với ạ
Bạn có thể sử dụng một class như là repository. Ví dụ như thế này, đảm bảo không cần extend Models
namespace App\Repo;
use Illuminate\Database\Connection;
use Illuminate\Support\Collection;
class StreetRepository implements StreetRepositoryInterface
{
/**
* @var \Illuminate\Database\Connection
*/
protected $db;
/**
* StreetRepository constructor.
*
* @param \Illuminate\Database\Connection $db
*/
public function __construct(Connection $db)
{
$this->db = $db;
}
/**
* @return \Illuminate\Database\Query\Builder
*/
protected function query()
{
return $this->db->table('streets');
}
public function paginateList($page = null, array $columns = ['*'], $perPage = StreetRepositoryInterface::DEFAULT_LIMIT)
{
return $this->query()
->paginate($perPage, $columns, 'page', $page);
}
....
Sử dụng toast trong laravel
toastr.clear();
Sau mỗi lần thực thi toast.success
hoặc toast.info
bạn ơi
Xin hỏi về get value 0 từ form trong laravel
Trên server xử lí, nếu value có giá trị bằng [] thì gán lại cho nó bằng 0 là được mà bạn =))
Validation trong Laravel
- Tạo 1 rule để validate số tiền trong ví và trong DB
php artisan make:rule CheckMaxMoney
Trong class CheckMaxMoney ở thư mục app/rule
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class CheckMaxMoney implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$money = Lấy dữ liệu money của người dùng trong DB;
if($value < $money) {
return true; // Pass validate
}
return false; // Không pass validate
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Số tiền trong ví nhỏ hơn số tiền chuyển .';
}
}
- Tạo 1 form validate để khi có request chuyển tiền
php artisan make:request TransactionRequest
Trong class TransactionRequest xử lí
<?php
namespace App\Http\Requests\Form;
use Illuminate\Foundation\Http\FormRequest;
use App\Rule\CheckMaxMoney;
class TransactionRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'monney' => new CheckMaxMoney($this->money)
];
}
}
Có thể idea đến triển khai sẽ gặp vài lỗi nhưng tư tưởng là vậy. Chúc bạn may mắn, mà chắc bạn đang dùng laravel nhỉ :3
Tham khảo thêm : https://laravel.com/docs/7.x/validation#using-rule-objects
Tìm hiểu về queues và Task Scheduling
Vì câu hỏi của bạn chưa rõ ràng về cấu hình queue ra sao nên mình cứ trả lời theo cách mình vẫn hay làm vậy. Thường thì mọi người hay cấu hình lưu job ở database
hoặc redis
. Mình thì hay chọn redis
nên mình setup như nhau.
- Cài đặt redis
sudo apt-get update
sudo apt-get install redis-server
- Kiểm tra xem nó đã được cài đặt hay chưa ? Nếu output là PONG thì coi như cài đặt thành công
redis-cli
127.0.0.1:6379> ping
//Output
PONG
- Tiếp theo là cài đặt
predis
đểLaravel
thao tác vớiredis
composer require predis/predis
- Cấu hình .env trong
Laravel
QUEUE_DRIVER=redis
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_CLIENT=predis
- Chạy câu lệnh để lằng nghe các job trong queue ở một terminal và giữ nguyên chúng trong quá trình làm việc ở local.
php artisan queue:restart // Cẩn thận nên mình khởi động lại queue
php artisan queue:work redis
- Khi có 1 job được lắng nghe trên terminal sẽ hiển thị như sau. Trường này là job của mình đang bị fail
SUN-ASTERISK\nguyen.huu.[email protected]-pc:~/Framgia-project/manager_shirt$ php artisan queue:work redis
[2020-04-26 07:16:16][pufXwWA6lXRXesqc5yGrJVelYBRQtYr7] Processing: App\Jobs\ProcessGetDataSheet
[2020-04-26 07:18:37][pufXwWA6lXRXesqc5yGrJVelYBRQtYr7] Failed: App\Jobs\ProcessGetDataSheet
- Khi queue lỗi thì theo config mặc định của file config/queue.php sẽ lưu vào bảng
fail_jobs
như sau
'failed' => [
'driver' => env('QUEUE_FAILED_DRIVER', 'database'),
'database' => env('DB_CONNECTION', 'mysql'),
'table' => 'failed_jobs',
],
Chúc bạn may mắn
PHP- tăng thời gian đợi khi load website
Bonus nếu bạn dùng Laravel bạn có thể khai báo luôn ở constructor của class
public function __construct()
{
set_time_limit(8000000);
}
Service hỗ trợ gọi video
Service khá ngon, bạn có thể tham khảo. Chúc bạn thành công https://stringee.com/vi/video-conference
Hỏi về cách note lại những kiến thức mình biết, nhận được hằng ngày
From G4 with love
hỏi trang kenh14
Đây là toàn bộ công nghệ mà site kenh14.vn đang sửa dụng bạn nhé, lướt qua 1 chút xem nào
Backend: ASP.NET
Frontend: Jquery thần thánh
Server: Nginx
Quay lại câu hỏi của bạn. Trước tiên tìm hiểu qua định nghĩa nhé.
Bootstrap là gì?
Bootstrap là một framework cho phép thiết kế website reponsive nhanh hơn và dễ dàng hơn Bootstrap là bao gồm các HTML templates, CSS templates và Javascript tao ra những cái cơ bản có sẵn như: typography, forms, buttons, tables, navigation, modals, image carousels và nhiều thứ khác. Trong bootstrap có thêm các plugin Javascript trong nó. Giúp cho việc thiết kế reponsive của bạn dễ dàng hơn và nhanh chóng hơn.
Suy luận 1:
Mình check <head> của site không thấy key bootstrap nào cả, vậy là không dùng Bootstrap
rồi
Chưa thể kết luận vội vì có thể dev bên đó đã mix file bootstrap nó vào file css nào đó
Suy luận 2:
F12 tìm đến tab element tìm các class thần thánh của Bootstrap
như btn btn-succces
, row
. . . nhưng cũng không thấy có
Suy luận 3:
Thu nhỏ site kenh14 về định dạng mobile, tablet thấy phần menu thiết kế khá ẩu khi không col lại theo từng màn hình được.
Kết luận : 80% là không dùng Bootstrap
bạn nhé
Lỗi khi vào trang viblo
Bạn thử F12 lên xoá hết cookie, storage đi rồi đăng nhập lại xem
Convert HTML/CSS/JS to VueJS Component
Nếu bạn viết Vuejs trong project laravel bạn có thể mở file theo đường dẫn resources/js/bootstrap.js
Ở đây bạn có thể thấy Laravel đã require sẵn jquery và một số thư viện js khác. Việc của bạn sẽ là tìm các thư viện publish trên npm packages và require vào đây
Ví dụ trong trường hợp project của mình mình đã require thư viện admin-lte
window._ = require('lodash');
/**
* We'll load jQuery and the Bootstrap jQuery plugin which provides support
* for JavaScript based Bootstrap features such as modals and tabs. This
* code may be modified to fit the specific needs of your application.
*/
try {
window.Popper = require('popper.js').default;
window.$ = window.jQuery = require('jquery');
require('bootstrap');
require('admin-lte');
require('jquery-contextmenu');
} catch (e) {}
Tham khảo thêm tại : https://viblo.asia/p/frontend-install-admin-lte-as-a-node-dependency-gDVK2R9eKLj
Lấy 2 mảng con trong mảng - Laravel
$this->whereHas('bills', function($query) {
$query->orderBy('id', 'DESC')->limit(2);
})->with('bills')->orderBy('id', 'ASC')->limit(10)->get();
Không biết đúng ý của bạn chưa
Viblo có thể làm cái cúp cho hot authors của tháng được không ?
Thi thoảng đọc được mấy bài đấy mình hay tự hỏi: "Làm sao để viết được những bài viblo tuyệt vời vậy nhỉ ?".
2 tip nhỏ em hay đặt ra để tạo ra một bài report hay.
- Luôn đặt target bài viết của mình phải được editor's choise Khi có target rồi sẽ dẫn tới hành động
- Viết bài report trước kì deadline, vì khi có deadline mình hay đi dịch để có bài. Vì thế em luôn chủ động viết trước deadline 5-10 ngày, mình trau chuốt bài viết của mình hơn/
Chúc anh có nhiều bài viết hay hơn nữa.
Lỗi khi chạy laravel sử dụng docker compose
B1: docker ps (list all container docker)
B2: Bash vào workspace của laravel
docker exec -it ten_workspace bash
B3: cấp quyền ghi cho thư mục storage, để cho nhanh mình thường dùng lệnh
sudo chmod -R 777 storage