从模型和控制器的刀片视图中获取json

时间:2019-11-29 17:48:29

标签: php jquery json laravel

我正在尝试将“旧的” PHP脚本转换为Laravel 这个脚本工作正常。我在jQuery fullcalendar中显示一些数据。但是现在我正在尝试将其集成到laravel项目中

我不想使用maddhatter软件包。我只想在日历上显示一些存储在我的mysql表中的数据。我不需要插入,更新等。

用于数据库查询的php页面:fetch-event.php

$ json = array();     $ sqlQuery =“ SELECT * FROM tbl_events OR ID BY”;

$result = mysqli_query($conn, $sqlQuery);
$eventArray = array();
while ($row = mysqli_fetch_assoc($result)) {
    array_push($eventArray, $row);
}
mysqli_free_result($result);

mysqli_close($conn);
echo json_encode($eventArray);

javascript的calendar.php页面

<script>
$(document).ready(function () { 
    var calendar = $('#calendar').fullCalendar({
        editable: false,
        events: "fetch-event.php",       
        } ,
        eventClick: function (event) {            
            $.getJSON('fetch-event.php', function (user) {
    var convertToTableau=Array.from(Object.values(user));
    var us=$.grep(convertToTableau,function(v){  
                  return v.id==event.id;                
    });

    $("#firstname").text(us[0].title);
    $("#idpilote").text(us[0].id); 
});
        }
    });
});

</script> 

我如何通过使用模型,控制器和视图将其转换为laravel(5.8)来转换此代码。谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这就是我在解决方案中进行的工作

我创建一个控制器

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;

class CalendrierController extends Controller
{
    public function getCalendrier()
    {
       // the view containing the fullcalendar
         return view('admin.blog.calendrier');        

    }

    public function getCalendrier2() {
        // the page who extract the data of the query 
        //(simple display of the data of the table) in json
        $products = Post::all();
        return $products;
    }
}

模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

    protected $fillable = [
        'name',
        'prenom',
        'adresse',
        'phone',        
        'date1',
        'date2',
        'date_born',
        'lieu_born',
        'nationalite',
        'profession',
        'type_avion',
        'immat_avion',
    ];


    public $timestamps = false;
}

显示全日历视图的路线

Route::get('/blog/calendrier', [   
        'uses' => 'CalendrierController@getCalendrier', 
        'as' => 'admin.blog.calendrier' // 
    ]);

带有jquery脚本的全日历刀片视图

<script>
                            $(document).ready(function() {

                                var calendar = $('#calendar').fullCalendar({
                                    editable: false,
                                    events: "{{ route('products') }}",                                    
                                    eventClick: function(event) {

                                        $.getJSON("{{ route('products') }}", function(user) {
                                            var convertToTableau = Array.from(Object.values(user));
                                            console.log(convertToTableau);
                                            var us = $.grep(convertToTableau, function(v) {


                                                return v.id == event.id;
                                                console.log(event.id);

                                            });
                                            $("#firstname").text(us[0].name);
                                            $("#idpilote").text(us[0].id);
                                        });
                                    }

                                });
                            });
                        </script>

                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12">

                    <div class="response"></div>
                    <div id='calendar'></div>
                    <div>
                        <p id="firstname"></p>
                        <p id="idpilote"></p>
                    </div>
                </div>
            </div>

在JSON中显示jquery脚本中url的输出查询的路由

Route::get('/products', [   
    'uses' => 'CalendrierController@getCalendrier2', 
    'as' => 'products' 
]);

在此页面(产品)中,我显示json

[{"id":1,"name":"Martin","prenom":"Andr\u00e9","adresse":"125 rue des Bleuets , Lamorlaye, 60005, France","phone":"0102030102","date1":"2019-07-01","date2":"2019-07-31","date_born":"1942-01-01","lieu_born":"Charleroix , Belgique","nationalite":"Fr","profession":"Pilote","type_avion":"Planneur","immat_avion":"DZ582MMA"},{"id":4,"name":"Raymond","prenom":"Michel","adresse":"15 avenue des platanes, Riez, 04250, Fr","phone":"04258745","date1":"2019-07-07","date2":"2019-07-18","date_born":"1960-05-08","lieu_born":"Toulouse , Fr","nationalite":"Fr","profession":"Pilote","type_avion":"Planneur","immat_avion":"DZ555A"},{"id":5,"name":"Schumacher","prenom":"Hanz","adresse":"Am Hallenbad, 52000, Wurzelen, De","phone":"201748541","date1":"2019-07-05","date2":"2019-07-11","date_born":"1953-05-12","lieu_born":"Berlin , De","nationalite":"De","profession":"Pilote","type_avion":"Planneur","immat_avion":"DZ3547MMA"},{"id":6,"name":"Carbonara","prenom":"Luigi","adresse":"Via Veneto, Roma, It","phone":"2147485","date1":"2019-07-08","date2":"2019-07-15","date_born":"1968-08-02","lieu_born":"Florence , It","nationalite":"It","profession":"Pilote","type_avion":"Planneur","immat_avion":"DBBB7MMA"}]

我认为全日历中有保留字(标题,开头,结尾)。在我的mysql表中,我有“ date1”和“ date2”,而不是“ start”和“ end”和“ name”,而不是“ title”。我将尝试通过更改mysql表中列的名称来进行尝试。在此之前,我必须对脚本(模型,迁移表等)进行一些小的更改。

那我会回来的。