Find a simple example of how laravel5.2 controller calls models

WBOY
Release: 2016-07-06 13:52:58
Original
972 people have browsed it

For example, there is a table called abc

The structure is id name time

I want to write a model (app/models/abc.php). There is a method called abc_db()
The function is to return the id and name data of the abc table

Then create a controller (app/http/controller/test/TestController.php)
How to call the model in this file and implement var_dump (abc_db() returns data);

Please provide the above examples, thank you very much, I have never understood it, mainly because of eloquent

Reply content:

For example, there is a table called abc

The structure is id name time

I want to write a model (app/models/abc.php). There is a method called abc_db()
The function is to return the id and name data of the abc table

Then create a controller (app/http/controller/test/TestController.php)
How to call the model in this file and implement var_dump (abc_db() returns data);

Please provide the above examples, thank you very much, I have never understood it, mainly because of eloquent

Bank.php

<code><?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Bank extends Model
{
    protected $fillable=['card','user_id','name','bankName','province','city','branch'];

    public function user(){
        return $this->belongsTo(User::class);
    }

    public static function getBankById($id){
        return self::findOrFail($id);
    }
}</code>
Copy after login

HomeController.php

<code><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Model\Bank;

class ActivityVoteController extends Controller
{
    public function index(){
        dd(Bank::getBankById(1));
    }
}</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template