簡體   English   中英

Laravel中如何傳數據查看?

[英]How to pass data to view in Laravel?

我通過return View::make('blog', $posts);將數據傳遞到我的刀片視圖在我的刀片視圖中,我試圖運行一個@foreach ($posts as $post)我最終得到一個錯誤,指出$posts未定義。

我的問題是如何調用$posts數組?

您可以使用with方法將數據傳遞給視圖。

return View::make('blog')->with('posts', $posts);

值得一提的是,從 Laravel 5 開始,將數據傳遞給視圖現在是這樣完成的:

return view("blog", ["posts"=>$posts]);

或者:

return view("blog", compact("posts"));

文檔可在此處獲得。

如果您只想傳遞一個變量來查看,您可以使用

控制器

return view('blog')->withTitle('Laravel Magic method.');

視圖

<div>
  Post title is {{$title}}.
</div>

如果你想傳遞多個變量來查看,你可以使用

控制器

return view('blog')->withTitle('Laravel magic method')->withAuthor('Mister Tandon');

視圖

<div>
   Post title is {{$title}}.Author Name is {{$author}}
</div>

您還可以將數組作為視圖模板名稱之后的第二個參數傳遞,而不是將一堆->with()方法串在一起。

return View::make('blog', array('posts' => $posts));

或者,如果您使用的是 PHP 5.4 或更高版本,您可以使用更好的“短”數組語法:

return View::make('blog', ['posts' => $posts]);

如果您想在其他地方計算數組,這很有用。 例如,如果您有一堆變量,每個控制器都需要傳遞給視圖,並且您希望將其與每個特定控制器唯一的變量數組結合起來(例如,使用array_merge ),您可能會計算$variables (其中包含一個數組!):

return View::make('blog', $variables);

(我不經意間就這樣做了:如果出現語法錯誤,請告訴我......)

提示1:

使用 With(),這是一個最佳實踐

return view('about')->withName('Author Willson')->withJob('Writer');
return View::make('home')->with(compact('about'))
return View::make('home')->with('comments', $comments);

提示2:

使用緊湊()

return view(about, compact('post1','post2'));

提示3:

使用第二個參數:

return view("about", ["comments"=>$posts]);
use TCG\Voyager\Models\Jobtype;

class FormController extends Controller
{
public function index()
{
   $category = Jobtype::all();
   return view('contact', compact('category'));

}
}
  • 使用你的模型
  • 將其分配給變量
  • 將對象傳遞給視圖這是一個示例:

控制器:

use App\your_model_name;
funtion index()
{
$post = your_model_name::all();
return view('index')->with('this_will_be_used_as_variable_in_views',$post);
}

指數:

<h1> posts</h1>
@if(count($this_will_be_used_as_variable_in_views)>0)
@foreach($this_will_be_used_as_variable_in_views as $any_variable)
<ul>
<p> {{$any_variable->enter_table_field}} </p>
 <p> {{$any_variable->created_at}} </p>
</ul>

@endforeach
@else
<p> empty </p>
@endif

希望這可以幫助! :)

你也可以這樣做

$arr_view_data['var1'] = $value1;
$arr_view_data['var2'] = $value2;
$arr_view_data['var3'] = $value3;

return view('your_viewname_here',$arr_view_data);

您訪問此變量以查看$var1$var2$var3

你也可以用另一種方式做同樣的事情,

如果您使用的是 PHP 5.5 或最新版本,那么您可以按照以下方式進行操作,

控制器:

return view(index, compact('data1','data2')); //as many as you want to pass

看法:

<div>
    You can access {{$data1}}. [if it is variable]
</div>

@foreach($data1 as $d1)
    <div>
        You can access {{$d1}}. [if it is array]
    </div>
@endforeach

同樣的方式,您可以訪問您在緊湊函數中傳遞的所有變量。

希望能幫助到你 :)

您可以使用 compact 將表數據傳遞給查看。

$users = RoleModel::get();
 return view('super-admin',compact('users'));

您可以使用 with 方法將數據傳遞給視圖。

return view('greeting', ['name' => 'James']);

好的,所有答案都告訴您如何將數據傳遞給視圖,但沒有人解釋如何在視圖中讀取它。
如果您使用:

 //Routes/web.php
 ...
 Route::get('/user', function () {
    return view('profile', [
       'variable1' => 'value1' ,
       'variable2'=> 'value2' , // add as much as you want
    ]);
 });

要在視圖中讀取這些變量(在本例中是profile.blade.php文件):

@if($variable1)

 <p> variable1 = {{  $variable1  }}</p>

@endif

Laravel 完成所有必要的工作並為你創建$variable1

您還可以編寫將多個數據從控制器傳遞到視圖

 return \View::make('myHome')
            ->with(compact('project'))
            ->with(['hello'=>$hello])
            ->with(['hello2'=>$hello2])
            ->with(['hello3'=>$hello3]);

您可以使用以下方法傳遞數據以在 Laravel 中查看:

public function contact($id) {
    return view('contact',compact('id'));
}

例如,您有一個包含數據的數組。

$array1  =  $data;
$array2 = $data;

從您的控制器中,您可以使用 compact 傳遞此變量。 您可以傳遞任意數量的數組。

return view('data.index',compact('array1','array2'));

這里 data 是視圖中的一個文件夾, index 是擴展名為 index.blade.php 的文件

在視圖中,您可以像這樣調用數組

@foreach ($array1 as $something)
   // some operation
@endforeach

對於任何認為在您有大量變量要傳遞給視圖或者您希望變量可以同時被許多視圖訪問的情況下真的很乏味的人,這是另一種方式

在控制器中,您將要傳遞的變量定義為global變量,並將這些值賦予這些變量。

示例global $variable; $variable = 1; global $variable; $variable = 1;

現在在視圖中,在頂部,只需執行

<?php global $variable;?>

然后,您現在可以從視圖中的任何位置調用您的變量,例如

{{$variable}}

希望這可以幫助某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM