试图从数据透视表中获取值

时间:2017-08-29 19:58:25

标签: laravel pivot-table

我有这个博客应用,帖子有标签。我做了很多很多关系,它的存储效果很好,但是在编辑时无法恢复它的价值。

让我们看一些代码:

发布模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
  public function category()
  {
    return $this->belongsTo('App\Category');
  }
  public function tags()
  {
    return $this->belongsToMany('App\Tag');
  }
  public function comments()
  {
    return $this->hasMany('App\Comment');
  }
  public function marca()
  {
    return $this->belongsToMany('App\Marca');
  }
  public function modelo()
  {
    return $this->belongsToMany('App\Modelo');
  }
  public function versao()
  {
    return $this->belongsToMany('App\Versao');
  }
}

PostController @create:

 $post = new Post;
        $post->title = $request->title;
        $post->slug = $request->slug;

        $post->category_id = $request->category_id;
        $post->body = $request->body;

        if ($request->hasFile('featured_image')){
            $image = $request->file('featured_image');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $location = public_path('images/' . $filename);
            Image::make($image)->save($location);
            $post->image = $filename;

        }

        $post->save();

        $post->tags()->sync($request->tags, false);

PostController @ edit:

public function edit($id)
{
    $post       = Post::find($id);
    $categories = Category::all();
    $tags = Tag::all();
    $tags2 = array();
    foreach ($tags as $tag) {
        $tags2[$tag->id] = $tag->name;
    }
    $marcas     = Marca::all();
    $modelos    = Modelo::all();
    $versaos    = Versao::all(); 
    return view('manage.posts.edit')->withPost($post)->withCategories($categories)->withMarcas($marcas)->withTags($tags2)->withModelos($modelos)->withVersaos($versaos);
}

最后,但并非最不重要的是HTML:

 <label for="cambio">TAG</label>
                    <select class="custom-select select-multi" name="tags[]" multiple="multiple">
                        <option value="{{ $tags->id }}">{{ $tags->name }}</option>
                    </select>
                    <br>
                    <br>

错误是:

(2/2) ErrorException
Trying to get property of non-object (View: /Users/marcellopato/Sites/CepCar2.0-BootStrap3.3/resources/views/manage/posts/edit.blade.php)

2 个答案:

答案 0 :(得分:1)

试试这个版本:

public function edit($id)
{
    $post       = Post::with('tags')->find($id);
    $categories = Category::all();
    $tags = Tag::all();
    $marcas     = Marca::all();
    $modelos    = Modelo::all();
    $versaos    = Versao::all(); 
    return view('manage.posts.edit')->withPost($post)->withCategories($categories)->withMarcas($marcas)->withModelos($modelos)->withVersaos($versaos);
}

在视图中:

<label for="cambio">TAG</label>
<select class="custom-select select-multi" name="tags[]" multiple="multiple">
     @foreach ($post->tags as $tag)
          <option value="{{ $tag->id }}">{{ $tag->name }}</option>
     @endforeach
</select>
<br>
<br>

答案 1 :(得分:0)

感谢@jacurtis和他的Vuejs.js课程,我找到了解决问题的方法。

一些剧本:

  var app = new Vue({
    el: '#app',
    data : {   
      ... 
        coresSelected : {!! $versao->cores->pluck('id') !!},
      ...
}

在HTML部分:

<label>Cores Existentes</label><br>
                <select class="custom-select select-multi but-to-but" multiple="multiple" name="cor[]" v-model="coresSelected">
                  @foreach($cores as $cor)
                    <option :value="{{ $cor->id }}">{{ $cor->descricao }}</option>
                  @endforeach
                </select>

结果是:

Those are colors added on create section of my CorController

谢谢大家!