<?php

namespace App\Http\Controllers;

use App\Models\foo;
use App\Models\User;
use Illuminate\Http\Request;


class FooController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = Foo::all();
        return $users;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //
        if (!$request->user()->tokenCan('uploader')) {
            abort(403,'你沒有上傳權限!'); 
        }
        $foo = Foo::Create([
            'name' => 'test4',
            'email' => 'foo2222@gmail.com',
            'fk_tests_id' => 1,
        ]);
        return $foo;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $foo = new Foo;

        $foo->name = $request->name;
        $foo->email = $request->email;
        $foo->email_verified_at = $request->email_verified_at;

        $foo->save();
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\foo  $foo
     * @return \Illuminate\Http\Response
     */
    public function show(foo $foo)
    {
        $foo = Foo::where('foos.id', '>', 1)
            ->leftjoin('fk_tests', 'fk_tests.id', '=', 'foos.fk_tests_id')
            ->select('foos.*', 'fk_tests.name as fk_name', 'fk_tests.id as fk_id')
            ->get();
        return $foo;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\foo  $foo
     * @return \Illuminate\Http\Response
     */
    public function edit(foo $foo)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\foo  $foo
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, foo $foo)
    {
        //
        $foo = foo::where('name', 'test2')->get()[0];
        // $foo = foo::find(1);
        $foo->email = $request->email;
        $foo->save();

        // foo::where('name', 'oransheep')
        //     ->update(['email' => $request->email]);

        return $foo;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\foo  $foo
     * @return \Illuminate\Http\Response
     */
    public function destroy(foo $foo)
    {
        //
    }
}
