Hiển thị các bài đăng có nhãn Sữa lỗi. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Sữa lỗi. Hiển thị tất cả bài đăng

Thứ Tư, 15 tháng 8, 2018

Cách sửa lỗi thêm kí tự "/" vào column có kiểu json khi sử dụng lệnh response()->json

Gỉa sử ta có 1 model là Place được mapping từ table places, table này có feild là place_list_image có kiể dữ liệu là json

Và có 1 PlaceController có 1 function là indexJson dùng để tạo file json có nội dung như sau

public function indexJson(){    $places = Place::all();    $header = array (       'Content-Type' => 'application/json; charset=UTF-8',       'charset' => 'utf-8'    );
      return response()->json(['places' => $places,           'status' => Response::HTTP_OK],200,$header,JSON_UNESCAPED_UNICODE);
}

Khi tạo Json thì trường place_list_image sẽ bị encode 1 lần nữa và sẽ thêm các kí tự "/" vào nộii dung trường place_list_image. Để giải quyết vấn đề này có 2 cách

1. Vào file Model Place ta thêm code sau để chuyển sang array:

protected $casts = [    'place_list_image' => 'array',];

2. Vào file Model Place ta thêm code sau để decode trường place_list_image:

public function getPlaceListImageAttribute($value){    return json_decode($value);}



Thứ Hai, 23 tháng 7, 2018

Sử lỗi Laravel 5.4: Specified key was too long error

Laravel 5.4 made a change to the default database character set, and it’s now utf8mb4 which includes support for storing emojis. This only affects new applications and as long as you are running MySQL v5.7.7 and higher you do not need to do anything.
For those running MariaDB or older versions of MySQL you may hit this error when trying to run migrations:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:
use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}
After that everything should work as normal. If you enjoyed this post be sure and join the weekly Laravel newsletter and never miss out on new releases, framework tips, and new tutorials.

Laravel với MongoDB

Mở đầu Hello anh em, đến hẹn lại lên hôm nay mình xin chia sẻ với mọi người về chủ đề Laravel kết hợp với MongoDB mà thông thường ta hay ...