加入这个雄辩的laravel5

时间:2015-06-15 19:33:33

标签: php laravel

我正确地接收了这两条消息,但我需要使用Laravel5加入这两个结果。

有人可以帮助我吗?或者是否有可能以其他形式这样做?

<?php
    require_once 'includes/dbconnect.php';

    session_start();
    //require_once 'includes/dbconnect.php';

    /* if (isset($_SESSION["username"]) && !empty($_SESSION["username"])){
    echo "".strtoupper($_SESSION['username'])."(Sign out)"; }else{
    echo "Sign In";
    } */

     if (isset($_POST["request"])) 
     {
    //take care of injections
    $service = mysqli_real_escape_string($link,$_POST['service']);
    $service_details  = mysqli_real_escape_string($link,$_POST['service_details']);
    $duedate  = mysqli_real_escape_string($link,$_POST['duedate']);
    $postdate  = mysqli_real_escape_string($link,$_POST['postdate']); //READ ONLY DATE POST



    // Insert data into mysql + LOGGED IN USERS USERNAME TO BE INSERTED
     $sql = "INSERT INTO serviceposts VALUES ('".$_SESSION["username"]."','".$service."', '".$service_details."', '".$duedate."', '".$postdate."')";

    $result= $link -> query($sql) or die (mysqli_error($link));

     echo "($result)";
    // if successfully insert data into database, displays message "Successful".
    if($result){
            echo "request post success!";
             echo'<script>window.location="http://localhost/Project2/User.php#viewrequests";</script>'; 

       // header('Location: ../User.php?success=1');

    }
    else {
        echo "Possible data input error";
    //header('Location: ../user.php?error=1');


            }
    $link->close();
    }

    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <script src="includes/js/modernizr.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="jqueryAssets/jquery.mobile.icons.min.css" rel="stylesheet" type="text/css">
    <link href="jqueryAssets/Mobi~Style~Me.min.css" rel="stylesheet" type="text/css">
    <link href="jqueryAssets/jquery.mobile-1.4.5.min.css" rel="stylesheet" type="text/css">
    <link href="jqueryAssets/jquery.ui.theme.min.css" rel="stylesheet" type="text/css">
    <link href="jqueryAssets/jquery.mobile.icons-1.4.5.min.css" rel="stylesheet" type="text/css">
    <link href="jqueryAssets/jquery.mobile.structure-1.4.5.min.css" rel="stylesheet" type="text/css">
    <link href="jqueryAssets/jquery.ui.datepicker.min.css" rel="stylesheet" type="text/css">
    <style type="text/css">
    body,td,th {
        font-family: Constantia, "Lucida Bright", "DejaVu Serif", Georgia, serif;
        color: #303;
    }
    body {
        background-image: url();
    }
    </style>
    <link href="jqueryAssets/jquery.ui.core.min.css" rel="stylesheet" type="text/css">
    <style type="text/css">
    a {
        font-weight: bolder;
    }
    </style>
    <script src="jqueryAssets/jquery-1.11.1.min.js"></script>
    <script src="jqueryAssets/jquery-ui.min.js"></script>
    <script src="jqueryAssets/jquery-2.1.1.min.js"></script>
    <script src="jqueryAssets/jquery.mobile-1.4.5.min.js"></script>
    <script src="jqueryAssets/jquery-ui-1.9.2.dialog.custom.min.js"></script>
    <script src="jqueryAssets/jquery-1.8.3.min.js" type="text/javascript"></script>
    <script src="jqueryAssets/jquery-ui-1.9.2.datepicker.custom.min.js" type="text/javascript"></script>
    <title>MyITjobs</title>
    <meta charset="utf-8">
    </head>  
    <body>  
      <div data-role="page" id="servicerequest" data-theme="c">
    <div data-role="header" data-add-back-btn="true"><h4>IT~Service Request</h4></div>
    <span align="center"><?php if (isset($_SESSION["username"]))
    {
     echo "".$_SESSION["username"]."";
    }
    ?>
    </span>
    <div data-role="content" class="ui-field-contain" align="center" data-content-theme="c">
         <form style="align-items:justify" method="post" action="" id="servicerequest">
        <span>
        <select name="selectmenu" required class="ui-bar-c slidedown" id="service" data-collapsed="false">
        <option selected="selected" class="ui-selectmenu-placeholder">IT-Services</option>
        <option value="service1" id="MobileApps">Mobile Apps Development</option>
        <option value="service2" id="WebDev">Website Development</option>
        <option value="service3" id="Programming">Programming</option>
               </select>
               </span>
               <br>
        <span>
        <div data-role="fieldcontain">
        <label for="service_details">IT~Service Request Details</label>
          <textarea name="service_details" cols="35" rows="6" id="service_details" placeholder="Input Request details"></textarea>
        </div>
        </span>
          <span>        
            <label for="duedate">Set Due Date:</label>
            <input type="datetime-local" required name="duedate" id="duedate" data-mini="true">
            </span> 
             <br>   
          <span>
           <label for="datetime-local">Request Date</label>
           <DateTime id="postdate" data-mini="true">
            <?php      
          $postdate = date("Y-m-d, H:i");
            echo "$postdate";
               ?>

           </span>  
           <input type="hidden" name="postdate" value="<?php echo $postdate; ?>"/>
           <br>
       <fieldset class="ui-grid-a">
    <div class="ui-block-a">
           <button type="submit" data-icon="check" data-iconshadow="true" data-iconpos="left" id="request" name="request">Request</button></div>
    <div class="ui-block-b">
      <button type="reset" data-icon="delete" data-iconshadow="true" data-iconpos="left">Cancel</button>
      </div>
     </fieldset>  
     <script>
     $( "#service option:selected" ).text();  
            </script>
            </form>               
                     </div>     
    <div data-role="footer" data-theme="c" align="center">
              </div>
         </div> 
    </body>
    </html>

更新

MessageUser

$mensajes = Messageuser::find(1)->conversaciones()->get();
$mensajes2 = Messageuser::find(4)->conversaciones()->get();
echo $mensajes;
echo $mensajes2;

消息模型

use Illuminate\Database\Eloquent\Model;
class MessageUser extends Model {
    protected $table ="message_user";

    public function conversaciones(){
        return $this->hasMany('App\Message','id_message','id');
    } 

}

UPDATED2

当我尝试在视图中排序时出现Illegal offset type

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Message extends Model {

    protected $table="messages";

    public function chat()
    {
        return $this->belongsTo('App\MessageUser','id');
    }

}

1 个答案:

答案 0 :(得分:2)

关系是hasMany,因此会返回一个收集集,而不是一个&#34; echoable&#34;事情:

$mensajes = Messageuser::find(1)->conversaciones()->get();
$mensajes2 = Messageuser::find(4)->conversaciones()->get();
\dd($mensajes, $mensajes2);

这将返回转储的两个Collection个对象。另请注意,您可以使用:

$mensajes = Messageuser::find(1)->conversaciones;

获得相同的回报。

现在您想加入结果,您可以使用merge类中Collection的内置方法:

$mensajes->merge($mensajes2);

基于created_at进行合并:

$mensajes = $mensajes->keyBy('created_at');
$mensajes->merge($mensajes2->keyBy('created_at'));