helloxielan 2017-02-10 14:26 采纳率: 0%
浏览 65

Laravel不返回页面

I am saving data into database by passing variable throught ajax and using it in the controller.

Saving works fine, it saves data into a database but the problem is displaying saved page. Database has 2 fields, name and html. Name is obviously name of the website and html is pure html code. so when user saves page with name 'random' I want to display html page to him using localhost/random

Error message I get is:

Trying to get property of non-object (View: C:\xampp\htdocs\fyprojectesources\views\layouts\website.blade.php)

Here's what I have done so far:

Ajax:

var web_name;
function updateDatabase(newCode, name_website)
{
    code2 = document.getElementById("content-link2").innerHTML;
    web_name = ($('#website_name').val());
    // make an ajax request to a PHP file
    // on our site that will update the database
    // pass in our lat/lng as parameters
    $.post('http://localhost/template', {
            _token: $('meta[name=csrf-token]').attr('content'),
            newCode: (code2),
            name_website: (web_name),
        })
        .done(function() {
        })
        .fail(function() {
            alert("error");
        });
}

Controller:

     public function postDB(Request $request) {
        $newName = $request->input('name_website');
        $newLat = $request->input('newCode');
        $websites = new Website();
        $websites->name = $newName;
        $websites->html = $newLat;
        $websites->save();
        $name = $newName;
        return redirect($name);
    }
    public function website($name) {
    $website = Website::where('name', $name)->first()->html;

    // Render resources/views/template.blade.php or any view you want
    // and pass the data. E.g. $website, so you can access $website->html in your view.
    return view('layouts/website', compact('name'));
}
}

Routes:

Route::get('home', 'BuilderController@homepage');
Route::get('template', 'BuilderController@templates');
Route::post('template', 'BuilderController@postDB');
Route::get('logout', 'BuilderController@getLogout');
Route::get('/{name}', 'BuilderController@website');

website.blade.php:

@extends('layouts.master') @section('title', 'Website Builder') @section('content')
<meta name="csrf-token" content="{{ csrf_token() }}" />


{{html_entity_decode($name->html)}}
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    axios.get('/localhost/name').then(
        html => document.querySelector('html').innerHTML = html     
    );
</script>
</html>
@endsection @show

What is going on?

  • 写回答

2条回答

  • weixin_33709590 2017-02-10 14:39
    关注

    The problem is you are passing variable called name to your view while you need to pass $website. furthermore you have a mistake in your Eloquent query.

    Try to change the following,

    $name->html // Change this
    $website->html // to this
    return view('layouts/website', compact('name')); // Change this
    return view('layouts/website', compact('website')); // To this
    $website = Website::where('name', $name)->first()->html; // Change this
    $website = Website::where('name', $name)->first(); // To this
    
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?