jeudi 31 mars 2016

How should I call a function from model in laravel?

I'm build an application with Laravel which is new for me and I want to create any method in model and i will call that method from those model in some Controller but I don't know it is good or follow Laravel structure or not because Codeigniter let developer do that thing.

How can I use this structure with Eloquent instead of using raw sql query? because I got no data if I used $data = self::select("*")->get(); to query data.

This is my Model

    <?php
/**
 * Created by PhpStorm.
 * User: SOPHEAK Heng
 * Date: 16/03/31
 * Time: 10:29 AM
 * Notification Module
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Notification extends Model {

    protected $table = 'notification';
    public $timestamps = false;

    public function __construct(array $attributes)
    {
        parent::__construct($attributes);
    }
     public static function queryData() {

    $data = self::all();
    return $data;
    }
}

Here is Controller:

<?php
/**
 * Created by PhpStorm.
 * User: SOPHEAK Heng
 * Date: 5/25/2015
 * Time: 5:40 PM
 */

namespace App\Http\Controllers;
use App\Models\Notification;

class MyController extends Controller
{ 

    public function __construct()
    { 
        $t = $this->sendDataToView();
        var_dump($t);
    }
    public function sendDataToView() {

        $test = new Notification();
        return $test->queryData();
    }

}

Here is my result when I have try to used Eloquent It seem known that tables but can't see any data at there enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire