如何从Laravel 5.3中的表中检索所有数据?

时间:2016-12-05 10:33:07

标签: laravel laravel-5.3

这是我的控制器代码。但这不起作用。这有什么问题?

  class Subnet_Behind_ClientController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
         $datas = Subnet_behind_client::all();
        $data = $datas->first();

        /*  $data = Subnet_behind_client::all();*/

        return view('subnet_behind_clients',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //

        $data = Subnet_behind_client::create([
    'client_id' => $request->client_id,
    'ip_address' => $request->ip_address,
    'netmask' => $request->netmask,

    return view('subnet_behind_clients1',compact('data'));
    // other properties here
]);

    }

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

     /*    $data = Subnet_behind_client::all();
         dd($data);

        $data->client_id = $request->client_id;
        $data->ip_address = $request->ip_address;
        $data->netmask = $request->netmask;*/
      //  $data->clients = $request->clients;

/*
         $data->created_at = new DateTime;
        $data->updated_at = new DateTime;*/
        $datas = Subnet_behind_client::all();
        $data = $datas->first();

        $data->client_id = $request->client_id;
        $data->ip_address = $request->ip_address;
        $data->netmask = $request->netmask;

        $data->save();
        return back();

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        //

          /* $data = Subnet_behind_client::all();*/
           $datas = Subnet_behind_client::all();
        $data = $datas->first();

        return view('view2',compact('data'));
    }

}

我尝试为其创建另一个页面以使用create方法。这会有帮助吗?因为我以为我正在更新记录而不是添加记录。

2 个答案:

答案 0 :(得分:0)

假设我想为User设置数据,那么在使用表执行操作之前需要初始化该实例;

$user = new User;//Object initialization
$user->username = $request->username;//Assign column value
$user->role_id = $request->role_id;//Assign column value
$user->email = $request->email;//Assign column value
$user->save();//Store records by `save` method

//Check for success
if($user){
    //Redirect success, with success message
}
else {
    //Redirect error, with error message
}

basic of laravel Insert处查看相同内容。

更新:将show方法替换为下面一个

public function show(Request $request)
{
  $data = $datas->get();

  return view('view2',compact('data'));
}

答案 1 :(得分:0)

$data = Subnet_behind_client::create([
    'client_id' => $request->client_id,
    'ip_address' => $request->ip_address,
    'netmask' => $request->netmask,

    return view('subnet_behind_clients1',compact('data'));
    // other properties here
]);

这是一个错误,您的返回位于create()方法内。它应该是这样的。

$data = Subnet_behind_client::create([
    'client_id' => $request->client_id,
    'ip_address' => $request->ip_address,
    'netmask' => $request->netmask,
]);


return view('subnet_behind_clients1',compact('data'));

请解释您从路线呼叫的方法,以便我们更轻松地进行调试。