我的更新,删除,创建功能不影响数据库

时间:2019-05-23 11:38:28

标签: php laravel

当我更新数据或删除或创建数据时不起作用。该代码没有给出任何类型的错误,因此对我来说似乎不错。当我尝试更新它时,更新后的数据出现在url中,但没有出现在数据库中。删除功能也一样。数据库是mysql。下面是我的控制器代码

<?php

namespace App\Http\Controllers;

use App\Company;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CompaniesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $comapanies = Company::all();
        return view('companies.index',['companies'=>$comapanies]);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
         if(Auth::check()){
             $company = Company::create([
                    'name'=>$request->input('name'),
                    'description'=>$request->input('description'),
                    'user_id'=>Auth::user()->id
                    //OR 'user_id'=>$request->user()->id above 5.5 versions of laravel
            ]);

            if($company){
                return redirect()->route('companies.show', ['company'=>$company->id])
                        ->with('success', 'Company Created Successfully');
            }
         }

         return back()->withInput()->with('errors', 'Error while creating company');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Company  $company
     * @return \Illuminate\Http\Response
     */
    public function show(Company $company)
    {
         $company = Company::where('id', $company->id)->first();
        // $company = Company::find($company->id);
        return view('companies.show', ['company'=>$company]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Company  $company
     * @return \Illuminate\Http\Response
     */
    public function edit(Company $company)
    {
        $company = Company::find($company->id);

        return view('companies.edit', ['company'=>$company]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Company  $company
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Company $company)
    {
        $companyupdate = Company::where('id', $company->id)->update([
                                               'name'=>$request->input('name'),
                                               'description'=>$request->input('description') 
                                            ]);
        if($companyupdate){
            return redirect()->route('companies.show',['company'=>$company->id])
                            ->with('success','Company Updated Successfully');
        }
         return back()->withInput()->with('errors', 'Updation failed');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Company  $company
     * @return \Illuminate\Http\Response
     */
    public function destroy(Company $company)
    {
        $findcompany = Company::find($company->id);
        if($findcompany->delete()){
            return redirect()->route('companies.index')
                    ->with('success', 'Company Deleted Successfully');
        }
        return back()->withInput()->with('errors', 'Company could not be deleted');
    }
}

这是“编辑”视图代码


@extends('layouts.app')

@section('content')
    <div class="col-md-9 col-lg-9 col-sm-9 pull-left">
    <!-- Jumbotron -->

        <div class="row col-lg-12 col-md-12 col-sm-12" style="background-color:white; margin:10px;">
            <form mehtod="post" action="{{ route('companies.update', [$company->id]) }}">
                {{ csrf_field() }}
                <input type="hidden" name="_method" value="put">

                <div class="form-group">
                    <label for="company-name">Name<span class="required">*</span></label>
                    <input placeholder="Enter Name"
                        id="company-name"
                        required
                        name="name"
                        spellcheck="false"
                        class="form-control"
                        value="{{$company->name}}"
                    />
                </div>
                <div class="form-group">
                    <label for="company-content">Description</label>
                    <textarea placeholder="Enter Description"
                        id="company-content"
                        required
                        name="description"
                        spellcheck="false"
                        class="form-control autosize-target text-left"
                        rows="5"> 
                        {{ $company->description }}</textarea>
                </div>
                <div class="form-group">
                    <input type="submit" class="btn btn-primary" value="Submit"/>
                </div>
            </form>
        </div>
    </div>

    <div class="col-sm-3 col-md-3 col-lg-3 pull-right">
         <!-- <div class="sidebar-module sidebar-module-inset">
        <h4>About</h4>
            <p>Etiam porta <em>sem malesuada magna</em> mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.</p>
          </div>-->
          <div class="sidebar-module">
            <h4>Actions</h4>
            <ol class="list-unstyled">
              <li><a href="/companies/{{ $company->id }}">View Company</a></li>
              <li><a href="/companies">All Companies</a></li>
            </ol>
          </div>
          <!--<div class="sidebar-module">
            <h4>Members</h4>
            <ol class="list-unstyled">
              <li><a href="#">March 2014</a></li>
            </ol>
          </div>-->
    </div>
    @endsection

这是我的公司模式

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
    Protected $fillable = [
        'name',
        'description',
        'user_id',
    ];

    public function users(){
        return $this->belongsTo('App\User');
    }

    public function projects(){
        return $this->hasmany('App\Project'); 
    }
}


1 个答案:

答案 0 :(得分:0)

在模型中添加表名

在这里,我使用表名“ companies”,可以将其替换为表名

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Company extends Model
{

    protected $table = 'companies';    

    Protected $fillable = [
        'name',
        'description',
        'user_id',
    ];

    public function users(){
        return $this->belongsTo('App\User');
    }

    public function projects(){
        return $this->hasmany('App\Project'); 
    }
}

在路由文件(web.php)中:

Route::resource('companies', 'CompaniesController');     

在用于存储和更新功能的控制器更改代码中:

public function store(Request $request){

    $post = $request->all();

    $user = Auth::User();

    if($user){
        $company = Company::create([
            'name'=> $post['name'],
            'description'=> $post['description'],
            'user_id'=> $user->id               
        ]);

        if($company){
            return redirect('/companies/'. $company->id)->with('success', 'Company Created Successfully');
        } else {
            return redirect('/companies/create')->with('error_message', 'Something went wrong!');
        }
    }    
}

public function update(Request $request, $id){

    $post = $request->all();

    $company = Company::find($id);

    $company->update($post);

    return redirect('/companies/'. $company->id)->with('success', 'Company Updated Successfully');
}