【问题标题】:Column not found in SQL laravel 5.4在 SQL laravel 5.4 中找不到列
【发布时间】:2017-09-14 17:35:26
【问题描述】:

我正在尝试与在我的应用中注册的用户一起发布帖子,但发生了以下错误:

我正在使用 XAMPP,我的帖子表就是这张图片

这个错误在 phpMyAdmin 中:

我的PostController 是:

use App\Post;

class PostsController extends Controller

{

    public function __construct()
    {
        $this->middleware('auth')->except(['index','show']);
    }

    public function index()
    {
        $posts=Post::latest()->get();

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

    public function show(Post $post)
    {

        return view('posts.show',compact('post'));
    }

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

    public function store()
    {

       $this->validate(request(),[
           'title'=>'required',
           'body' => 'required|min:5'
       ]);


        Post::create(request([
           'title' => request('title'),
            'body' => request('body'),
            'user_id' =>auth()->id()
            //auth()->user()->id*/
        ]));

        return redirect()->home;
    }
}

和帖子模型:

class Post extends Model
{
    public function comments()
    {
      return $this->hasMany(Comment::class);
    }

    public function user()
    {
      return $this->belongsTo(User::class);
    }
}

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    看来你的 store 方法不对。

    试试这样的:

    public function store()
    {
    
       $this->validate(request(),[
           'title'=>'required',
           'body' => 'required|min:5'
       ]);
    
       Post::create([
           'title' => request('title'),
           'body' => request('body'),
           'user_id' =>auth()->id()
       ]);
    
        return redirect()->home;
    
    }
    

    此代码适合您吗?

    【讨论】:

    • 您的代码错误,因为您需要将一组信息传递给 Post 的 create 方法。该方法需要知道列 => 值。以前,您也发送了请求本身。就是这样……
    【解决方案2】:
    INSERT into posts (title, body, userId) VALUES ('My title', 'My body', 7)
    

    第一部分指定字段名称,第二部分指定要插入每个字段的值。 “我的标题”不是您要插入信息的字段,它显示为标题。

    此外,createdon 使用时间戳,因此您不需要包含它。记录更改后,更新时间可以插入当前时间戳。

    【讨论】:

    • 请写完整我该如何解决它
    • 我不确定您要在上面的语句中插入什么数据,但它应该进入上面括号中的 VALUES 之后的部分。问题出现在您如何命名字段中。插入帖子(字段 1、字段 2、字段 3)值(“字段 1 值”、“字段 2 值”、“字段 3 值”)
    【解决方案3】:
    <?php
    if (isset($_POST['submit']))
     {
        include('../dbcon.php');
        $rolno= $_POST['rollno'];
        $name= $_POST['name'];
        $city= $_POST['city'];
        $pcon= $_POST['pcon'];
        $std= $_POST['std'];
        $imagename= $_FILES['simg'] ['name'];
        $tempname= $_FILES['simg'] ['tmp_name'];
    
        move_uploaded_file($tempname,"../dataimg/$imagename");
    
        $qry= "SELECT * FROM `student` WHERE `rollno`='$rolno' AND `name`='$name'";
        //$qry="INSERT INTO `student`(`rollno`, `name`, `city`, `pcont`, `standerd`,`image`) VALUES ('$rolno','$name','$city','$pcon','$std','$imagename')";
            echo "$qry";
        $run= mysqli_query($con,$qry);
    
        if ($run == true)
         {
            ?>
            <script>
                alert('Data Inserted Successfully.');
            </script>
            <?php
    
        }
    
    } 
    

    ?>

    【讨论】:

    • 在这个程序中我遇到了问题
    • 请解释您的代码行,以便其他用户了解其功能
    猜你喜欢
    • 2017-09-11
    • 2021-04-07
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2018-10-30
    相关资源
    最近更新 更多