我是Laravel的初学者......
如何在Laravel中的控制器上调试一些值,结果可以像javascript上的语法console.log()
一样显示到控制台?
示例控制器功能:
class TestMeController extends Controller {
public function __construct()
{
$this->middleware('jsonify');
}
public function callMe($id)
{
$params = Request::all();
console.log($id); <== how to write code on Laravel?
}
}
答案 0 :(得分:9)
在Laravel中使用 dd($id)
,或者如果您不想暂停执行,可以使用 dump($var)
。
您仍然可以使用PHP的本机功能,例如 var_dump
, die
和 print_r
。
答案 1 :(得分:2)
dd($ var)非常棒,因为它在运行后会停止程序,所以通过移动它可以很好地了解哪行代码导致错误。
答案 2 :(得分:1)
您可以使用以下几种方法在 Laravel 中进行打印。
1. dump($var)
2. dd($id)
3. var_dump($var)
4. die($var)
5. print_r($var)
答案 3 :(得分:0)
在我的情况下,我将laravel 7与刀片模板一起使用并测试我的控制器 (MVC建筑技术) 我通常使用:
return dd($variable)
甚至只是
return $variable
答案 4 :(得分:0)
如果你只是想看数据
$events = new Event();
echo "<pre>";
print_r($events->all()->toArray());
echo "</pre>";
die;
答案 5 :(得分:-3)
使用带有json的返回响应:
public class AVLTree {
Node root;
public class Node{ //subclass of Node in AVL tree
private Node left;
private Node right;
int height = 1;
int key;
public Node(int n)
{
this.key = n;
height = 1;
}
}
public int height(Node n){
if(n == null)
return 0;
return n.height;
}
public void in(int key)
{
root = insert(root, key);
}
public Node insert(Node node, int key) //insert
{
if(node == null)
{
node = new Node(key);
}
else if(key < node.key)
{
node.left = insert(node.left, key);
if(height(node.left)- height(node.right) == 2) {
if(key < node.left.key)
{
node = rotateLeft(node);
}
else {
node.left = rotateRight(node.left);
rotateLeft(node);
}
}
}
else if(key > node.key)
{
node.right = insert(node.right, key);
if(height(node.right)- height(node.left) == 2) {
if(key < node.right.key)
{
node = rotateRight(node);
}
else {
node.right = rotateLeft(node.right);
rotateRight(node);
}
}
}
else {
node.height = Math.max(height(node.left), height(node.right)) + 1; //increase height of the node
}
return node;
}
public Node delete(Node root, int key) {
if(root == null)
return root;
Node temp;
if (key < root.key) {
root.left = delete(root.left, key);
}
else if(key > root.key) {
root.right = delete(root.right,key);
}
else if(key == root.key){
if(root.left == null || root.right == null){ //root has one child
if(root.left != null)
temp = root.left;
else
temp = root.right;
if( temp == null) { // no child
temp = root;
root = null;
}
else
root = temp;
temp = null;
}
else {
temp = minNode(root.right);
root.key = temp.key;
root.right = delete(root.right, temp.key);
}
}
if(root == null)
return root;
root.height = Math.max(height(root.left), height(root.right)) + 1;
//once deleted we must rebalance it
int balance = height(root.left) - height(root.right); //check balance of top node
if(balance > 1 && key < root.left.key) //left left rotation
return rotateRight(root);
else if(balance < -1 && key > root.right.key) //right right
return rotateLeft(root);
else if(balance > 1 && key > root.left.key) //left righ
return rotateRight(root);
else if(balance > -1 && key < root.right.key) //right left
return rotateRight(root);
return root;
}
private Node rotateLeft(Node y) {
Node x = y.left;
y.left = x.right;
x.right = y; //rotates
x.height = Math.max(height(x.left), height(x.right))+1;
y.height = Math.max(height(y.left), height(y.right))+1;
return x;
}
private Node rotateRight(Node x)
{
Node y = x.right;
x.right = y.left;
y.left = x; //rotates
x.height = Math.max(height(x.left), height(x.right))+1;
y.height = Math.max(height(y.left), height(y.right))+1;
return y;
}
void TpreOrder(Node node)
{
if (node != null)
{
System.out.print(node.key + " ");
TpreOrder(node.left);
TpreOrder(node.right);
}
}
void TpostOrder(Node node)
{
if (node != null)
{
TpreOrder(node.left);
TpreOrder(node.right);
System.out.print(node.key + " ");
}
}
public void TinOrder(Node root)
{
if(root != null) {
TinOrder(root.left);
System.out.print(root.key+" ");
TinOrder(root.right);
}
}
public void inOrder(Node root, ArrayList<Integer> pre)
{
if(root != null) {
inOrder(root.left, pre);
pre.add(root.key);
pre.add(root.key);
inOrder(root.right, pre);
}
}
public ArrayList<Integer> toArray() {
ArrayList<Integer> result = new ArrayList<Integer>();
inOrder(root, result);
return result;
}
public void preOrder(Node root, ArrayList<Integer> in)
{
if(root != null) {
preOrder(root.left, in);
in.add(root.key);
preOrder(root.right, in);
}
}
public void postOrder(Node root, ArrayList<Integer> post)
{
if(root != null) {
postOrder(root.right, post);
post.add(root.key);
postOrder(root.left, post);
}
}
private Node minNode(Node node) {
Node cur = node;
while(cur.left != null)
cur = cur.left;
return cur;
}
}
而不是console.log();
return response()->json($dataVar, status_code);