model içerisine;
1 |
protected $hidden = ['kolonadi']; |
yok ben direk bir controller yaptım onun sonucunda çıkmasın diyorsan
1 |
$data = $data->makeHidden('kolonadi'); |
işe yarar kod blokları
model içerisine;
1 |
protected $hidden = ['kolonadi']; |
yok ben direk bir controller yaptım onun sonucunda çıkmasın diyorsan
1 |
$data = $data->makeHidden('kolonadi'); |
map fonksiyonu ile gelen verilere istediğimiz isimleri ve değerleri vererek kullanıcıya sunabiliriz.
1 2 3 4 5 6 7 8 |
$urun = Urun::take(10)->get(); $mapped = $urun->map(function($urun){ return [ 'id' => $product['id'], 'isim___' => $product['name'] ] }); return $mapped->all(); |
isterseniz fiyat değeri geliyorsa %18 kdv koyup yayınlarsınız ya da %5 zam korsunuz gibi gibi.
migrasyon oluşturmak için artisan komutu;
1 |
php artisan make:migration create_tabloismi_table |
public function up içine;
1 2 3 4 5 6 |
Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email'); $table->timestamps(); }); |
gibi tablo özelliklerini yazıyoruz. veri tipleri burada ayrıntılı bi şekilde yazıyor.
örneğin bir urun tablonuz olsun. ona kategoriler diye bir tablo daha oluşturacaksınız. eğer o kategorilerden birini sildiğimizde o kategoriye ait ürünlerin de silinmesini istiyorsak
1 |
$table->foreign('urun_id')->references('id')->on('urun')->onDelete('cascade'); |
urun tablosundaki 1 numaralı ürünü sildiğimizde kategori tablosunda da 1 numaralı urun_id’sine sahip ne varsa sil dedik.
çalıştırmak için migration’ı;
1 |
php artisan migrate |
değişiklik yaptıysak
1 |
php artisan migrate:fresh |
ilk önce telegram uygulamasını indirin ve webden giriş yapın.
https://web.telegram.org/
daha sonra https://web.telegram.org/#/im?p=@BotFather botfathera bağlanıyoruz. adamlar bizim bot yapacağımızı bildiğinden bize bot yapma botu yapmışlar. evet bunu da yaptılar.
bu çocuğa ilk diyoruz /newbot sonra o bize diyor isim ver veriyoruz sonra bi daha isim istiyor fakat sonunda _bot olacak şekilde veriyoruz ve bize bir anahtar veriyor. search kısmından botumuzu arıyoruz ve ona bir şeyler yazıyoruz. sonra aşağıdaki linke giriyoruz.
1 2 3 |
https://api.telegram.org/bot{token bilgisini giriyoruz}/getUpdates örn : https://api.telegram.org/bot1028492955:AAGa6L0glytHusLt8y3S6zPDYX2ep2_lFpk/getUpdates |
oradaki linkte bota yazılan mesaj olup olmadığına bakıyoruz. tabi biz yazdık. hemen oradan from->id kısmındaki id’yi alıyoruz. bu bota yazan kişinin id’si. buna mesaj göndereceğimiz için bu id’ye ihtiyacımız var.
mesaj göndermek için:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $token = "1028492912:AAGa6L0glytHusLt6y3S6zPDYX2ep6_lFpk"; $user_id = "1058079453"; $msg = "selam deneme mesajı"; $request_params = [ 'chat_id' => $user_id, 'text' => $msg ]; $request_url = 'https://api.telegram.org/bot'.$token.'/sendMessage?'.http_build_query($request_params); echo file_get_contents($request_url); ?> |
fotoğraf göndermek için:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$token = "1028492914:AAGa6L0glytHusLt8y3D4zPDYX2ep6_lFpk"; $user_id = "1058079203"; $msg = "a.jpg"; $request_params = [ 'chat_id' => $user_id, 'photo' => $msg ]; define('BOTAPI','https://api.telegram.org/bot' . $token .'/'); $cfile = new CURLFile(realpath('a.jpg'), 'image/jpg', 'a.jpg'); //first parameter is YOUR IMAGE path $data = [ 'chat_id' => $user_id , 'photo' => $cfile ]; $ch = curl_init(BOTAPI.'sendPhoto'); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); curl_close($ch); |
1 2 3 4 5 6 7 8 9 10 11 12 |
$offset = $request->has('offset') ? $request->query('offset') : 0; $limit = $request->has('limit') ? $request->query('limit') : 10; $sorgu = product::query(); if($request->has('q')) $sorgu->where('product_name','like','%'.$request->query('q').'%'); if($request->has('sortby')) $sorgu->orderBy($request->query('sortby'),$request->query('sort','desc')); $data = $sorgu->offset($offset)->limit($limit)->get(); return response($data,200); |
product?q=elma&sortby=id&sort=desc&limit=2
linkini yukarıdaki kod ile çalıştırabiliriz.
1 |
return response(product::paginate(10),200); |
zaten çıktının en altında otomatik diğer sayfalara nasıl gidileceğini yazıyor
1 2 3 4 5 6 7 8 9 |
public function store(Request $request) { $input = $request->all(); $product->create($input); return response([ 'data' => $product, 'message' => 'Product updated' ],200); } |
1 2 3 4 5 6 7 |
public function destroy(product $product) { $product->delete(); return response([ 'message' => 'Product updated' ],200); } |
modelimiz tanımlıysa ve gelen tüm verileri güncelleyeceksek
1 2 3 4 5 6 7 8 9 |
public function update(Request $request, product $product) { $input = $request->all(); $product->update($input); return response([ 'data' => $product, 'message' => 'Product updated' ],2000); } |
yok kardeşim ben tek kolonu güncelleyeceğim dersen
1 2 3 4 5 |
$flight = App\Flight::find(1); $flight->name = 'New Flight Name'; $flight->save(); |
hepsi normal ekleme silme işlemi gibi zaten.
zaten resource ile show metodu geliyor. eğer model tanımlamadıysak controller’a
1 2 3 4 5 6 7 8 |
public function show($id) { $product = product::find($id); if($product) return response($product,200); else return response(['message' => 'Ürün bulunamadı'],404); } |
eğer modelimiz varsa
1 2 3 4 |
public function show(product $product) { return response($product,200); /* örnek kullanım */ } |