Laravel 5.5 Crud Routing

时间:2018-01-14 09:55:19

标签: php laravel-5.5

我是Laravel 5.5的新手,我正在尝试创建一个CRUD。现在我遇到了一个视图错误。我不知道我哪里出错了。如果有人指出我正确的方向,我将不胜感激。

我尝试了一些不同的尝试来解决此问题,例如将我的路由更改为大写L而不是l的小写leads以使其遵循目录大小写但不是果。

我的错误

Route [leads.create] not defined. (View: .../resources/views/leads/index.blade.php)

来自我的index.blade.php文件的错误来源

<div class="pull-right">
  <div class="btn-group"> <a href="{{ route('leads.create') }}" class="btn btn-info" >Add New</a> </div>
</div>

我的树

views
    |-- leads
    |   |-- create.blade.php
    |   |-- edit.blade.php
    |   |-- index.blade.php
    |   `-- show.blade.php

我的网站.php

// Leads
Route::resource('Leads','LeadsController');
Route::get('leads/index', function () { return view('Leads.index'); });
Route::get('leads/create', function () { return view('Leads.create'); });

我的控制器

namespace App\Http\Controllers;
use App\Leads;
use Illuminate\Http\Request;

class LeadsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       $videos = Leads::orderBy('id','DESC')->paginate(5);

       return view('leads.index',compact('leads'));
    }

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

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

            'first_name' => 'required',

            'primary_phone' => 'required',

        ]);


        Leads::create($request->all());

        return redirect()->route('leads.index')

                        ->with('success','Lead created successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $leads = Leads::find($id);
        return view('leads.show',compact('leads'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
       $leads = Leads::find($id);

        return view('leads.edit',compact('leads'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [

            'first_name' => 'required',

            'primary_phone' => 'required',

        ]);


        Leads::find($id)->update($request->all());

        return redirect()->route('leads.index')

                        ->with('success','Lead updated successfully');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Leads::find($id)->delete();

        return redirect()->route('leads.index')

                        ->with('success','Lead deleted successfully');
    }
}

2 个答案:

答案 0 :(得分:2)

您可以使用url()转到网址链接。

<div class="pull-right">
<div class="btn-group"> <a href="{{ url('leads/create') }}" class="btn btn-info" >Add New</a> </div>
 </div>

或者您可以使用named route

Route::get('leads/create', function () { 
     return view('Leads.create'); 
})->name('leads.create');

答案 1 :(得分:0)

Create Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Contact;
use Hash;

class ContactController extends Controller
{
    public function index(Request $request)
    {

        $search = $request->get('search');

        $field = $request->get('field') != '' ? $request->get('field') : 'first_name';
        $sort = $request->get('sort') != '' ? $request->get('sort') : 'asc';
        $contacts = new Contact();

        $contacts = $contacts->where('first_name', 'like', '%' . $search . '%')
            ->orderBy($field, $sort)
            ->paginate(10)
            ->withPath('?search=' . $search . '&field=' . $field . '&sort=' . $sort);
        return view('contacts.index', compact('contacts'))
                ->with('i', ($request->input('page', 1) - 1) * 10);
    }

    public function create()
        {
            return view('contacts.create');
        }

    public function store(Request $request)
        {
            $request->validate([
                'first_name'=>'required|min:3|max:50',
                'last_name'=>'required|min:3|max:50',
                'email'=>'required|email|unique:contacts',
                'phone' => 'required|numeric|phone',
                'password' =>'required|min:3|max:20',
                'confirm_password' =>'required|min:3|max:20|same:password'

            ]);

            $contact = new Contact([
                'first_name' => $request->get('first_name'),
                'last_name' => $request->get('last_name'),
                'email' => $request->get('email'),
                'job_title' => $request->get('job_title'),
                'city' => $request->get('city'),
                'country' => $request->get('country'),
                'phone' => $request->get('phone'),
                'password' => $request->get('password')

            ]);

            $contact->save();

            return redirect('/contacts/index')->with('success', 'Contact saved!');
        }

    public function edit($id)
        {
            $contact = Contact::find($id);
            //print_r($contact);exit;
            return view('contacts.edit', compact('contact'));        
        }

    public function update(Request $request, $id)
    {
        $request->validate([
                'first_name'=>'required|min:3|max:50',
                'last_name'=>'required|min:3|max:50',
                'email'=>'required|email',
                'city' => 'required'
            ]);

        $contact = Contact::find($id);
        $contact->first_name =  $request->get('first_name');
        $contact->last_name = $request->get('last_name');
        $contact->email = $request->get('email');
        $contact->job_title = $request->get('job_title');
        $contact->city = $request->get('city');
        $contact->country = $request->get('country');
        $contact->phone = $request->get('phone');
        $contact->password = $request->get('password');
        $contact->save();

        return redirect('/contacts/index')->with('success', 'Contact updated!');
    }

    public function delete($id)
    {
        $contact = Contact::find($id);
        $contact->delete();

        return redirect('/contacts/index')->with('success', 'Contact deleted!');
    }
}

创建模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $fillable = [
        'first_name',
        'last_name',
        'email',
        'city',
        'country',
        'job_title',
        'phone',
        'password'     
    ];

    public function setPasswordAttribute($password)
    {   
        $this->attributes['password'] = bcrypt($password);
    }
}

在web.php中创建路由

Route::get('contacts/index', 'ContactController@index');
Route::get('contacts/create', 'ContactController@create');
Route::post('contacts/store', 'ContactController@store');
Route::get('contacts/edit/{id}', 'ContactController@edit');
Route::post('contacts/update/{id}', 'ContactController@update');
Route::post('contacts/delete/{id}','ContactController@delete');
Route::post('contacts/index', 'ContactController@index');

在resources / views文件夹中创建base.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Laravel 5.8 & MySQL CRUD Tutorial</title>
  <link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
</head>
<body>
  <div class="container">
    @yield('main')
  </div>
  <script src="{{ asset('js/app.js') }}" type="text/js"></script>
</body>
</html>

在通讯录文件夹中创建index.blade.php

@extends('base')

@section('main')

<div class="col-sm-12">

  @if(session()->get('success'))
    <div class="alert alert-success">
      {{ session()->get('success') }}  
    </div>
  @endif
</div>

<div class="row">
   <div class="col-sm-12">

      <h1 class="display-3">Contacts</h1> 
         <a class="pull-right btn btn-primary" href="<?php echo url('contacts/create') ?>">Add Contacts</a>
   </div>
</div>

<div class="row">
   <form action="<?php echo url('contacts/index')?>" method="post">
      <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">

       <button class="pull-right btn btn-primary" href="<?php echo url('contacts/index') ?>">view all</button>

       <div class="pull-right col-lg-3 input-group custom-search-form">
           <input class="form-control" name="search" placeholder="Search..." type="text" value="{{ request('search') }}">
               <span class="input-group-btn ">
                   <button class="btn btn-default" type="submit">
                       <i class="fa fa-search"></i>
                   </button>
               </span>
       </div>
         <input type="hidden" value="{{request('field')}}" name="field"/>
         <input type="hidden" value="{{request('sort')}}" name="sort"/>
            <table class="table table-striped">
               <thead>
                 <tr>

                  <td>ID</td>
                  <td>
                    <a href="{{url('contacts/index')}}?search={{request('search')}}&field=first_name&sort={{request('sort','asc')=='asc'?'desc':'asc'}}">
                        Name
                    </a>
                        {{request('field','first_name')=='first_name'?(request('sort','asc')=='asc'?'&#9652;':'&#9662;'):''}}
                  </td>
                  <td>
                    <a href="{{url('contacts/index')}}?search={{request('search')}}&field=email&sort={{request('sort','asc')=='asc'?'desc':'asc'}}">
                        Email
                    </a>
                        {{request('field','email')=='email'?(request('sort','asc')=='asc'?'&#9652;':'&#9662;'):''}}
                  </td>
                  <td>
                    <a href="{{url('contacts/index')}}?search={{request('search')}}&field=job_title&sort={{request('sort','asc')=='asc'?'desc':'asc'}}">
                        Job Title
                    </a>
                        {{request('field','job_title')=='job_title'?(request('sort','asc')=='asc'?'&#9652;':'&#9662;'):''}}
                  </td>
                  <td>
                    <a href="{{url('contacts/index')}}?search={{request('search')}}&field=city&sort={{request('sort','asc')=='asc'?'desc':'asc'}}">
                        City
                    </a>
                        {{request('field','city')=='city'?(request('sort','asc')=='asc'?'&#9652;':'&#9662;'):''}}
                  </td>
                  <td>
                    <a href="{{url('contacts/index')}}?search={{request('search')}}&field=country&sort={{request('sort','asc')=='asc'?'desc':'asc'}}">
                        Country
                    </a>
                        {{request('field','country')=='country'?(request('sort','asc')=='asc'?'&#9652;':'&#9662;'):''}}
                  </td>
                  <td colspan = 2>Actions</td>
                 </tr>
               </thead>
               <tbody>
                 @foreach($contacts as $contact)
                 <tr>
                     <td>{{$contact->id}}</td>
                     <td>{{$contact->first_name}} {{$contact->last_name}}</td>
                     <td>{{$contact->email}}</td>
                     <td>{{$contact->job_title}}</td>
                     <td>{{$contact->city}}</td>
                     <td>{{$contact->country}}</td>
                     <td>
                       <a href ='edit/<?php echo $contact->id?>' class="btn btn-primary">Edit</a>
                     </td>
                     <td>
                         <form action="delete/<?php echo $contact->id?>" method="post">
                           <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">

                           <button class="btn btn-danger" type="submit">Delete</button>
                         </form>
                     </td>
                 </tr>
                 @endforeach
               </tbody>
            </table>
   </form>         
</div>
@endsection

创建表单create.blade.php

@extends('base')

@section('main')
<div class="row">
 <div class="col-sm-8 offset-sm-2">
    <h1 class="display-3">Add a contact</h1>
  <div>

      <form method="post" action="{{ url('contacts/store') }}">
           <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
        <div class="row">
          <div class="col-lg-6">   
                <div class="form-group">    
                    <label for="first_name">First Name:</label>
                    <input type="text" class="form-control" name="first_name"/>
                    <span class="text-danger">{{ $errors->first('first_name') }}</span>
                </div>
            </div>
            <div class="col-lg-6">  
                <div class="form-group">
                    <label for="last_name">Last Name:</label>
                    <input type="text" class="form-control" name="last_name"/>
                    <span class="text-danger">{{ $errors->first('last_name') }}</span>
                </div>
            </div>
        </div> 
        <div class="row">
            <div class="col-lg-6">   
                <div class="form-group">
                      <label for="email">Email:</label>
                      <input type="text" class="form-control" name="email"/>
                      <span class="text-danger">{{ $errors->first('email') }}</span>
                </div>
            </div>
            <div class="col-lg-6">        
              <div class="form-group">
                  <label for="phone">Phone:</label>
                  <input type="text" class="form-control" name="phone"/>
                  <span class="text-danger">{{ $errors->first('phone') }}</span>
              </div>
            </div>
        </div> 
        <div class="row">
            <div class="col-lg-6">     
                <div class="form-group">
                  <label for="city">City:</label>
                  <input type="text" class="form-control" name="city"/>
                  <span class="text-danger">{{ $errors->first('city') }}</span>
                </div>
            </div>
            <div class="col-lg-6">    
                <div class="form-group">
                      <label for="country">Country:</label>
                      <input type="text" class="form-control" name="country"/>
                      <span class="text-danger">{{ $errors->first('country') }}</span>
                </div>  
            </div>
        </div>  

        <div class="row">
            <div class="col-lg-6">     
                <div class="form-group">
                  <label for="password">Password:</label>
                  <input type="password" class="form-control" name="password"/>
                  <span class="text-danger">{{ $errors->first('password') }}</span>
                </div>
            </div>
            <div class="col-lg-6">    
                <div class="form-group">
                      <label for="confirm_password">Confrim Password:</label>
                      <input type="password" class="form-control" name="confirm_password"/>
                      <span class="text-danger">{{ $errors->first('confirm_password') }}</span>
                </div>  
            </div>
        </div>
        <div class="row">
            <div class="col-lg-6">     
                <div class="form-group">
                      <label for="job_title">Job Title:</label>
                      <input type="text" class="form-control" name="job_title"/>
                      <span class="text-danger">{{ $errors->first('job_title') }}</span>
                </div>
            </div>
        </div>  
          <button type="submit" class="btn btn-primary">Add contact</button>
      </form>
  </div>
</div>
</div>
@endsection

创建edit.blade.php

@extends('base') 
@section('main')
<div class="row">
    <div class="col-sm-8 offset-sm-2">
        <h1 class="display-3">Update a contact</h1>

        <form method="post" action="{{ url('contacts/update', $contact->id) }}">

           <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
            <div class="row">
                <div class="col-lg-6">
                    <div class="form-group">
                        <label for="first_name">First Name:</label>
                        <input type="text" class="form-control" name="first_name" value="<?php echo $contact->first_name ?>" />
                         <span class="text-danger">{{ $errors->first('first_name') }}</span>
                    </div>
                </div>
                <div class="col-lg-6">    
                    <div class="form-group">
                        <label for="last_name">Last Name:</label>
                        <input type="text" class="form-control" name="last_name" value="<?php echo $contact->last_name ?>" />
                         <span class="text-danger">{{ $errors->first('last_name') }}</span>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-lg-6">

                    <div class="form-group">
                        <label for="email">Email:</label>
                        <input type="text" class="form-control" name="email" value="<?php echo $contact->email ?>" />
                         <span class="text-danger">{{ $errors->first('email') }}</span>
                    </div>
                </div>
                <div class="col-lg-6">
                    <div class="form-group">
                        <label for="phone">Phone:</label>
                        <input type="text" class="form-control" name="phone" value="<?php echo $contact->phone ?>" />
                    </div>

                </div>
            </div>
            <div class="row">
                <div class="col-lg-6">     
                    <div class="form-group">
                        <label for="city">City:</label>
                        <input type="text" class="form-control" name="city" value="<?php echo $contact->city ?>" />
                    </div>
                </div>

                <div class="col-lg-6">  
                    <div class="form-group">
                        <label for="country">Country:</label>
                        <input type="text" class="form-control" name="country" value="<?php echo $contact->country ?>" />
                    </div>
                </div>
            </div>  
            <div class="row">
                <div class="col-lg-6">
                    <div class="form-group">
                        <label for="job_title">Job Title:</label>
                        <input type="text" class="form-control" name="job_title" value='<?php echo $contact->job_title;?>' />
                    </div>
                </div> 
            </div>         
            <button type="submit" class="btn btn-primary">Update</button>
        </form>
    </div>
</div>
@endsection

电话号码验证将这段代码放在启动功能的AppServiceProvider.php中

Validator::extend('phone', function($attribute, $value, $parameters, $validator) {

            return substr($value, 0, 3) == '+91';

            });

原始操作链接-https://www.techiediaries.com/php-laravel-crud-mysql-tutorial/