在我看来的条件

时间:2018-04-28 10:55:24

标签: php laravel laravel-5 laravel-5.6

我认为还有另一个问题。 此条件@if不起作用:

@if( ! empty($news))
    <div class="container">
        <div class="row justify-content-center">
            <h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
        </div>
    </div>
@else
    <h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Last News :</h1>
    <div class="col-md-8">
        <div class="row">
            @foreach($news as $new)
                <div class="card" style="width: 18rem; margin-left:30px;">
                    <img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
                    <div class="card-body">
                        <h5 class="card-title">{{$new['title']}}</h5>
                        <p class="card-text">
                            @if(strlen($new['content'])>150)
                                {{substr(strip_tags($new['content']),0,150)}}...
                            @else
                                {{$new['content']}}
                            @endif
                        </p>
                        <a href="#" class="btn btn-primary">More.</a>
                        @if(@admin)
                            <a href="{{action('NewsController@edit', $new['id'])}}" class="btn btn-warning">Edit</a></td>
                            <form action="{{action('NewsController@destroy', $new['id'])}}" method="post">
                                @csrf
                                <input name="_method" type="hidden" value="DELETE">
                                <button class="btn btn-danger" type="submit">Delete</button>
                            </form>
                        @endif
                    </div>
                </div>
            @endforeach
        </div>
    </div>
@endif

我想在没有新闻时显示Any News !,在有新闻时显示Last News ! 你能救我吗?

其他信息:

型号:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class News extends Model
{
    //
}

控制器:

<?php

namespace App\Http\Controllers;

use App\News;
use Illuminate\Http\Request;

class NewsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $news=News::all();
        return view('home',compact('news'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('news.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $news = new News();
        $news->title=$request->get('title');
        $news->content=$request->get('content');
        $news->picture=$request->get('picture');
        $news->save();
        return redirect('news')->with('success', 'Les Informations ont bien été ajoutées.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $news = News::findOrFail($id);
        return view('news');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $news = News::find($id);
        return view('news.edit',compact('news','id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $news= News::find($id);
        $news->title=$request->get('title');
        $news->content=$request->get('content');
        $news->picture=$request->get('picture');
        $news->save();
        return redirect('news');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $news = News::find($id);
        $news->delete();
        return redirect('news')->with('success','La News à bien été supprimée.');
    }
}

查看:

@extends('layouts.app')

@section('content')
    @if(empty($news))
        {{--<div class="col-md-12">--}}
            {{--<div class="row">--}}
                <h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
            {{--</div>--}}
        {{--</div>--}}
    @else
        <h1 style="color:#fff;text-decoration:underline;margin-left:25px;">Last News :</h1>
        <div class="col-md-12">
            <div class="row">
                @foreach($news->sortByDesc('created_at') as $new)
                    <div class="card" style="width: 18rem; margin: 10px 0 10px 20px;">
                        <img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
                        <div class="card-body">
                            <h5 class="card-title">{{$new['title']}}</h5>
                            <p class="card-text">
                                @if(strlen($new['content'])>150)
                                    {{substr(strip_tags($new['content']),0,150)}}...
                                @else
                                    {{$new['content']}}
                                @endif
                            </p>
                            <a href="#" class="btn btn-primary">More.</a>
                            @admin
                                <a href="{{action('NewsController@edit', $new['id'])}}" class="btn btn-warning">Editer</a>
                                <form action="{{action('NewsController@destroy', $new['id'])}}" method="post">
                                    @csrf
                                    <input name="_method" type="hidden" value="DELETE">
                                    <button class="btn btn-danger" type="submit">Supprimer</button>
                                </form>
                            @endadmin
                        </div>
                    </div>
                @endforeach
            </div>
        </div>
    @endif
@endsection

路线:

<?php    
/*
 * Route Resource
 */

Route::resource('news', 'NewsController');

/*Route HOME*/
Auth::routes();

Route::get('/', 'HomeController@index')->name('home');
Route::get('/news', 'NewsController@index')->name('news');

1 个答案:

答案 0 :(得分:0)

您可以尝试:

@if($news->count())
//enter your code here if you have news
    <h1 style="color:#fff;text-decoration:underline;margin-left:25px;">Last News :</h1>
    <div class="col-md-12">
        <div class="row">
            @foreach($news->sortByDesc('created_at') as $new)
                <div class="card" style="width: 18rem; margin: 10px 0 10px 20px;">
                    <img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
                    <div class="card-body">
                        <h5 class="card-title">{{$new['title']}}</h5>
                        <p class="card-text">
                            @if(strlen($new['content'])>150)
                                {{substr(strip_tags($new['content']),0,150)}}...
                            @else
                                {{$new['content']}}
                            @endif
                        </p>
                        <a href="#" class="btn btn-primary">More.</a>
                        @admin
                            <a href="{{action('NewsController@edit', $new['id'])}}" class="btn btn-warning">Editer</a>
                            <form action="{{action('NewsController@destroy', $new['id'])}}" method="post">
                                @csrf
                                <input name="_method" type="hidden" value="DELETE">
                                <button class="btn btn-danger" type="submit">Supprimer</button>
                            </form>
                        @endadmin
                    </div>
                </div>
            @endforeach
        </div>
    </div>
@else
//enter your code here if you don't have news
    {{--<div class="col-md-12">--}}
        {{--<div class="row">--}}
            <h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
        {{--</div>--}}
    {{--</div>--}}
@endif

我希望它适合你